How to deal with data tables in Cucumber and Protractor

 

So, today I will give a code snippet about how to deal with Cucumber data tables and protractor.

Following the example:

 

  • Scenario with data table:
 Scenario: Register multiple users from different countries
   Given I want to register multiple users
    | user  | country  |
    | nicko | uk       |
    | pitty | brazil   |
    | slash | us       |
   When I send the form
   Then all the users should be registered

 

Remember to create the data table with the headers. Don’t forget that data tables are different from examples, here the first step will create a hash table with the data from the table and send it as parameter. Examples are used to run the same scenario with different variables in each run.

 

  • Related Step Definitions:
'use strict';

var protractor = require('protractor');
var browser = protractor.browser;
var _ = require('lodash');
var Q = require('q');

var RegisterPageSteps = function() {

 this.Given(/^I want to register multiple users$/, function(data) {
    var promises = [];
    var rows = data.hashes();
    //For each row you will get the user and the country
    _.each(rows, function(row) {
       var user = row.user;
       var country = row.country;

       //Here you can add the promises to perform sequentially
       //you can call the promises passing the user and the 
       //country as parameters
       promises.push((addUserCountry(user, country));
    });

    //You can also have promises to be performed that don't need
    //the parameters from the data table
    promises.push(navigateToSubmit());

    //Here you return all the promises
    return Q.all(promises);
  });

 this.When(/^I send the form$/, function(callback) {
    callback();
 });

 this.Then(/^all the users should be registered$/, function(callback) {
    callback();
 });

};

module.exports = RegisterPageSteps;

 

I have not implemented other functions and steps as the aim here is to show how to deal with the data table in your scenario.

Thank you !