【问题标题】:Is it possible to use variables in beforeFeature Hook as part of the Specflow scenario statement?是否可以在 beforeFeature Hook 中使用变量作为 Specflow 场景语句的一部分?
【发布时间】:2022-02-23 18:11:31
【问题描述】:

我对 Specflow 还很陌生,我正在尝试使用 Specflow BeforeFeature Hook 看看这是否可行。我有一些仅用于特定功能的动态测试数据设置。我想将这些测试数据值用作场景测试的一部分。这是功能文件的场景测试之一:

@logintest
Feature: User login
Scenario: User login with different product access
Given I am in <productType> login page
When I login as <user>
I should see <productType> logo in my account

在 BeforeFeature() 中,我们有生成的函数: -具有产品访问类型属性的随机用户帐户 - 产品登录页面的随机网址。 (例如:productA234234.dev.local)

[Binding]
public class LoginHooks
{
    [BeforeFeature("logintest")]
    public static void BeforeFeature()
    {
        UserAccount testUser = new UserAccount(productType1);
        var product1Url = CreateProductUrl(productType1);
        FeatureContext.Current.Set("testUser", testUser);
        FeatureContext.Current.Set("productUrl", productUrl);
        ...
    }

因为每次运行测试时都会随机生成用户名,所以我无法将用户名作为数据表等的一部分提供给 Gherkins。有没有办法构建 Gherkins 以从 FeatureContext 中获取值?

[Binding]
public class UserLoginSteps
{
   //var productUrl = ???
   [Given(@"I am in (.*) login page")]
   public void GivenIAmInLoginPage(string productUrl)
   { . . .}
}

问题:

  1. 是否可以获取 productUrl 的 FeatureContext 值并在步骤中设置?
  2. 我认为在这种情况下给定的正则表达式是不正确的。是否有另一种方法来构造它以将变量作为数据输入的一部分?

谢谢!

【问题讨论】:

    标签: c# hook specflow


    【解决方案1】:

    使用 BDD 时使用随机数据是一种不好的方法 AFAK 所以我认为您需要将您的功能更改为这样的内容

      @alogintest
    Feature: SpecFlowFeature1
        try many user logins 
    
    Scenario Outline:  User login with different product access
    Given I am in <productType> login page
    And  the user is <user>
    When I login 
    Then I should see <prouctResult> logo in my account
    Examples:  
    | productType | userAccount | userPwd | productTypeResult |
    | P1          | u1          | pwd1    | p1                |
    | P2          | u2          | pwd2    | p2                |
    

    is it possible to get FeatureContext values for the productUrl and set in the step? 是的,您可以为每个特定的productTypeUrl 添加一列,例如

    注意 that you can add as many as you want scenarios

    I think the regex Given is incorrect in such case. Is there another way to construct it to take a variable as part of the data input?

    是的,因为占位符仅用于Scenario Outlines

    这里生成了你的步骤

    using TechTalk.SpecFlow;
    
    namespace loginTest
    {
        [Binding]
        public class SpecFlowFeature1Steps
        {
            [Given(@"I am in P(.*) login page")]
            public void GivenIAmInPLoginPage(int p0)
            {
                ScenarioContext.Current.Pending();
            }
    
            [Given(@"the user is (.*)")]
            public void GivenTheUserIs(string p0)
            {
                ScenarioContext.Current.Pending();
            }
    
            [When(@"I login")]
            public void WhenILogin()
            {
                ScenarioContext.Current.Pending();
            }
    
            [Then(@"I should see (.*) logo in my account")]
            public void ThenIShouldSeeLogoInMyAccount(string p0)
            {
                ScenarioContext.Current.Pending();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-13
      • 2021-06-27
      • 2018-11-11
      • 2020-08-03
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      相关资源
      最近更新 更多