0

我是 BDD 和 C# 的新手,我在基本功能和步骤定义方面取得了一些成功。

我现在正在尝试编写一个步骤 def,它在网页上声明一些文本。问题是,文本显示时跨越多行,其 HTML 标记如下:

<span>
    Your username or password didn't match. Please check you've entered them correctly. 
    <br>
    <br>
    If you're not sure about your username (or not sure if you're registered), you can <a href="/MyAccount/ForgottenYourDetails/ForgottenUsername">check your username here</a>. You can then reset your password if you need to.
</span>

我的功能文件如下所示:

Scenario Outline: Customer tries to login multiple times and on the 5th try locks their account
Given a registered user with a username of 'username' 
    * has <x> failed login attempts
    * is '<status>'
    * I am on the login page
When I enter 'username' and 'password'
    * I click Login
Then I will see the login '<error message>'
Examples: 
    | x | status   | error message|
    | 0 | Unlocked | Your username or password didn't match. Please check you've entered them correctly. If you're not sure about your username (or not sure if you're registered), you can check your username here. You can then reset your password if you need to.                         |
    | 1 | Unlocked | Your username or password didn't match. Please check you've entered them correctly. If you're not sure about your username (or not sure if you're registered), you can check your username here. You can then reset your password if you need to.                         |
    | 2 | Unlocked | Your username or password didn't match. Please check you've entered them correctly. If you're not sure about your username (or not sure if you're registered), you can check your username here. You can then reset your password if you need to.                         |
    | 3 | Unlocked | Your username or password didn't match. Please check you've entered them correctly. If you're not sure about your username (or not sure if you're registered), you can check your username here. You can then reset your password if you need to.                         |
    | 4 | Unlocked | Your account is locked. Don't worry, this is simply a security measure to protect your personal information and ensure only you have access to your account. You can unlock your account yourself online by answering your security question and resetting your password. |

我遇到问题的步骤是 THEN 步骤

Then I will see the login '<error message>'

这一步后面的步骤定义如下

 [Then(@"I will see the login '(.*)'")]
 public void ThenIWillSeeTheLogin(string p0)
 {
    var actual = WebBrowser.Current.FindElement(By.Id("validationSummarycontainer")).Text;
    Assert.AreEqual(p0, actual);
 }

因为我要检查的文本是用换行符显示的,所以我尝试了几个选项:

  1. 如上所述,但是当从网页声明文本时, \r\n\r\n 代替了换行符,但不是我的预期 - 所以失败
  2. 我试过把 \r\n\r\n 放在“正确”之间。和“如果”,例如“正确。\r\n\r\nIf”,但是这会引发异常

用户代码未处理 NUnit.Framework.InconclusiveException HResult=-2146233088 消息=未找到一个或多个步骤的匹配步骤定义。使用系统;使用 TechTalk.SpecFlow;

namespace MyNamespace
{
    [Binding]
    public class StepDefinitions
    {
        [Then(@"I will see the login '(.*)'t match\. Please check you've entered them correctly\.\\r\n\\r\nIf you'(.*)'re registered\), you can check your username here\. You can then reset your password if you need to\.'")]
    public void ThenIWillSeeTheLoginTMatch_PleaseCheckYouVeEnteredThemCorrectly_R_R_IfYouReRegisteredYouCanCheckYourUsernameHere_YouCanThenResetYourPasswordIfYouNeedTo_(string p0, string p1)
        {
            ScenarioContext.Current.Pending();
        }
    }
}

Source=nunit.framework StackTrace: 在 NUnit.Framework.Assert.Inconclusive(String message, Object[] args) at lambda_method(Closure, String, Object[]) at TechTalk.SpecFlow.UnitTestProvider.NUnitRuntimeProvider.TestInconclusive(String message) at TechTalk.SpecFlow.UnitTestProvider.NUnitRuntimeProvider.TestPending(String message) 在 TechTalk.SpecFlow.ErrorHandling.ErrorProvider.ThrowPendingError(TestStatus testStatus, String message) 在 TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnAfterLastStep() 在 TechTalk.SpecFlow.TestRunner.CollectScenarioErrors () 在 MyBDD.OR_RegressionFeature.ScenarioCleanup() 在 d:\Data\jonec34\My Documents\Visual Studio 2012\Projects\MyBDD\MyBDD\OR-Regression.feature.cs:MyBDD.OR_RegressionFeature 的第 0 行。CustomerTriesToLoginMultipleTimesAndOnThe5ThTryLocksTheirAccount(String x, String status, String errorMessage, String[] exampleTags) 在 d:\Data\jonec34\My Documents\Visual Studio 2012\Projects\MyBDD\MyBDD\OR-Regression.feature:line 38 InnerException:

不幸的是,我现在很难过,如果有人可以提供帮助或提供其他很棒的建议

提前致谢

4

1 回答 1

1

如果这是我,我会以稍微不同的方式构建我的 HTML 和我的测试。对于测试,我将消息分成两部分,主要消息和解决建议,如下所示:

Scenario Outline: Customer tries to login multiple times and on the 5th try locks their account
Given a registered user with a username of 'username' 
    * has <x> failed login attempts
    * is '<status>'
    * I am on the login page
When I enter 'username' and 'password'
    * I click Login
Then I will see the message '<main error message>'
And I will see the advice '<resolution advice>'
Examples: 
    | x | status   | main error message| resolution advice|
    | 0 | Unlocked | Your username or password didn't match. Please check you've entered them correctly. |If you're not sure about your username (or not sure if you're registered), you can check your username here. You can then reset your password if you need to.                         |

这将允许您分别测试消息中的两个字符串,只需对元素执行 contains 即可。

除此之外,我会更改 html,以便每段文本都在其自己的容器中(跨度或 div 或段落或其他任何内容),因此可以单独找到每个文本,然后您可以直接匹配内容并摆脱<br>' s

于 2014-12-01T17:53:31.427 回答