Skip to content

krisboit/protractor-xmlhttprequest-mock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

protractor-xmlhttprequest-mock

Build Status

Ajax calls mocking plugin for protractor, will work with angular 2 also.

A simple example of usage:

import {browser, $} from 'protractor';
import {MockService} from 'protractor-xmlhttprequest-mock';

describe('the backend', () => {
  it('should reply with code 418', async () => {
    await MockService.setup(browser);
    await MockService.addMock('mock1', {
      path: '/api/teapot',
      response: {status: 418, data: "I'm a teapot"},
    });

    expect($('div').getText()).toEqual("I'm a teapot");
  });
});

Using regex:

import {browser, $} from 'protractor';
import {MockService} from 'protractor-xmlhttprequest-mock';

describe('the backend', () => {
  it('should reply with code 418', () => {
    MockService.setup(browser);
    MockService.addMock('mock1', {
      path: /^(\/api)\/teapot\/[1-9a-z]/,
      response: {status: 418, data: "I'm a teapot"},
    });

    $('button').click();
    expect($('div').getText()).toEqual("I'm a teapot");
  });
});

Method support:

import {browser, $} from 'protractor';
import {MockService} from 'protractor-xmlhttprequest-mock';

describe('the backend', () => {
  it('should reply with code 418', () => {
    MockService.setup(browser);
    MockService.addMock('mock1', {
      path: '/api/teapot',
      mothod: 'get',
      response: {status: 418, data: "I'm a teapot"},
    });
    MockService.addMock('mock2', {
      path: '/api/teapot',
      mothod: 'post',
      response: {status: 401, data: "unauthorized"},
    });

    $('button').click();
    expect($('div').getText()).toEqual("I'm a teapot");
  });
});

Multiple responses for a mock

import {browser, $} from 'protractor';
import {MockService} from 'protractor-xmlhttprequest-mock';

describe('the backend', () => {
  it('should reply with code 418', () => {
    MockService.setup(browser);
    MockService.addMock('mock1', {
      path: '/api/teapot',
      mothod: 'get',
      response: [
        {status: 418, numberOfRequests: 1, data: "I'm a teapot"},
        {status: 200, numberOfRequests: 2, data: "I'm not a teapot"},
        {status: 418, numberOfRequests: 1, data: "I'm a teapot again"}
        ],
    });
    MockService.addMock('mock2', {
      path: '/api/teapot',
      mothod: 'post',
      response: {status: 401, data: "unauthorized"},
    });

    $('button').click();
    expect($('div').getText()).toEqual("I'm a teapot");
  });
});

***Note numberOfRequests is mandatory.

Other examples can be found in the tests from tests folder.