Thursday, September 21Be an Automation Engineer

Using rest api in protractor tests

While automating an UI application sometimes we may need to use rest api for creating test data required for the current test.

Protractor is designed for automating application on the browser. Protractor by default can not make rest calls on it’s own. So we need to depend on third party tools like restler for calling rest api in our tests. Restler is also asynchronous. We need to add reslter calls to protractor promise queue to make protractor wait for restler to complete it’s operation.

Let’s see an example.

1. Install ‘restler’:

Run the below command in your project folder.

npm install restler

2. Use Restler to make a call to a rest api:

Create a folder in your project (say: restservice) and create the below js file which calls a yahoo’s weather rest api.

var rest = require('restler');
var defered = protractor.promise.defer();
var YahooWeather = function () {
    this.getCityWeather = function () {
        rest.get('*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22Hyderabad%2C%20Tel%2C%20india%22)&format=json&').on('complete', function (result) {
            if (result instanceof Error) {
                //console.log('Error:', result.message);
                //this.retry(5000); // try again after 5 sec 
            } else {
        return defered.promise;
module.exports = YahooWeather;

We have used protractor.promise.defer(); to get hold of protractor’s promise. We can resolve the promise by using ‘.fulfill(returnValue)’ for success case and ‘.reject(returnValue)’ for failure case. We have to return the promise at the end.

We need to call the getCityWeather function in our test. Let’s see how to do that.

3. Calling the above code in test script:

Create you spec file as below:

var YahooWeather = require('./../restservice/RestOperations.js');
describe('Using rest api', function () {
    it('Getting test data from rest service', function () {
        browser.get("").then(function () {
            var weather = new YahooWeather();
            //Gets weather
            weather.getCityWeather().then(function (result) {
                // Enter text under Name input field
                element(by.model('yourName')).sendKeys(result).then(function () {
                    //Getting welcome text
                    expect(element(by.binding('yourName')).getText()).toEqual('Hello India!');
        //To see the text typed in the TODO field. Not Mandatory

In first line we have imported the RestOperations.js file into our script.
var weather = new YahooWeather();: we have created object.
weather.getCityWeather(): Makes call to created function under ‘RestOperations.js’
.then: It will handle the returned promise.

4. Your config file would be:

exports.config = {
    seleniumAddress: 'http://localhost:4444/wd/hub',
    capabilities: {
        'browserName': 'chrome'
    framework: 'jasmine',
    specs: ['./specs/UsingRestApi.js'],
    jasmineNodeOpts: {
        defaultTimeoutInterval: 999999
    onPrepare: function () {

5. How to Run:

Make sure you have started webdriver-manager.

webdriver-manager start

Run protractor in a new terminal.

protractor config.js

  • Vinay

    Thanks for this..

  • Katrina Engelbrecht

    I am using Angular4 and typescript. How to write the codes above in typescript???

    • Vijay Daram

      Hi Katrina,

      If you are not aware of writing protractor tests using type script, please go through my article with examples below.

      For the current example, you need to copy the import statements in the given article and place it in first line in both RestOperations.ts and UsingRestApi.ts files. This should make it work for typescript.

      Happy testing!!!

%d bloggers like this: