【问题标题】:Extending a Cucumber step扩展 Cucumber 步骤
【发布时间】:2019-08-11 04:49:36
【问题描述】:

我有一个 Cucumber 步骤,如下所示:

When I enter the credentials for the user

还有一个说

When I enter the correct credentials for the user

对应的步骤定义为:

@When("I enter the ([^\"]*) for the user$")
public void stepDefinition(String cred){
    //code
}
@When("I enter the correct ([^\"]*) for the user$")
public void otherStepDefinition(String cred){
    //other code
}

但是我的第二个黄瓜步骤(“我为用户输入正确的凭据”)与第一步的定义相匹配,只是在凭据中添加了“正确”一词。

  1. 我该如何解决这个问题?
  2. 我是正则表达式的新手。是否可以从“何时”步骤中排除“正确”部分,以便我可以有一个可以用“正确”部分“扩展”的基本步骤?

【问题讨论】:

  • 尝试@When("I enter the (\\S*) for the user$")作为第一条规则
  • 更改订单。
  • @WiktorStribiżew 这行得通!你能解释一下为什么吗?并将其作为答案发布,以便我接受。
  • 添加了更多选项的答案。

标签: regex automated-tests cucumber bdd


【解决方案1】:

请执行以下步骤定义,如果对您有用,请告诉我们。

@When("^I enter the ([^\"]*) for the user$")
public void stepDefinition(String cred){
    //code
}
@When("^I enter the correct ([^\"]*) for the user$")
public void otherStepDefinition(String cred){
    //other code
}

无参数

带参数

两个元字符 (^, $) 被称为锚点,因为它们用于绑定每个 正则表达式的结尾到他们所指定的字符串的开头和结尾 匹配上。

【讨论】:

  • 添加的元字符同理。它们是如何工作的?难道问题是它们之间的字符串的开头和结尾是相同的?
  • 它对我有用。请参阅答案描述中添加的屏幕截图。
  • 您没有在步骤定义模式中捕获凭据参数。
  • 添加参数后也可以工作。请检查
  • 在这两种情况下都将捕获文本“凭据”。凭据不会被明确提及。
【解决方案2】:

第一条规则应该改为

@When("I enter the (\\S+) for the user$")

这里,\S+ 匹配 1 个或多个非空白字符。如果没有非空白字符,请使用\S*

为了匹配两个你可以使用的“单词”

@When("I enter the (\\S+\\s+\\S+) for the user$")

请注意,您可以使用量词来控制“单词”的数量,例如这将匹配 2 或 3 个单词:

@When("I enter the (\\S+(?:\\s+\\S+){1,2}) for the user$")

要匹配 2 个或更多单词:

@When("I enter the (\\S+(?:\\s+\\S+){1,}) for the user$")
@When("I enter the (\\S+(?:\\s+\\S+)+) for the user$")

【讨论】:

    【解决方案3】:

    有几种方法可以改进这些步骤并避免使用正则表达式。

    1) 让用户知道其凭据并让步骤要求用户提供凭据

    所以你会有

    
    Given I am a user
      @user = create_user # method creates a user with credentials
    end
    
    When `I enter the users credentials` do
      fill_in username: @user.username
      fill_in password: @user.password
    end
    
    When `I enter the wrong credentials for the user` do
      fill_in username: @user.username
      fill_in password: @user.bad_password # or perhaps just bad_password
    end
    

    这种方法消除了 cucumber 的所有复杂性,并将其放在您为创建用户而调用的辅助方法中。

    2) 为您的步骤定义提供更多参数

    When 'I enter the credentials user: (\\S+) password: (\\S+) do |username, password|
      fill_in username: username
      fill_in password: password 
    end
    
    When 'I enter the bad credentials user: (\\S+) password: (\\S+) do |username, password|
      fill_in username: username
      fill_in password: password 
    end
    
    

    我非常喜欢第一种方法,您应该保持功能和场景超级简单,并将复杂性降低到代码中。代码比 Cucumber 更擅长处理复杂性。

    在 Cucumber 被命名之前,我一直在 cuking,现在我在 cuke 时从不使用正则表达式或场景大纲。你也不需要。

    【讨论】:

      【解决方案4】:

      几个答案提出了一种命令式方法,在 BDD 中被认为是一种反模式。相反,我强烈建议您使用自然或商业语言对 Gherkin 遵循声明性方法。如果您实际上是在测试登录功能,我会提出类似的建议:

      When an authorised user enters their credentials
      

      或基于角色的

      When an Administrator is authorised
      

      如果登录实际上是被测功能的先决条件,那么例如:

      Given an authorised user
      

      Given an authorised Administrator
      

      这些可以通过凭据管理器进行备份。

      ... = ExpectedData.credentialsFor("@authorised");
      

      标签应该代表特征,而不是预期数据的身份,从测试数据 db 或 csv 中检索,包含以下内容:

      @admin, administrator, password
      @authorised, user, password
      @unauthorised, user, wrong
      

      所有测试数据输入都应该使用相同的方法,例如:

      Given a Cash Customer
      Given a Credit Customer
      Given a Customer with an overdue account
      

      这种方法的一个重要好处是,通过使数据/凭据处理程序环境感知,测试套件可以很容易地在不同的环境中重用。

      【讨论】:

      • 重新。 “所有现有的答案都太迫切了”随着时间的流逝和添加更多答案,这将很快过时。也许是“一些……”或“避免命令式解决方案并做一些声明性的事情”……
      • @diabolist 单独改写了您建议的行,任何其他反馈都非常感谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多