如何在Cucumber中管理重复的步骤?

时间:2017-07-13 05:30:00

标签: java cucumber cucumber-jvm

我的Cucumber中有不同的场景。第一个(FirstTask Scenario)是:

@tag
Feature: Click on link
  The webdriver should be able to click on link

  @tag1
  Scenario: Title of your scenario
    Given I am in "http://suvian.in/selenium/1.1link.html"
    When I clcik on the link
    Then It navigates to the "http://suvian.in/selenium/1.1link_validate.html"
    And I can see the message "Link Successfully clicked"

,第二个是(SecondTask Scenario):

@tag
Feature: Fill the input text
  The webdriver should be able to fill an input in a form

  @tag1
  Scenario: Title of your scenario
    Given I am in "http://suvian.in/selenium/1.2text_field.html"
    When I enter "Salman" in the input
    And I click on "Next Task" 
    Then It navigates to the "http://suvian.in/selenium/1.3age_plceholder.html"

当我运行第二种情况时,jvm-cucumber给了我以下内容:

@tag1
  Scenario: Title of your scenario                                              # D:/Workspace/IgniteTask/src/features/SecondTask.feature:6
    Given I am in "http://suvian.in/selenium/1.2text_field.html"                # FirstTask_Step.i_am_in(String)
    When I enter "Salman" in the input
    And I click on "Next Task"
    Then It navigates to the "http://suvian.in/selenium/1.3age_plceholder.html" # FirstTask_Step.it_navigates_to_the(String)

1 Scenarios (1 undefined)
4 Steps (1 skipped, 2 undefined, 1 passed)
0m11.038s


You can implement missing steps with the snippets below:

@When("^I enter \"([^\"]*)\" in the input$")
public void i_enter_in_the_input(String arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@When("^I click on \"([^\"]*)\"$")
public void i_click_on(String arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

在部分中:

Given I am in "http://suvian.in/selenium/1.2text_field.html"                # FirstTask_Step.i_am_in(String)
     Then It navigates to the "http://suvian.in/selenium/1.3age_plceholder.html" # FirstTask_Step.it_navigates_to_the(String)

这意味着对于这两个步骤,它会运行firstTask

中的步骤

对我来说没有意义。因为每个场景都应该有自己的步骤。但是这里的sceondTask场景是使用第一步吗?

1 个答案:

答案 0 :(得分:0)

对于每个场景,每次都包含“然后...注释”的所有类,这些类存在于' glue' cucumberoptions中提到的选项已加载。

通过将您在When Then ...注释的值中创建的正则表达式与场景步骤文本的值相匹配,选择要用于每个场景步骤的步骤定义

特定的stepdefinition类和功能文件之间没有映射。因此,如果您需要不同的行为,则需要修改步骤的文本并记下相应的步骤定义方法。

相同的逻辑将适用于挂钩之前和之后。它们将从指定胶水选项中包含的任何类中选取。