gpt4 book ai didi

asp.net-mvc - ASP.NET MVC、BDD、Specflow 和 WatiN : putting the application in a specific state

转载 作者:行者123 更新时间:2023-12-04 19:14:54 31 4
gpt4 key购买 nike

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

我已经弄清楚如何基本上使用这些工具,并且我成功地构建了我的第一个验收测试:登录网站。

这是此测试的小 cucumber :

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 完成每个场景开始时的身份验证步骤会更容易。

关于asp.net-mvc - ASP.NET MVC、BDD、Specflow 和 WatiN : putting the application in a specific state,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10855224/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com