ASP.NET MVC,BDD,Specflow和WatiN:将应用程序置于特定状态

Lou*_*upi 5 asp.net-mvc bdd watin specflow

我是BDD,Specflow和WatiN的新手.我想使用这些工具自动化我的ASP.NET MVC应用程序的验收测试.

我已经想出了如何基本上使用这些工具,并且我成功构建了我的第一个验收测试:登录到网站.

这是测试的小黄瓜:

Feature: Log on to the web 
    As a normal user
    I want to log on to the web site

Scenario: Log on
    Given I am not logged in
    And I have entered my name in the username textbox
    And I have entered my password in the password textbox
    When I click on the login button
    Then I should be logged and redirected to home
Run Code Online (Sandbox Code Playgroud)

现在,我想写一堆其他测试,它们都需要用户进行身份验证.例如:

Feature: List the products 
    As an authenticated user
    I want to list all the products

Scenario: Get Products
    Given I am authenticated
    And I am on the products page
    When I click the GetProducts button
    Then I should get a list of products
Run Code Online (Sandbox Code Playgroud)

让我烦恼的是,为了使这个测试独立于其他测试,我必须编写代码再次登录网站.这是要走的路吗?我怀疑.

我想知道是否有最佳实践可以用来测试这样的场景.我应该在同一个浏览器上打开浏览器并按特定顺序运行测试吗?或者我应该将MVC应用程序置于特定状态?

dan*_*wig 4

为此,我们有一个特定的给定步骤,在 Gherkin 中看起来像这样:

Given I am signed in as user@domain.tld
Run Code Online (Sandbox Code Playgroud)

正如您所提到的,此步骤基本上重复使用其他步骤来登录用户。我们还有一个需要密码的“重载”,以防测试用户有非默认测试密码:

Given I am signed in as user@domain.tld using password "<Password>"

[Binding]
public class SignInSteps
{
    [Given(@"I am signed in as (.*)")]
    public void SignIn(string email)
    {
        SignInWithSpecialPassword(email, "asdfasdf");
    }

    [Given(@"I am signed in as (.*) using password ""(.*)""")]
    public void SignInWithSpecialPassword(string email, string password)
    {
        var nav = new NavigationSteps();
        var button = new ButtonSteps();
        var text = new TextFieldSteps();
        var link = new LinkSteps();

        nav.GoToPage(SignOutPage.TitleText);
        nav.GoToPage(SignInPage.TitleText);
        nav.SeePage(SignInPage.TitleText);
        text.TypeIntoTextField(email, SignInPage.EmailAddressLabel);
        text.TypeIntoTextField(password, SignInPage.PasswordLabel);
        button.ClickLabeledSubmitButton(SignInPage.SubmitButtonLabel);
        nav.SeePage(MyHomePage.TitleText);
        link.SeeLinkWithText("Sign Out");
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为这是最好的方法,因为您不应该能够保证所有测试都按特定顺序运行。

不过,您也可以使用 SpecFlow 标签来执行此操作,并让该标签执行 BeforeScenario。这可能看起来像这样:

Feature: List the products 
    As an authenticated user
    I want to list all the products

@GivenIAmAuthenticated
Scenario: Get Products
    Given I am on the products page
    When I click the GetProducts button
    Then I should get a list of products

[BeforeScenario("GivenIAmAuthenticated")]
public void AuthenticateUser()
{
    // code to sign on the user using Watin, or by reusing step methods
}
Run Code Online (Sandbox Code Playgroud)

...我应该将 MVC 应用程序置于特定状态吗?

当用户登录时,不是 MVC 应用程序需要进入特定状态,而是浏览器需要进入特定状态——即写入身份验证 cookie。鉴于 auth cookie 已加密,我不确定您是否可以执行此操作。我总是发现让 SF 在每个场景开始时完成身份验证步骤会更容易。