Datatable in background in cucumber

Viewed 714

How would I create a data table in the background and access its data in the scenario?

I am running scenarios with variations of values ​​and data and would like to know if I can access the values ​​entered in a backgorund table

Background: 
Given Essas informacoes:
  | Price | Name | Insurance     | Damage           | Type     | Email |
  |       |      |  3.000.000,00 | Full Coverage    | Gold     |       |
  |       |      | 10.000.000,00 | No Coverage      | Silver   |       |
  |       |      | 25.000.000,00 | Partial Coverage | Platinum |       |
  |       |      |               |                  | Ultimate |       |
  |       |      |               |                  |          |       |
And E o navegador aberto
And O site carregado
And Tendo selecionado Truck
@ValidarCotas  
Scenario: Selecionar cotacao de seguro de tipo diferente de acordo com valores variados
        Given Esta na tela do formulario de Truck
        And Preenchido os dados do veiculo e os dados do segurado
        When Preencho os campos do formulario de dados do seguro  "**<Insurance>**" "**<Damage>**"
        And Seleciono cota de seguro "<Type>"
        Then Valido se a cota foi enviada para o email
        And Valido se o valor da cota for o mesmo que eu selecionei no site
        When Fecho o navegador
2 Answers

You need to use "Scenario Outline" here, it can be used to run the same Scenario multiple times, with different combinations of values.

Scenario Outline: eating
  Given there are <start> cucumbers
  When I eat <eat> cucumbers
  Then I should have <left> cucumbers

  Examples:
    | start | eat | left |
    |    12 |   5 |    7 |
    |    20 |   5 |   15 |

Use Background if you have multiple repeatable "Given" steps in each test case.

As per my understanding you want to reuse your examples across multiple scenarios. AFAIK it is not supported when using gherkin with cucumber.

One of the way is to reuse examples between scenarios by moving examples in external file and referencing it in your scenario which is supported using gherkin with qaf or BDD2 with qaf but not cucumber.

For example:

Background: 
Given E o navegador aberto
And O site carregado
And Tendo selecionado Truck

@ValidarCotas
@dataFile:resources/data/testdata.csv   
Scenario: Selecionar cotacao de seguro de tipo diferente de acordo com valores variados
        Given Esta na tela do formulario de Truck
        And Preenchido os dados do veiculo e os dados do segurado
        When Preencho os campos do formulario de dados do seguro  "**<Insurance>**" "**<Damage>**"
        And Seleciono cota de seguro "<Type>"
        Then Valido se a cota foi enviada para o email
        And Valido se o valor da cota for o mesmo que eu selecionei no site
        When Fecho o navegador

Refer documentation for more details.

Related