加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – ASP.NET MVC,BDD,Specflow和WatiN:将应用程序置

发布时间:2020-12-16 06:59:32 所属栏目:asp.Net 来源:网络整理
导读:我是BDD,Specflow和WatiN的新手.我想使用这些工具自动化我的ASP.NET MVC应用程序的验收测试. 我已经想出了如何基本上使用这些工具,并且我成功构建了我的第一个验收测试:登录到网站. 这是测试的小黄瓜: Feature: Log on to the web As a normal user I want
我是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应用程序置于特定状态?

解决方法

为此,我们有一个特定的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
}

…should I put the MVC application in a specific state?

登录用户时,不是需要将MVC应用程序置于特定状态,而是需要将其置于特定状态的浏览器 – 即编写身份验证cookie.鉴于auth cookie已加密,我不确定您是否可以这样做.我总是发现让SF在每个场景开始时通过身份验证步骤更容易.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读