1

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)
})
Lunivore
  • 17,277
  • 4
  • 47
  • 92

1 Answers1

1

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
});
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
  • 1
    how I will be passing the two args in the function ins that case should it be like that When(/^I log in with '(.*)' and '(.*)'$/, (username,password) => { p.fillUserName(username,password) return this; }) –  Apr 03 '21 at 16:39
  • @amrkamel: yes, your individual steps will not be dealing with data tables. Your individual steps will only deal with strings. – Greg Burghardt Apr 03 '21 at 19:47
  • great thanks man, I would like to ask you something else if you don't mind I there a way to add-hook (beforeAll) to be run once and before all specific set of scenario let's say I have 4 scenarios 2 of them needs a specific method to be run once before these specific two, I have tried to use before hook with tags but I find it's equal to beforeEach, which means its not running only once –  Apr 04 '21 at 15:29
  • @amrkamel Sounds like you have another question! Post it as a new question and you'll get more answers. – Lunivore Apr 12 '21 at 07:33