当前位置: 首页 > 知识库问答 >
问题:

Serenity Cucumber,如何跨多个功能文件使用相同的场景步骤

时浩波
2023-03-14

使用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: 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。运行时。DuplicateStepDefinitionException是我理解的,但我一直在读到serenity cucumber提供了重用步骤的选项,这也是一个好主意。

那么,如何在其他测试中重用场景或场景的步骤定义?我不需要为它们提供新方法,我只需要调用我在之前的场景中创建的现有方法。

有办法做到这一点吗?

我可以这样做吗?(或者我甚至不需要写下背景?)

  @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个答案

韩照
2023-03-14

找到答案。。。

只要要素文件文本:

<代码>给定用户位于登录页上

在多个要素文件中写入相同的,只需编写一次“步骤定义”方法

 @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()并且不需要在该测试的步骤定义文件上编写代码。

 类似资料:
  • @cucumberoptions(features=“src/test/resources/features/xxxxx/xxxxx.feature”,标签=“@tag1”,胶={“com.xxxx.sfdc.opportunities.stepdefinities”},格式={“pretty”,“html:target/cucumber-reports/cucumber-pretty”,“jso

  • 我们已经知道场景是并行运行的。但是我们需要从功能文件(将从另一个功能文件调用)返回变量。 我们在功能文件中有如下多个场景: 我们正在重用上面的特征文件,以获取用于另一个特征文件的变量。然而,其他特征文件似乎间歇性失败,抱怨从其他特征文件获得的变量为空。 我的假设是返回的变量没有正确返回,因为功能文件上有多个场景。我们试图删除@负面场景,但只有1个场景。突然间,断断续续的故障消失了。 在保持并行运行

  • 我正在使用的弹出窗口来显示菜单选项。 但问题是,它不适用于多种场景。 我的意思是,当我点击第一个菜单按钮时,同样的方法会被触发,因此每次都会打开同样的菜单。 处理此特定场景的更好方法应该是什么。 这是小吃

  • 这种方法的优点是,每当我添加一个新模板时,我就不需要更新特性测试。 谢谢

  • 我正在使用Maven SureFire,TestNG(扩展AbstractTestNGCucumber测试)和Cucumber,并且有几个功能文件,每个文件都有几个场景。我希望能够在一个功能文件中并行运行每个场景,但一次只能运行一个功能文件。这有可能吗? 举个例子: 我希望场景1a、1b和1c在功能文件1中并行运行。一旦这些完成,运行场景2a和功能2等的2b。 这是当前一次从所有功能文件运行所有方

  • 这个应用程序的每个JVM应该使用相同的数据库吗?否则跟踪令牌不会在同一个应用程序中“共享”? 如何在运行传奇的相同应用程序中拆分事件?一个saga类型或saga实例是否总是在同一个应用程序上处理(直到它被关闭,所以另一个实例负责它)? 还是每个JVM都接收事件,并且每个相同类型的传奇都将运行?(并导致发送重复命令和错误) 等。还有很多问题。