Pass POJOs in Cucumber Example table

北战南征 提交于 2021-01-28 04:16:53

问题


Description: As a test developer, I would like to use a single scenario to test 3 different environments.

Simplified Scenario example:

  @smoke
  Scenario: Login to the login page and assert that the user is logged in
    Given User navigates to the page
    And User enters valid login credentials
    When User clicks on the login button
    Then Landing page can be seen

Data ( These are grabbed from a property file - converted to POJO ) :

Env1.class
url = www.environment1.com
username = john
password = doe1

Env2.class
url = www.environment2.com
username = john2
password = doe2

Env2.class
url = www.environment3.com
username = john3
password = doe3

Test Setup

  1. Each environment has its own test runner ( failsafe )
  2. Each environment runs in parallel.
  3. Test runs and is built via ~mvn clean verify
  4. Tests are property file dependant as environments may change.

Potential solution: Is there a way to pass POJOs in the Example Table? or Cucumber's data table?

I am new to BDD and Cucumber - any help would be great. Thank you.

TLDR: is there a way to pass the Prop File variable in the Examples Table in Cucumber?

| URL | Username | Password | 
| env1.getUrl | env1.getUsername | env1.getPassword |

So it'll be

 @smoke
  Scenario: Login to the login page and assert that the user is logged in
    Given User navigates to the page <URL>
    And User enters valid login credentials <Username> and <Password>
    When User clicks on the login button
    Then Landing page can be seen

回答1:


You can use the scenario outline to run the same scenario with different data for each run. But it will be not parallel. It is sequential. The feature file is,

  @smoke
  Scenario Outline: Login to the login page and assert that the user is logged in
    Given User navigates to the page <URL>
    And User enters valid login credentials <Username> and <Password>
    When User clicks on the login button
    Then Landing page can be seen
  Example:
  |URL                 |UserName|Passowrd|
  |www.environment1.com|john1   |doe1    |
  |www.environment2.com|john2   |doe2    |    
  |www.environment2.com|john3   |doe3    |

You can use a single runner class. No need to use either property file nor pojo class.




回答2:


You can achieve that using cucumber extension for BDD2. By using it you can have external examples or you can use properties in example as below:

| URL | Username | Password | 
| ${env1.getUrl} | {env1.getUsername} | ${env1.getPassword} |

Alternate is you can use CSV or XML data provider.

In order to use pojo, you need to modify your step definition to accept either DataTable or POJO as argument. When accepting POJO as argument you need to write transformer.

When you use cucumber extension you can also use QAFTestStep which accepts POJO without addition effort. Here you can find step examples and feature file.



来源:https://stackoverflow.com/questions/59654972/pass-pojos-in-cucumber-example-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!