Protractor HTTP Mocking

0.10.0 · abandoned · verified Wed Apr 22

Protractor HTTP Mock provides a NodeJS module designed to facilitate mocking HTTP calls within Protractor end-to-end tests, specifically for AngularJS applications. Its core function is to allow developers to isolate UI and client-side application code by intercepting and responding to network requests with predefined data, thus removing dependencies on external APIs during test execution. A key differentiator is its independence from Angular Mocks (ngMockE2E), meaning it does not require any modifications to the AngularJS application under test. The current stable version, 0.10.0, was released in 2017. Due to its tight coupling with Protractor, which was officially deprecated in 2022, `protractor-http-mock` is effectively an abandoned package with no ongoing development or maintenance.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates configuring `protractor-http-mock` in a Protractor configuration file, defining an inline HTTP mock, and loading a mock from an external file within a test spec. It also shows the essential `setup` and `teardown` calls in `beforeEach` and `afterEach` hooks.

const mock = require('protractor-http-mock');
const path = require('path');

exports.config = {
  directConnect: true,
  capabilities: {
    'browserName': 'chrome'
  },
  framework: 'jasmine',
  specs: [path.resolve(__dirname, 'mock.spec.js')],
  onPrepare: function() {
    require('protractor-http-mock').config = {
      rootDirectory: __dirname,
      protractorConfig: 'protractor.conf.js', // Or the actual name of this config file
      mocks: {
        default: ['mock-login'],
        dir: 'mocks'
      }
    };
  }
};

// mock.spec.js (in the same directory, or specified in specs)

describe('Protractor HTTP Mock Example', function() {
  beforeEach(function() {
    mock.setup(); // Ensure mocks are cleared and ready for each test
  });

  afterEach(function() {
    mock.teardown();
  });

  it('should mock a GET request for user data', function() {
    mock([
      {
        request: {
          path: '/api/users/1',
          method: 'GET'
        },
        response: {
          data: {
            userName: 'Mocked User',
            email: 'mock@example.com'
          },
          status: 200
        }
      }
    ]);

    // Assume your app navigates to a page that makes this request
    browser.get('http://localhost:8000/app/#/users/1'); 

    // Example: Verify UI reflects mocked data (replace with actual selectors)
    element(by.id('username-display')).getText().then(function(text) {
      expect(text).toEqual('Mocked User');
    });
    element(by.id('email-display')).getText().then(function(text) {
      expect(text).toEqual('mock@example.com');
    });
  });

  it('should load mocks from a file', function() {
    // Create a file: mocks/products.js
    // module.exports = [{ request: { path: '/api/products', method: 'GET' }, response: { data: [{id: 1, name: 'Mock Product'}] } }];
    mock(['products']); // Loads from mocks/products.js relative to rootDirectory
    browser.get('http://localhost:8000/app/#/products');
    // Assert that products are loaded from mock
  });
});

view raw JSON →