Behat3 在自定义步骤中重定向

Behat3 redirect within custom step

我正在尝试从自定义步骤中重定向到 url。这是可行的 behat 2.x 通过:

return new Step\Then("my/url/page");

遗憾的是,在 behat 3.0 中,您无法从自定义步骤中调用外部步骤!

如何在自定义步骤中执行重定向?

步骤链接已从 Behat 3 中删除,因为它被认为是一种不好的做法。

explained by everzet:

Use the code tools like abstraction, compositions and inheritance together with a simple method calls :) Chained steps was an anti-pattern. It was hard to debug and maintain them. And in most cases, what could be done with 2-3 chained steps could be done with 1-2 method calls.

here

Because as soon as you start doing that you:

  1. Can not refactor your contexts
  2. Can not clearly see what the code actually does
  3. Can not easily reword your features

And if you think that using inline steps is easier:

return array(
    new Step\Given(‘I am on the homepage’),
    new Step\When(‘I follow ”Login”’),
    new Step\When(‘I fill ”Username” with “‘ . $username . '"’),
    new Step\When(‘I fill ”Password” with “‘ . $password . '"’),
    new Step\When(‘I click “Login"’),
);

It is not:

$session = $this->getSession();
$page = $session->getPage();

$session->visit($this->locateUrl(‘/‘));
$page->clickLink(‘Login’);
$page->fillField(‘Username’, $username);
$page->fillField(‘Password’, $password);
$page->pressButton(‘Login’);

另见 https://github.com/Behat/Behat/issues/546

Behat 3 有一个带有步骤链接实现的扩展,但它已损坏,不太可能得到官方支持:https://github.com/Behat/ChainedStepsExtension

解决方案

要解决您的问题,您可以扩展 Behat\MinkExtension\Context\RawMinkContext 并使用它来访问会话:

$this->getSession()->visitPath('my/url/page');

您还可以查看 the page object extension