【问题标题】:ASP.NET MVC, BDD, Specflow and WatiN: putting the application in a specific stateASP.NET MVC、BDD、Specflow 和 WatiN:将应用程序置于特定状态
【发布时间】:2012-06-06 23:40:50
【问题描述】:

我是 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

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

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

让我感到困扰的是,为了使这个测试独立于其他测试,我必须编写代码才能再次登录网站。这是要走的路吗?我怀疑。

我想知道是否有最佳实践可用于测试此类场景。我应该保持浏览器打开并让测试在同一个浏览器上按特定顺序运行吗?还是应该将 MVC 应用程序置于特定状态?

【问题讨论】:

    标签: asp.net-mvc bdd watin specflow


    【解决方案1】:

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

    Given I am signed in as user@domain.tld
    

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

    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");
        }
    }
    

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

    您也可以使用 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
    }
    

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

    在用户登录时,需要将其置于特定状态的不是 MVC 应用程序,而是需要将浏览器置于特定状态——即写入身份验证 cookie。我不确定你是否可以这样做,因为 auth cookie 是加密的。我总是发现让 SF 在每个场景开始时完成身份验证步骤会更容易。

    【讨论】:

    • 感谢丹的出色回答。我将按照建议重用这些步骤。
    【解决方案2】:

    您可能需要考虑进一步完善您的解决方案。阅读Dan North's Who's Domain is it anyway,然后问问自己这两个场景是否真的属于一起。我发现 danludwig 建议的方法是有价值的,但是根据他们的领域分离你的场景更有价值。将其视为重构您的场景。

    您仍然可以从另一个场景的活动中构建一个场景,但问问自己,您是否真的需要执行相同的步骤,或者为您的会话提供一个模拟的“已经登录”对象会更好。

    【讨论】:

      猜你喜欢
      • 2011-07-11
      • 1970-01-01
      • 2011-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 2021-11-08
      • 1970-01-01
      相关资源
      最近更新 更多