This repository has been archived by the owner on Jan 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(puppeteer): add takeScreenshot function (#401)
- Loading branch information
Showing
8 changed files
with
134 additions
and
16 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
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,17 @@ | ||
/// <reference path="../types/puppeteer.d.ts" /> | ||
|
||
import tempWrite = require('temp-write'); | ||
|
||
import {Page} from 'puppeteer'; | ||
|
||
function isOnCI(): boolean { | ||
return process.env.CI === 'true'; | ||
} | ||
|
||
export async function takeScreenshot(page: Page): Promise<string> { | ||
const screenshot = await page.screenshot({type: 'png'}); | ||
|
||
return isOnCI() | ||
? screenshot.toString('base64') | ||
: tempWrite(screenshot, 'screenshot.png'); | ||
} |
This file was deleted.
Oops, something went wrong.
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,60 @@ | ||
/* tslint:disable no-any */ | ||
|
||
import isPNG = require('is-png'); | ||
|
||
import {createTestRunner} from '@cybernaut/test/lib/TestRunner'; | ||
import {readFile} from 'fs-extra'; | ||
import {createTestSetup, createTestTeardown} from '../TestContext'; | ||
import {takeScreenshot} from '../takeScreenshot'; | ||
|
||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30 * 1000; /* 30 seconds */ | ||
|
||
const run = createTestRunner(createTestSetup(), createTestTeardown()); | ||
|
||
describe('takeScreenshot()', () => { | ||
it( | ||
'should return a Base64-encoded PNG', | ||
run(({page}) => [ | ||
async () => { | ||
const CI = process.env.CI; | ||
|
||
process.env.CI = 'true'; | ||
|
||
try { | ||
const png = await takeScreenshot(page); | ||
const data = new Buffer(png, 'base64'); | ||
|
||
expect(isPNG(data)).toBe(true); | ||
} finally { | ||
process.env.CI = CI; | ||
} | ||
} | ||
]) | ||
); | ||
|
||
it( | ||
'should write a PNG file and return its filename', | ||
run(({page}) => [ | ||
async () => { | ||
const CI = process.env.CI; | ||
|
||
process.env.CI = undefined; | ||
|
||
try { | ||
const filename = await takeScreenshot(page); | ||
|
||
expect(filename).toMatch( | ||
/* https://stackoverflow.com/a/13653180 */ | ||
/[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\/screenshot.png$/i | ||
); | ||
|
||
const data = await readFile(filename); | ||
|
||
expect(isPNG(data)).toBe(true); | ||
} finally { | ||
process.env.CI = CI; | ||
} | ||
} | ||
]) | ||
); | ||
}); |
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,5 @@ | ||
declare module 'is-png' { | ||
function isPNG(data: Buffer): boolean; | ||
|
||
export = isPNG; | ||
} |
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,5 @@ | ||
declare module 'temp-write' { | ||
function tempWrite(data: Buffer, name?: string): Promise<string>; | ||
|
||
export = tempWrite; | ||
} |
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