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 019059e commit 89eeb88
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,4 +4,5 @@ coverage
dist
node_modules
npm-debug.log*
screenshots
todo.tasks
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -19,6 +19,8 @@ test('Star the "clebert/cybernaut" repository on GitHub', async t => {

await t.assert(browser.pageTitle, it.should.contain('clebert/cybernaut'));

await t.perform(browser.takeScreenshot());

const starButton = defineElement('ul.pagehead-actions > li:nth-child(2) > a:nth-child(1)');

await t.perform(starButton.click());
Expand Down
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
Binary file modified example/screenshot.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions example/test.e2e.js
Expand Up @@ -5,6 +5,8 @@ test('Star the "clebert/cybernaut" repository on GitHub', async t => {

await t.assert(browser.pageTitle, it.should.contain('clebert/cybernaut'));

await t.perform(browser.takeScreenshot());

const starButton = defineElement('ul.pagehead-actions > li:nth-child(2) > a:nth-child(1)');

await t.perform(starButton.click());
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
89 changes: 65 additions & 24 deletions src/__tests__/browser.test.ts
Expand Up @@ -7,28 +7,42 @@ import {stub} from 'sinon';
import {format} from '../description';
import {Deferred} from './deferred';

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

proxyquire.noPreserveCache();
proxyquire.preserveCache();

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

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

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

test.beforeEach(t => {
sleep.reset();
sleep.resetBehavior();
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 @@ -42,7 +56,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 @@ -56,7 +70,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 @@ -72,7 +86,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 @@ -88,7 +102,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 @@ -104,7 +118,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 @@ -121,7 +135,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 @@ -137,7 +151,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 @@ -155,7 +169,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 @@ -173,7 +187,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 @@ -190,7 +204,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 @@ -207,7 +221,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 @@ -224,7 +238,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 @@ -241,7 +255,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 @@ -262,7 +276,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 @@ -281,18 +295,45 @@ test(createTestName('setWindowSize', 'action'), async t => {
test(createTestName('sleep', 'action'), async t => {
t.plan(4);

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

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

const deferred = new Deferred();

sleep.resolves(deferred);
stubs.sleep.resolves(deferred);

await action.perform({} as any);

t.true(deferred.done);

t.is(sleep.callCount, 1);
t.is(sleep.args[0][0], 50);
t.is(stubs.sleep.callCount, 1);
t.is(stubs.sleep.args[0][0], 50);
});

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'});
});

0 comments on commit 89eeb88

Please sign in to comment.