javascriptcucumberbddcucumberjswdio-v6

How I can pull data from a Cucumber table in Javascript?


I want to perform a test case where I'm trying to login with different credentials and check the error message. How this can be done in Cucumber?

Feature: Login

Login Test Suite

Background: 
  Given I'm on the login page

Scenario: 01. Should not be able to login with invalid cred
When I log in with "username" and "password"
    |  username   | password | ExpectedError                     |
    |    asdasd   | anything | Invalid credentials specified     |
    |             | anything | Please specify a username         |
    |    asdasd   |          | Please specify a password         |
    |             |          | No username or password specified |
Then An error msg should appear

Here's where I want to pass the two args, username and password:

When('I log in with (string) and (string)', (username,password) => {
    p.loginWith(username, password)
})

Solution

  • It looks like you want a Scenario Outline. You will need to rephrase each step, and the data table will get moved to an "Examples" table:

    Feature: Login
      Login Test Suite
    
    Background: 
      Given I'm on the login page
    
    Scenario Outline: 01. Should not be able to login with invalid cred
      When I log in with "<username>" and "<password>"
      Then the "<ExpectedError>" error msg should appear
    
    Examples:
      | username | password | ExpectedError                     |
      | asdasd   | anything | Invalid credentials specified     |
      |          | anything | Please specify a username         |
      | asdasd   |          | Please specify a password         |
      |          |          | No username or password specified |
    

    The scenario will get executed once for each row in the Examples table. The <...> tokens in the steps allow you to reference a value in one of the example table columns.

    Your Then step needs to be rephrased to pass the expected validation error. It's step definition is pretty simple, and I'll leave the implementation up to you. Here is the stub:

    Then('the (string) error msg should appear', (expectedError) => {
      // TODO: Make assertion
    });