Skip to content
Permalink
Browse files
feat(protractor): Browser.log() allows the actor to read the browser …
…log entries
  • Loading branch information
jan-molak committed Apr 29, 2019
1 parent 5c0290a commit 2a088b7
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
@@ -25,6 +25,10 @@ exports.config = {
capabilities: {
browserName: 'chrome',

loggingPrefs: {
browser: 'INFO',
},

chromeOptions: {
args: [
'--disable-infobars',
@@ -0,0 +1,26 @@
import { stage } from '@integration/testing-tools';
import { containAtLeastOneItemThat, Ensure, includes, property } from '@serenity-js/assertions';

import { Browser, Navigate } from '../../../src';
import { pageFromTemplate } from '../../fixtures';
import { UIActors } from '../../UIActors';

describe('Browser', () => {

const Bernie = stage(new UIActors()).actor('Bernie');

/** @test {Browser.log} */
it('allows the actor to read the browser log entries', () => Bernie.attemptsTo(
Navigate.to(pageFromTemplate(`
<html lang="en">
<body>
<script>
console.log('Hello from the console!');
</script>
</body>
</html>
`)),

Ensure.that(Browser.log(), containAtLeastOneItemThat(property('message', includes('Hello from the console!')))),
));
});
@@ -0,0 +1,54 @@
import { Question } from '@serenity-js/core';
import { logging } from 'selenium-webdriver';
import { promiseOf } from '../../promiseOf';
import { BrowseTheWeb } from '../abilities';

export class Browser {

/**
* @desc
* Creates a {@link Question} that reads the entries of the browser log
* so that they can be asserted on.
*
* Please note that in order to ensure that the automated test has access to the browser log,
* Protractor needs to be configured with the desired logging level, as per the example below.
*
* @example <caption>Enabling Protractor browser logging</caption>
* // protractor.conf.js
* exports.config = {
* capabilities: {
* loggingPrefs: {
* // available options: OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, ALL
* browser: 'SEVERE'
* },
* }
* }
*
* @example <caption>Ensure the app didn't log anything to the console during the test</caption>
* import { Actor, BrowseTheWeb, Browser } from '@serenity-js/core';
* import { Ensure, property, equals } from '@serenity-js/assertions';
*
* actor.attemptsTo(
* Ensure.that(Browser.log(), property('length', equals(0))),
* );
*
* @example <caption>Mark the test as "compromised" if the server responds with a 500 to any AJAX request during the test</caption>
* import { Actor, BrowseTheWeb, Browser, TestCompromisedError } from '@serenity-js/core';
* import { Ensure, property, equals, not, contrainAtLeastOneItemThat } from '@serenity-js/assertions'
*
* actor.attemptsTo(
* Ensure.that(Browser.log(),
* not(contrainAtLeastOneItemThat(
* property('message', includes('the server responded with a status of 500'))
* ))
* ).otherwiseFailWith(TestCompromisedError, 'The server is down'),
* );
*
* @see https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities#loggingpreferences-json-object
* @returns {Question<Promise<logging.Entry[]>>}
*/
static log() {
return Question.about<Promise<logging.Entry[]>>(`browser log`, actor =>
promiseOf(BrowseTheWeb.as(actor).manage().logs().get('browser')));
}
}
@@ -1,4 +1,5 @@
export * from './Attribute';
export * from './Browser';
export * from './Cookie';
export * from './CSSClasses';
export { Pick } from './Pick';

0 comments on commit 2a088b7

Please sign in to comment.