-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(screenshot): Add screenshot function to e2e test (button and che…
…ckbox) (#2532) * Add screenshot function to e2e test (button and checkbox) * Add reporter for jasmine * Change directory to constant, and use variable to store name
- Loading branch information
1 parent
64f6d1b
commit 8ba8deb
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +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 () { | ||
element(by.id('test-button')).click(); | ||
expect(element(by.id('click-counter')).getText()).toEqual('1'); | ||
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('click disabled'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import * as fs from 'fs'; | ||
import * as gulp from 'gulp'; | ||
import * as path from 'path'; | ||
import {browser} from 'protractor'; | ||
|
||
const OUTPUT_DIR = '/tmp/angular-material2-build/screenshots/'; | ||
|
||
let currentJasmineSpecName = ''; | ||
|
||
/** Adds a custom jasmine reporter that simply keeps track of the current test name. */ | ||
function initializeEnvironment(jasmine: any) { | ||
let reporter = new jasmine.JsApiReporter({}); | ||
reporter.specStarted = function(result: any) { | ||
currentJasmineSpecName = result.fullName; | ||
}; | ||
jasmine.getEnv().addReporter(reporter); | ||
} | ||
|
||
initializeEnvironment(jasmine); | ||
|
||
export class Screenshot { | ||
id: string; | ||
|
||
/** The filename used to store the screenshot. */ | ||
get filename(): string { | ||
return this.id | ||
.toLowerCase() | ||
.replace(/\s/g, '_') | ||
.replace(/[^/a-z0-9_]+/g, '') | ||
+ '.screenshot.png'; | ||
} | ||
|
||
/** The full path to the screenshot */ | ||
get fullPath(): string { | ||
return path.resolve(OUTPUT_DIR, this.filename); | ||
} | ||
|
||
constructor(id: string) { | ||
this.id = `${currentJasmineSpecName} ${id}`; | ||
browser.takeScreenshot().then(png => this.storeScreenshot(png)); | ||
} | ||
|
||
/** Replaces the existing screenshot with the newly generated one. */ | ||
storeScreenshot(png: any) { | ||
if (!fs.existsSync(OUTPUT_DIR)) { | ||
fs.mkdirSync(OUTPUT_DIR, '744'); | ||
} | ||
|
||
if (fs.existsSync(OUTPUT_DIR)) { | ||
fs.writeFileSync(this.fullPath, png, {encoding: 'base64' }); | ||
} | ||
} | ||
} | ||
|
||
export function screenshot(id: string) { | ||
return new Screenshot(id); | ||
} |