如何使用不同的用户角色多次运行相同的BDD功能

时间:2014-09-05 10:57:33

标签: php bdd behat mink

无论如何,我们可以多次运行相同的测试来验证功能。

这是我的专题文件:

Feature: End to end tests 

    I want an End to End test pack
    As Super user and Admin user
    So that I can ensure that the integrated components of the application function as expected

    Background:
            Given I have the login Page
            When I login to application
            Then the list is displayed

    @javascript
    Scenario: To verify the functionality on the Dashboard          
            When I navigate to the Dashboard Page
            Then the Dashboard Page is displayed

我想为2个不同的用户运行此方案。有没有办法可以使用多个用户/角色运行相同的功能。

我有几个其他功能文件需要使用2或3个不同的用户运行,我需要在一夜之间运行

请参阅下面的上下文文件:

public function iLoginToApplication() { 
$page = $this->getSession()->getPage();
$page->find('css', '#username')->setValue("admin");
$page->find('css', '#password')->setValue("Password");
$signInButton->press();
}

1 个答案:

答案 0 :(得分:1)

是的,你可以。直接位于Feature: …下的部分从功能角度来看大部分都是无用的,并用作文档。 AFIAK你可以删除它或写任何你想要的东西,你的测试将完全相同。

您要查找的内容为scenario outlines,它们的确属于此类,但您需要更新一些场景,并仍然在Examples下手动指定每个用户。< / p>

Feature: End to end tests 

    I want an End to End test pack
    As Super user and Admin user
    So that I can ensure that the integrated components of the application function as expected

    Background:
        Given I have the login Page

    @javascript
    Scenario Outline: To verify the functionality on the Dashboard         
        When I login to application as "<username>" with "<password>"
        Then the list is displayed
        When I navigate to the Dashboard Page
        Then the Dashboard Page is displayed

    Examples:
        | username | password |
        |  super   |  qwerty  |
        |  admin   |  qwerty  |

/**
 * @When /^I login to application$/
 * @When /^I login to application as "(.+)" with "(.+)"$/
 */
public function iLoginToApplication($username = null, $password = null) {
    $page = $this->getSession()->getPage();
    $page->find('css', '#username')->setValue(isset($username) ? $username : 'admin');
    $page->find('css', '#password')->setValue(isset($password) ? $password : 'Password');
    $signInButton->press();
}

另一种方法是在运行套件之前在全局范围内配置它,例如,在运行Behat时将带有用户名的环境变量传递给PHP,并使用不同的配置多次运行所有内容。这将是上述解决方案的一个黑客,而不是直观的,所以你最好以Behat的方式做到这一点。