Skip to content

Commit

Permalink
Add reporter for jasmine
Browse files Browse the repository at this point in the history
  • Loading branch information
tinayuangao committed Jan 6, 2017
1 parent b40999b commit a48c356
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 37 deletions.
9 changes: 4 additions & 5 deletions e2e/components/button/button.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import {browser, by, element} from 'protractor';
import {screenshot} from '../../screenshot';


describe('button', function () {
describe('disabling behavior', function () {
beforeEach(function() {
browser.get('/button');
});

it('should prevent click handlers from executing when disabled', function () {
let testname =
`button disabling behavior should prevent click handlers from executing when disabled`;
screenshot(testname);
element(by.id('test-button')).click();
expect(element(by.id('click-counter')).getText()).toEqual('1');
screenshot(testname + ' click once');
screenshot('clicked once');

element(by.id('disable-toggle')).click();
element(by.id('test-button')).click();
expect(element(by.id('click-counter')).getText()).toEqual('1');
screenshot(testname + 'click disabled');
screenshot('click disabled');
});
});
});
13 changes: 5 additions & 8 deletions e2e/components/checkbox/checkbox.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,36 @@ describe('checkbox', function () {
});

it('should be checked when clicked, and be unchecked when clicked again', () => {
let testName =
'checkbox should be checked when clicked, and be unchecked when clicked again';
let checkboxEl = element(by.id('test-checkbox'));
let inputEl = element(by.css('input[id=input-test-checkbox]'));

screenshot(testName);
screenshot('start');
checkboxEl.click();
inputEl.getAttribute('checked').then((value: string) => {
expect(value).toBeTruthy('Expect checkbox "checked" property to be true');
});
screenshot(testName + ' checked');
screenshot('checked');

checkboxEl.click();
inputEl.getAttribute('checked').then((value: string) => {
expect(value).toBeFalsy('Expect checkbox "checked" property to be false');
});
screenshot(testName + ' unchecked');
screenshot('unchecked');
});

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

inputEl.getAttribute('checked').then((value: string) => {
expect(value).toBeFalsy('Expect checkbox "checked" property to be false');
screenshot(testName);
screenshot('start');
});

inputEl.sendKeys(Key.SPACE);

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

Expand Down
50 changes: 26 additions & 24 deletions e2e/screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,52 @@ import * as gulp from 'gulp';
import * as path from 'path';
import {browser} from 'protractor';


function initializeEnvironment(jasmine: any) {
var reporter = new jasmine.JsApiReporter({});
reporter.specStarted = function(result: any) {
jasmine.getEnv().currentSpec = result.fullName;
};
jasmine.getEnv().addReporter(reporter);
}

initializeEnvironment(jasmine);

export class Screenshot {
id: string;
dir: string = '/tmp/angular-material2-build/screenshots/';

/**
* The filename used to store the screenshot
* @returns {string}
*/
get filename() {
/** The filename used to store the screenshot. */
get filename(): string {
return this.id
.toLowerCase()
.replace(/[ :\/]/g, '_')
.replace(/\s/g, '_')
.replace(/[^/a-z0-9_]+/g, '')
+ '.screenshot.png';
}

/**
* The full path to the screenshot
* @returns {string}
*/
get path() {
return path.resolve(__dirname, '..', 'screenshots', this.filename);
/** The full path to the screenshot */
get fullPath(): string {
return path.resolve(this.dir, this.filename);
}

/**
* @param {string} id A unique identifier used for the screenshot
*/
constructor(id: string) {
this.id = id;
this.id = `${jasmine.getEnv().currentSpec} ${id}`;
browser.takeScreenshot().then(png => this.storeScreenshot(png));
}

/**
* Replaces the existing screenshot with the newly generated one.
*/
/** Replaces the existing screenshot with the newly generated one. */
storeScreenshot(png: any) {
var dir = path.resolve(__dirname, '..', 'screenshots');
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, 0o744);
if (!fs.existsSync(this.dir)) {
fs.mkdirSync(this.dir, '744');
}

if (fs.existsSync(this.dir)) {
fs.writeFileSync(this.fullPath, png, {encoding: 'base64' });
}
fs.writeFileSync(this.path, png, {encoding: 'base64'});
}
}


export function screenshot(id: string) {
return new Screenshot(id);
}

0 comments on commit a48c356

Please sign in to comment.