Friday, August 18Be an Automation Engineer

Protractor with jasmine data provider – Write once test many times

What is data provider?

When it comes to automation, data provider is an approach to pass test data to the automation tests. Each and every automation tool has it’s own way of providing test data to the automation
When we take TestNG framework, it has @DataProvider annotation which will help to pass test data to the automation test.

Why we need a data provider?

You may already have doubt that why we need a data provider while we can retrieve the test data whenever we need it in the test spec.

Some times we may need to execute same spec by taking different test inputs each time. Lets say we have to verify login functionality of an applications with different login details. In this case the spec will be same but the test data will vary. Such type of scenarios can make use of data provider. The data provider will pass different tests data to the same spec and execute the spec for each test input passed.

Please don’t think that we can put the spec in a for/while loop and iterate it. It’s will be a bad approach.

There is one more advantage if we use data provider, the spec results for each test data input will be shown separately which can help testers for identifying the specific test data for which the test got failed/passed.

Which data provider we need to use for protractor?

The data providers are specific to the unit test frameworks that we use.

  1. For Jasmine framework – jasmine-data-provider npm packagejasmine-data-provider
  2. For Mocha framework – data-driven and leche npm packages

Here i am explaining about jasmine-data-provider with examples. I will be covering Mocha data providers in a separate article.

Without any delay let’s see an example program.
Note: Assuming that you have installed protractor in your machine. Click here to install and run a sample program otherwise.

1. Install jasmine-data-provider:

Run the below command in terminal/command prompt to install jasmine-data-provider

cd <projectLocation>
npm install jasmine-data-provider
2. Write your data in a separate file:

'use strict';
module.exports = {
    webdriverjsInfo: {
        'webdriverjs': { tagline: 'Js implementation of selenium - Official' },
        'protracor': { tagline: 'end-to-end test framework for Angular and AngularJS applications' },
        'webdriverio': { tagline: 'Selenium 2.0 bindings for NodeJS' },
        'nightwatch.js': { tagline: 'Write End-to-End tests in Node.js quickly and effortlessly that run against a Selenium/WebDriver server.' }
    }
}

3. Write your spec file:

var seleniumJsFlavours = require('./TestData.js');
var using = require('jasmine-data-provider');

describe('Insert selenium javascript library\'s tagline', function () {
    beforeEach(function () {
        browser.get('http://angularjs.org');
    });
    using(seleniumJsFlavours.webdriverjsInfo, function (data, description) {
        it("Insert description of " + description, function () {
            element(by.model('todoList.todoText')).sendKeys(data.tagline);
            // Clicks on 'Add' button
            element(by.css('[value="add"]')).click();
            // Getting all Todo lists displayed
            var todoList = element.all(by.repeater('todo in todoList.todos'));
            // Asserting the TODO's count as 3
            expect(todoList.count()).toEqual(3);
            //Verifying newly entered TODO is added
            expect(todoList.get(2).getText()).toEqual(data.tagline);
        });
    });
});

How the data is passed to spec?
1. Import test data: We have imported TestData file in the first line.
2. using block: We are passing test data to protractor spec by using block. Using block will iterate test for 4 times as we have 4 inputs in TestData.js file.

3. Protractor Config file:

Your config file will look like below.

exports.config = {
    capabilities: {
        'directConnect':true,
        'browserName': 'chrome'
    },
    framework: 'jasmine',
    specs: ['./DataProvider_Spec.js'],
    jasmineNodeOpts: {
        defaultTimeoutInterval: 999999
    },
    onPrepare: function () {
        browser.manage().window().maximize();
        browser.manage().timeouts().implicitlyWait(5000);
        browser.manage().timeouts().setScriptTimeout(60000);
    }
};

4. How to run?

Run the below command to execute your spec.

protractor config.js

Your test will run 4 times by taking different test data each time. Output in the terminal will look like below.

Additional information:

You can also pass an array of objects as test data. Each object will be consumed as test data for each single iteration.

Your test data file will look like below.

'use strict';
module.exports = {
    webdriverjsInfo: [
        { tagline: 'Js implementation of selenium - Official' },
        { tagline: 'end-to-end test framework for Angular and AngularJS applications' },
        { tagline: 'Selenium 2.0 bindings for NodeJS' },
        { tagline: 'Write End-to-End tests in Node.js quickly and effortlessly that run against a Selenium/WebDriver server.' }
    ]
}

Your script file will be

var seleniumJsFlavours = require('./TestData.js');
var using = require('jasmine-data-provider');

describe('Insert selenium javascript library\'s tagline', function () {
    beforeEach(function () {
        browser.get('http://angularjs.org');
    });
    using(seleniumJsFlavours.webdriverjsInfo, function (data) {
        it("Insert taglines of automation tools", function () {
            element(by.model('todoList.todoText')).sendKeys(data.tagline);
            // Clicks on 'Add' button
            element(by.css('[value="add"]')).click();
            // Getting all Todo lists displayed
            var todoList = element.all(by.repeater('todo in todoList.todos'));
            // Asserting the TODO's count as 3
            expect(todoList.count()).toEqual(3);
            //Verifying newly entered TODO is added
            expect(todoList.get(2).getText()).toEqual(data.tagline);
        });
    });
});

Happy Testing!!!

%d bloggers like this: