是否可以在新场景的开头包含相同的几行场景?

Is it possible to include the same few lines of scenario at the beginning of a new one?

我有一个 138 次测试的长场景,允许我测试工作流是否正常工作。

但是,我不太喜欢重复允许我以管理员身份登录的相同 6 行,它们是:

Given I am on "user/login"
And I fill in "admin@admin.com" for "name"
And I fill in "admin" for "pass"
And I press "Log in"
Then I should get a "200" HTTP response
And I should see "Dashboard"

到目前为止,我将这部分重复了 6 次,并且我计划需要为其他一些测试增加一些时间。

所以我的问题如下:有没有办法通过 FeatureContext 文件或任何其他方式重复这些行?

提前致谢


所以我是这样做的:

我没有一个接一个地调用 Gherkin 句子,而是解析了 vendor/ 目录以查找句子生成方式的示例。

连接我的函数如下所示:

/**
 * @throws ElementNotFoundException
 * @throws Exception
 * @Given I am the administrator
 */
public function iAmTheAdministrator(){

  $this->visitPath('/user/login');
  $element = $this->getSession()->getPage();
  $element->fillField('name', 'admin@admin.com');
  $element->fillField('pass', 'admin');
  $element->pressButton("Se connecter");
  $this->assertSession()->pageTextContains('Dashboard');
}

这很简单,而且效果很好

我知道有 2 种重复步骤的方法。一个是背景步骤,另一个涉及 运行 宁步骤片段。

每个场景开始时的背景步骤运行。

Background:
    Given I have done this
    And this other thing

Scenario: Do stuff
    When I do this
    Then stuff should happen

这仅在所有测试都具有相同的启动程序时才有效...

片段 运行 每当您调用它们时,我认为您更愿意

Given I have logged in as an administrator

步骤定义:

Given(/^I have logged in as an administrator$/) do
    steps %{
    Given I am on "user/login"
    And I fill in "admin@admin.com" for "name"
    And I fill in "admin" for "pass"
    And I press "Log in"
    Then I should get a "200" HTTP response
    And I should see "Dashboard"
    }
end

这允许您只使用一个步骤,您可以随时调用它到 运行 多个步骤

希望对您有所帮助。