2

I am learning how to cucumber.js, protractor, bdd etc. I can't find out how to use 'Scenario Outlines' in cucumber.js and protractor

  1. I have the following in my .feature file

Scenario Outline : Invalid Login
  When I enter invalid <user>
  And I enter invalid <pass>
  And I press login button
  Then I should see an error message

  Examples:
    |user |pass|
    |abc  |def|
    |bcd  |efg|
  1. I have the following code in my steps.js file

   
     this.When('I enter invalid username', function (callback) {
        var userNameElement = element(by.id('username'));
        userNameElement.sendKeys('userA');
        callback();
    });

    this.When('I enter invalid password', function (callback) {
        var passwordElement = element(by.id('password'));
        passwordElement.sendKeys('userB');
        callback();

    });
  1. How can I pass the 'example' data from the 'feature' file to my 'steps' file and run the test with the data table define in the 'feature' file?
user9033344
  • 21
  • 1
  • 3

2 Answers2

2

Scenario outline values are passed as parameter to step definitions. Your step definitions should be like this:

 this.When(/^I enter "([^"]*)" as invalid username$/, function (user, callback) {
    var userNameElement = element(by.id('username'));
    userNameElement.sendKeys(user);
    callback();
});

this.When(/^I enter "([^"]*)" as invalid password$/', function (pwd, callback) {
    var passwordElement = element(by.id('password'));
    passwordElement.sendKeys(pwd);
    callback();

});
sisbilir
  • 76
  • 5
  • Hello, I have the same issue but I have already done what you say and it gives an error: https://stackoverflow.com/questions/51028287/each-key-must-be-a-number-of-string-got-function – Tester Jun 25 '18 at 16:53
-1

the correct syntax for using parameter tables with cucumberjs in standard javascript, using brackets, is the following :

Scenario Outline: Joe Given I enter "<goat>" as invalid username
Examples:  
| goat | 
| goe | 
| bill | 
| bish |

and in the js file :

const assert = require('assert');
const { Given, When, Then } = require('cucumber');


// https://docs.cucumber.io/gherkin/reference/#scenario-outline
// https://stackoverflow.com/questions/47571808/cucumber-js-protractor-javascript-example-with-scenario-outlines
When(/^I enter "(.*)" as invalid username$/, function ( goat ) {
  console.log("display Parameter passed : " + goat );
});

-------results ----

./node_modules/.bin/cucumber-js
display Parameter passed : goe
.display Parameter passed : bill
.display Parameter passed : bish
.display Parameter passed : hey
.display Parameter passed : joe
.

6 scenarios (6 passed)
5 steps (5 passed)
0m00.015s

and you can also use it without brackets.

superluminary
  • 47,086
  • 25
  • 151
  • 148