Serenity-Cucumber,如何在多个功能文件中使用相同的方案步骤

时间:2019-05-10 15:58:49

标签: cucumber serenity-bdd test-suite

我使用Serenity-Cucumber,试图构建一个测试套件,以便可以在多个功能文件中重复使用步骤定义(给出,何时,然后,以及..)。

例如:

Scenario: User Logs in
    Given User is on the Login page
    When User Logs in using 'userName' and 'password'
    Then User on page containing: '/logedin/url/path'

上述测试用例登录了一个用户,我将需要在其他情况下使用它。例如,如果我添加了一个测试用例来更新密码,则在更新密码之前必须执行上述方案。

该测试将需要执行登录步骤,然后更新密码步骤。据我所知,似乎我需要在Background:步骤中使用它。因此,在更新密码方案之前,我将具有以下内容:

Background: User Logs in
    Given User is on the Login page
    When User Logs in using 'userName' and 'password'
    Then User on page containing: '/logedin/url/path'

Scenario: User Updates Password
    Given User is on the Manage Account page
    When User clicks Update Password
    And User type 'existingPassowrd' and 'newPassword'
    And User clicks Update Password
    Then Password is displayed as 'newPassword'

这给了我一个我理解的错误cucumber.runtime.DuplicateStepDefinitionException,但我一直在读,宁静黄瓜让您可以选择重复使用步骤,我再次知道这是个好主意。

那么,如何在其他测试中重用场景或场景的步骤定义?我不需要它们的新方法,只需要调用在上一个方案中创建的现有方法即可。

有没有办法做到这一点?

我可以做这样的事情吗? (或者我什至不需要写背景吗?)

  @Steps
  User user;

  //This is the background in the feature file.
  //Instead of creating methods, I would just reference the pre-existing ones from the other test case.

  @Given("^User is on the Login page$")
  @When("^User Logs in using '(.*)' and '(.*)'$")
  @Then("^User on page containing: '(.*)'$")

  //This is the Scenario in the feature file

  @Given("^User is on the Manage Account page$")
  public void user_is_on_the_manage_account_page(String expectedUrl) throws Exception {

    user.is_on_page(expectedUrl);
  }

   @When("^User clicks Update Password$")
  public void user_clicks_update_password() throws Exception {

    user.click_update_password();
  }

  code continues
  ...
  ...
  ...

1 个答案:

答案 0 :(得分:0)

找到答案...

只要功能文件文本为

Given User is on the Login page

在多个功能文件中的编写方式相同,您只需编写一次“步骤定义”方法

 @Given("^User is on the Login page$")
  public void user_is_on_the_login_page() throws Exception {

    user.is_on_login_page();
  }

“用户在登录页面上”可以继续在多个功能文件上进行重写,它将始终使用相同的方法user_is_on_the_login_page(),并且无需在该测试的步骤定义文件上编写任何代码。