参数化步骤

在编写 Gherkin 时,有时你可能需要为实施测试计划的工程师参数化可重用性的步骤。步骤通过正则表达式捕获组接收参数。 ( 工程注意: 如果正则表达式中的每个捕获组都没有匹配的参数,则可能会出现“CucumberException:Arity mismatch”抛出)在下面的示例中,我们决定用双引号括起参数,以及 as 接受整数作为参数。

 Feature: Product Login
    As a user, I would like to be able to use my credentials to successfully 
    login. 
    
    Rules:
    - The user must have a valid username
    - The user must have a valid password
    - The user must have an active subscription 
    - User is locked out after 3 invalid attempts
    - User will get a generic error message following 
      login attempt with invalid credentials 

    Scenario: The user successfully logs in with valid credentials 
        This scenario tests that a user is able to successfully login
        provided they enter a valid username, valid password, and 
        currently have an active subscription on their account. 

        Given the user is on the login page
        When the user signs in with "valid" credentials
        Then the user should be logged in

    Scenario: The user attempts to log in with invalid credentials 
        This scenario tests that a user is not able to log in when
        they enter invalid credentials

        Given the user is on the login page
        When the user signs in with "invalid" credentials
        Then the user should be logged in

    Scenario: The user is locked out after too many failed attempts
        This scenario validates that the user is locked out
        of their account after failing three consecutive 
        attempts to log in

        Given the user is on the login page
        When the user fails to log in 3 times
        Then the user should be locked out of their account