Giter Club home page Giter Club logo

protractor-browser-logs's Introduction

Protractor browser logs assertion library

It allows asserting the browser console logs in each test for warnings and errors. You can set the ignored messages, specify the expectations using strings, regex or custom matchers.

Inspired by https://github.com/angular/protractor-console-plugin/

Installation

npm install protractor-browser-logs --save-dev

Typical library usage

var browserLogs = require('protractor-browser-logs');

describe('Home page:', function () {

  var logs;

  beforeEach(function () {
    logs = browserLogs(browser);
    logs.ignore(logs.DEBUG);
    logs.ignore(logs.INFO);
  });

  afterEach(function () {
    return logs.verify();
  });

  it('should log an error after clicking a button', function () {
    logs.expect(/server request failed/);

    browser.get('...');
    element(by.id('button')).click();
  });

});

API

You can ignore any message whenever it appears.

logs.ignore('text');
logs.ignore(/text/i);
logs.ignore(function (message) {
  return message.message.toLowerCase().indexOf('text') !== -1;
});

// You can combine them all together
logs.ignore('hello', 'world'); // ignores messages containing `hello` and `world`
logs.ignore(/hello/i, function (message) { // ignore all messages containting `hello` but not `world`
  return message.message.indexOf('world') === -1;
});

You can also expect some messages. The order does matter.

logs.expect('one');
logs.expect(/two/i);
logs.expect(function (message) {
  return message.message.toLowerCase().indexOf('three') === 0;
});

You can check the expectations using verify method which returns a promise:

it('this is my test', function () {
  logs.expect('one');
  // ...
  return logs.verify();
});

Using advanced features

var browserLogs = require('protractor-browser-logs');

describe('Home page:', function () {

  var log = browserLogs(browser);

  beforeEach(function () {
    // Use only one instance, but need to reset before each test.
    log.reset();

    // Combine matcher functions
    logs.ignore(logs.or(logs.DEBUG, logs.INFO));

    // Specify custom matcher function
    logs.ignore(function (message) {
      return message.message.indexOf('Oops') !== -1;
    });
  });

  it('should log an error after clicking a button', function () {
    // The sequence of expectations does matter
    logs.expect(/retrying/, logs.WARN); // Expect message having "retrying" text and WARNING level.
    logs.expect(/server request failed/, logs.ERROR);

    browser.get('...');
    element(by.id('button')).click();
  });

  afterEach(function () {
    return logs.verify();
  });

});

Sharing the code inside a protractor configuration file

onPrepare = function () {

  var browserLogs = require('protractor-browser-logs'),
      logs = browserLogs(browser);

  if (global.logs) {
    throw new Error('Oops, name is already reserved!');
  }
  global.logs = logs;

  beforeEach(function () {
    logs.reset();

    // You can put here all expected generic expectations.
    logs.ignore('cast_sender.js');
    logs.ignore('favicon.ico');

    logs.ignore(logs.or(logs.INFO, logs.DEBUG));
  });

  afterEach(function () {
    return logs.verify();
  });
};

Protractor capabilities configuration

By default browser allows recording only WARNING and SEVERE level messages. In order to be able asserting any level, You need to change the loggingPrefs.browser capabilities.

capabilities: {
  loggingPrefs: {
    browser: 'ALL' // "OFF", "SEVERE", "WARNING", "INFO", "CONFIG", "FINE", "FINER", "FINEST", "ALL".
  }
}

More details could be found here: https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities#loggingpreferences-json-object

Custom reporters

It's also possible to specify custom reporters, for filtering and printing out the log entries.

function simple(entries) {
  entries.forEach(function (entry) {
    console.log([entry.level.name, entry.message].join(': '));
  });
}

function colored(entries) {
  var colors = { INFO: 35 /* magenta */, WARNING: 33 /* yellow */, SEVERE: 31 /* red */};
  entries.forEach(function (entry) {
    console.log('\u001b[' + (colors[entry.level.name] || 37) + 'm' + [entry.level.name, entry.message].join(': ') + '\u001b[39m');
  });
}

var browserLogs = require('protractor-browser-logs');
browserLogs(browser, {
  reporters: [simple, colored]
});

protractor-browser-logs's People

Contributors

mastertheblaster avatar shahata avatar

Watchers

Anna Tsibulskaya avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.