Skip to content

Commit 8ba8deb

Browse files
authored
feat(screenshot): Add screenshot function to e2e test (button and checkbox) (#2532)
* Add screenshot function to e2e test (button and checkbox) * Add reporter for jasmine * Change directory to constant, and use variable to store name
1 parent 64f6d1b commit 8ba8deb

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

e2e/components/button/button.e2e.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import {browser, by, element} from 'protractor';
2+
import {screenshot} from '../../screenshot';
3+
24

35
describe('button', function () {
46
describe('disabling behavior', function () {
57
beforeEach(function() {
68
browser.get('/button');
79
});
10+
811
it('should prevent click handlers from executing when disabled', function () {
912
element(by.id('test-button')).click();
1013
expect(element(by.id('click-counter')).getText()).toEqual('1');
14+
screenshot('clicked once');
1115

1216
element(by.id('disable-toggle')).click();
1317
element(by.id('test-button')).click();
1418
expect(element(by.id('click-counter')).getText()).toEqual('1');
19+
screenshot('click disabled');
1520
});
1621
});
1722
});

e2e/components/checkbox/checkbox.e2e.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {browser, by, element, Key} from 'protractor';
2+
import {screenshot} from '../../screenshot';
23

34
describe('checkbox', function () {
45

@@ -12,28 +13,33 @@ describe('checkbox', function () {
1213
let checkboxEl = element(by.id('test-checkbox'));
1314
let inputEl = element(by.css('input[id=input-test-checkbox]'));
1415

16+
screenshot('start');
1517
checkboxEl.click();
1618
inputEl.getAttribute('checked').then((value: string) => {
1719
expect(value).toBeTruthy('Expect checkbox "checked" property to be true');
1820
});
21+
screenshot('checked');
1922

2023
checkboxEl.click();
2124
inputEl.getAttribute('checked').then((value: string) => {
2225
expect(value).toBeFalsy('Expect checkbox "checked" property to be false');
2326
});
27+
screenshot('unchecked');
2428
});
2529

2630
it('should toggle the checkbox when pressing space', () => {
2731
let inputEl = element(by.css('input[id=input-test-checkbox]'));
2832

2933
inputEl.getAttribute('checked').then((value: string) => {
3034
expect(value).toBeFalsy('Expect checkbox "checked" property to be false');
35+
screenshot('start');
3136
});
3237

3338
inputEl.sendKeys(Key.SPACE);
3439

3540
inputEl.getAttribute('checked').then((value: string) => {
3641
expect(value).toBeTruthy('Expect checkbox "checked" property to be true');
42+
screenshot('pressed space');
3743
});
3844
});
3945

e2e/screenshot.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as fs from 'fs';
2+
import * as gulp from 'gulp';
3+
import * as path from 'path';
4+
import {browser} from 'protractor';
5+
6+
const OUTPUT_DIR = '/tmp/angular-material2-build/screenshots/';
7+
8+
let currentJasmineSpecName = '';
9+
10+
/** Adds a custom jasmine reporter that simply keeps track of the current test name. */
11+
function initializeEnvironment(jasmine: any) {
12+
let reporter = new jasmine.JsApiReporter({});
13+
reporter.specStarted = function(result: any) {
14+
currentJasmineSpecName = result.fullName;
15+
};
16+
jasmine.getEnv().addReporter(reporter);
17+
}
18+
19+
initializeEnvironment(jasmine);
20+
21+
export class Screenshot {
22+
id: string;
23+
24+
/** The filename used to store the screenshot. */
25+
get filename(): string {
26+
return this.id
27+
.toLowerCase()
28+
.replace(/\s/g, '_')
29+
.replace(/[^/a-z0-9_]+/g, '')
30+
+ '.screenshot.png';
31+
}
32+
33+
/** The full path to the screenshot */
34+
get fullPath(): string {
35+
return path.resolve(OUTPUT_DIR, this.filename);
36+
}
37+
38+
constructor(id: string) {
39+
this.id = `${currentJasmineSpecName} ${id}`;
40+
browser.takeScreenshot().then(png => this.storeScreenshot(png));
41+
}
42+
43+
/** Replaces the existing screenshot with the newly generated one. */
44+
storeScreenshot(png: any) {
45+
if (!fs.existsSync(OUTPUT_DIR)) {
46+
fs.mkdirSync(OUTPUT_DIR, '744');
47+
}
48+
49+
if (fs.existsSync(OUTPUT_DIR)) {
50+
fs.writeFileSync(this.fullPath, png, {encoding: 'base64' });
51+
}
52+
}
53+
}
54+
55+
export function screenshot(id: string) {
56+
return new Screenshot(id);
57+
}

0 commit comments

Comments
 (0)