Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Commit

Permalink
feat: add Browser.takeScreenshot action
Browse files Browse the repository at this point in the history
  • Loading branch information
clebert committed Apr 3, 2017
1 parent 934dd54 commit 9987426
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 35 deletions.
4 changes: 4 additions & 0 deletions config-schema.json
Expand Up @@ -61,6 +61,10 @@
"type": "number",
"minimum": 0,
"multipleOf": 1
},
"screenshotDirectory": {
"type": "string",
"minLength": 1
}
},
"additionalProperties": false
Expand Down
3 changes: 2 additions & 1 deletion fixtures/config.js
Expand Up @@ -5,5 +5,6 @@ module.exports = {
exclude: ['custom-config.js'],
include: '*.js',
retries: 3,
retryDelay: 1000
retryDelay: 1000,
screenshotDirectory: '/dev/null'
};
6 changes: 5 additions & 1 deletion package.json
Expand Up @@ -77,14 +77,18 @@
"watch:tests": "ava --fail-fast --watch"
},
"dependencies": {
"@types/fs-extra": "0.0.37",
"@types/mz": "0.0.30",
"@types/node": "7.0.12",
"@types/proxyquire": "1.3.27",
"ajv": "4.11.5",
"deep-strict-equal": "0.2.0",
"fs-promise": "2.0.2",
"glob": "7.1.1",
"selenium-webdriver": "3.3.0",
"tap": "10.3.1",
"tslib": "1.6.0"
"tslib": "1.6.0",
"uuid": "3.0.1"
},
"devDependencies": {
"ava": "0.18.2",
Expand Down
84 changes: 67 additions & 17 deletions src/__tests__/browser.test.ts
@@ -1,19 +1,42 @@
// tslint:disable no-any

import proxyquire = require('proxyquire');

import test from 'ava';
import {stub} from 'sinon';
import {Browser} from '../browser';
import {format} from '../description';
import {Deferred} from './utils';

const stubs = {
outputFile: stub(),
uuidV4: stub()
};

proxyquire('../browser', {
'fs-promise': {outputFile: stubs.outputFile}, 'uuid/v4': stubs.uuidV4
});

import {Browser} from '../browser';

function createTestName(method: string, result: string): string {
return `\`Browser.${method}\` should return an ${result}`;
}

let browser: Browser;

test.beforeEach(() => {
browser = new Browser('screenshotDirectory');

for (const key of Object.keys(stubs)) {
(stubs as any)[key].reset();
(stubs as any)[key].resetBehavior();
}
});

test(createTestName('pageTitle', 'accessor'), async t => {
t.plan(3);

const accessor = new Browser().pageTitle;
const accessor = browser.pageTitle;

t.is(format(accessor.description), 'page title');

Expand All @@ -27,7 +50,7 @@ test(createTestName('pageTitle', 'accessor'), async t => {
test(createTestName('pageUrl', 'accessor'), async t => {
t.plan(3);

const accessor = new Browser().pageUrl;
const accessor = browser.pageUrl;

t.is(format(accessor.description), 'page url');

Expand All @@ -41,7 +64,7 @@ test(createTestName('pageUrl', 'accessor'), async t => {
test(createTestName('windowX', 'accessor'), async t => {
t.plan(3);

const accessor = new Browser().windowX;
const accessor = browser.windowX;

t.is(format(accessor.description), 'window x-position');

Expand All @@ -57,7 +80,7 @@ test(createTestName('windowX', 'accessor'), async t => {
test(createTestName('windowY', 'accessor'), async t => {
t.plan(3);

const accessor = new Browser().windowY;
const accessor = browser.windowY;

t.is(format(accessor.description), 'window y-position');

Expand All @@ -73,7 +96,7 @@ test(createTestName('windowY', 'accessor'), async t => {
test(createTestName('windowWidth', 'accessor'), async t => {
t.plan(3);

const accessor = new Browser().windowWidth;
const accessor = browser.windowWidth;

t.is(format(accessor.description), 'window width');

Expand All @@ -89,7 +112,7 @@ test(createTestName('windowWidth', 'accessor'), async t => {
test(createTestName('windowHeight', 'accessor'), async t => {
t.plan(3);

const accessor = new Browser().windowHeight;
const accessor = browser.windowHeight;

t.is(format(accessor.description), 'window height');

Expand All @@ -106,7 +129,7 @@ test(createTestName('scriptResult', 'accessor'), async t => {
t.plan(4);

const script = () => undefined;
const accessor = new Browser().scriptResult('scriptName', script);
const accessor = browser.scriptResult('scriptName', script);

t.is(format(accessor.description), 'result of script \'scriptName\'');

Expand All @@ -122,7 +145,7 @@ test(createTestName('executeScript', 'action'), async t => {
t.plan(4);

const script = () => undefined;
const action = new Browser().executeScript('scriptName', script);
const action = browser.executeScript('scriptName', script);

t.is(format(action.description), 'execute script \'scriptName\'');

Expand All @@ -140,7 +163,7 @@ test(createTestName('executeScript', 'action'), async t => {
test(createTestName('loadPage', 'action'), async t => {
t.plan(4);

const action = new Browser().loadPage('pageUrl');
const action = browser.loadPage('pageUrl');

t.is(format(action.description), 'load page \'pageUrl\'');

Expand All @@ -158,7 +181,7 @@ test(createTestName('loadPage', 'action'), async t => {
test(createTestName('maximizeWindow', 'action'), async t => {
t.plan(3);

const action = new Browser().maximizeWindow();
const action = browser.maximizeWindow();

t.is(format(action.description), 'maximize window');

Expand All @@ -175,7 +198,7 @@ test(createTestName('maximizeWindow', 'action'), async t => {
test(createTestName('navigateBack', 'action'), async t => {
t.plan(3);

const action = new Browser().navigateBack();
const action = browser.navigateBack();

t.is(format(action.description), 'navigate back');

Expand All @@ -192,7 +215,7 @@ test(createTestName('navigateBack', 'action'), async t => {
test(createTestName('navigateForward', 'action'), async t => {
t.plan(3);

const action = new Browser().navigateForward();
const action = browser.navigateForward();

t.is(format(action.description), 'navigate forward');

Expand All @@ -209,7 +232,7 @@ test(createTestName('navigateForward', 'action'), async t => {
test(createTestName('reloadPage', 'action'), async t => {
t.plan(3);

const action = new Browser().reloadPage();
const action = browser.reloadPage();

t.is(format(action.description), 'reload page');

Expand All @@ -226,7 +249,7 @@ test(createTestName('reloadPage', 'action'), async t => {
test(createTestName('setWindowPosition', 'action'), async t => {
t.plan(5);

const action = new Browser().setWindowPosition(123, 456);
const action = browser.setWindowPosition(123, 456);

t.is(format(action.description), 'set window position to 123,456');

Expand All @@ -247,7 +270,7 @@ test(createTestName('setWindowPosition', 'action'), async t => {
test(createTestName('setWindowSize', 'action'), async t => {
t.plan(5);

const action = new Browser().setWindowSize(123, 456);
const action = browser.setWindowSize(123, 456);

t.is(format(action.description), 'set window size to 123x456');

Expand All @@ -266,7 +289,7 @@ test(createTestName('setWindowSize', 'action'), async t => {
test(createTestName('sleep', 'action'), async t => {
t.plan(2);

const action = new Browser().sleep(50);
const action = browser.sleep(50);

t.is(format(action.description), `sleep for ${50} ms`);

Expand All @@ -276,3 +299,30 @@ test(createTestName('sleep', 'action'), async t => {

t.true(Date.now() - startTime >= 49);
});

test(createTestName('takeScreenshot', 'action'), async t => {
t.plan(6);

stubs.uuidV4.returns('uuid');

const action = browser.takeScreenshot();

t.is(
format(action.description),
'take screenshot \'screenshotDirectory/uuid.png\''
);

const takeScreenshot = stub().resolves('screenshot');
const deferred = new Deferred();

stubs.outputFile.resolves(deferred);

await action.perform({takeScreenshot} as any);

t.true(deferred.done);

t.is(stubs.outputFile.callCount, 1);
t.is(stubs.outputFile.args[0][0], 'screenshotDirectory/uuid.png');
t.is(stubs.outputFile.args[0][1], 'screenshot');
t.deepEqual(stubs.outputFile.args[0][2], {encoding: 'base64'});
});
32 changes: 19 additions & 13 deletions src/__tests__/element.test.ts
Expand Up @@ -11,10 +11,16 @@ function createTestName(method: string, result: string): string {
return `\`Element.${method}\` should return an ${result}`;
}

let element: Element;

test.beforeEach(() => {
element = new Element('selector');
});

test(createTestName('tagName', 'accessor'), async t => {
t.plan(5);

const accessor = new Element('selector').tagName;
const accessor = element.tagName;

t.is(format(accessor.description), 'tag name of element \'selector\'');

Expand All @@ -32,7 +38,7 @@ test(createTestName('tagName', 'accessor'), async t => {
test(createTestName('text', 'accessor'), async t => {
t.plan(5);

const accessor = new Element('selector').text;
const accessor = element.text;

t.is(format(accessor.description), 'text of element \'selector\'');

Expand All @@ -50,7 +56,7 @@ test(createTestName('text', 'accessor'), async t => {
test(createTestName('visibility', 'accessor'), async t => {
t.plan(5);

const accessor = new Element('selector').visibility;
const accessor = element.visibility;

t.is(format(accessor.description), 'visibility of element \'selector\'');

Expand All @@ -68,7 +74,7 @@ test(createTestName('visibility', 'accessor'), async t => {
test(createTestName('x', 'accessor'), async t => {
t.plan(5);

const accessor = new Element('selector').x;
const accessor = element.x;

t.is(format(accessor.description), 'x-position of element \'selector\'');

Expand All @@ -86,7 +92,7 @@ test(createTestName('x', 'accessor'), async t => {
test(createTestName('y', 'accessor'), async t => {
t.plan(5);

const accessor = new Element('selector').y;
const accessor = element.y;

t.is(format(accessor.description), 'y-position of element \'selector\'');

Expand All @@ -104,7 +110,7 @@ test(createTestName('y', 'accessor'), async t => {
test(createTestName('width', 'accessor'), async t => {
t.plan(5);

const accessor = new Element('selector').width;
const accessor = element.width;

t.is(format(accessor.description), 'width of element \'selector\'');

Expand All @@ -122,7 +128,7 @@ test(createTestName('width', 'accessor'), async t => {
test(createTestName('height', 'accessor'), async t => {
t.plan(5);

const accessor = new Element('selector').height;
const accessor = element.height;

t.is(format(accessor.description), 'height of element \'selector\'');

Expand All @@ -140,7 +146,7 @@ test(createTestName('height', 'accessor'), async t => {
test(createTestName('cssValue', 'accessor'), async t => {
t.plan(6);

const accessor = new Element('selector').cssValue('cssName');
const accessor = element.cssValue('cssName');

t.is(
format(accessor.description),
Expand All @@ -162,7 +168,7 @@ test(createTestName('cssValue', 'accessor'), async t => {
test(createTestName('propertyValue', 'accessor'), async t => {
t.plan(6);

const accessor = new Element('selector').propertyValue('propertyName');
const accessor = element.propertyValue('propertyName');

t.is(
format(accessor.description),
Expand All @@ -184,7 +190,7 @@ test(createTestName('propertyValue', 'accessor'), async t => {
test(createTestName('clearValue', 'action'), async t => {
t.plan(5);

const action = new Element('selector').clearValue();
const action = element.clearValue();

t.is(format(action.description), 'clear value of element \'selector\'');

Expand All @@ -205,7 +211,7 @@ test(createTestName('clearValue', 'action'), async t => {
test(createTestName('click', 'action'), async t => {
t.plan(5);

const action = new Element('selector').click();
const action = element.click();

t.is(format(action.description), 'click on element \'selector\'');

Expand All @@ -226,7 +232,7 @@ test(createTestName('click', 'action'), async t => {
test(createTestName('sendKeys', 'action'), async t => {
t.plan(7);

const action = new Element('selector').sendKeys('key1', 'key2');
const action = element.sendKeys('key1', 'key2');

t.is(
format(action.description),
Expand All @@ -252,7 +258,7 @@ test(createTestName('sendKeys', 'action'), async t => {
test(createTestName('submitForm', 'action'), async t => {
t.plan(5);

const action = new Element('selector').submitForm();
const action = element.submitForm();

t.is(
format(action.description), 'submit form containing element \'selector\''
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/index.test.ts
Expand Up @@ -17,6 +17,7 @@ Config:
include: '*.js'
retries: 3
retryDelay: 1000
screenshotDirectory: '/dev/null'
`;

const defaultStderr = `
Expand All @@ -28,6 +29,7 @@ Config:
include: '**/*.e2e.js'
retries: 4
retryDelay: 500
screenshotDirectory: 'screenshots'
`;

let customResult: Result;
Expand Down

0 comments on commit 9987426

Please sign in to comment.