2020-06-02 20:30:00 +00:00
|
|
|
'use strict';
|
|
|
|
|
2020-04-17 21:55:45 +00:00
|
|
|
const accessKey = process.env.SAUCE_ONDEMAND_ACCESS_KEY,
|
|
|
|
Builder = require( 'selenium-webdriver' ).Builder,
|
|
|
|
fs = require( 'fs' ),
|
|
|
|
Jimp = require( 'jimp' ),
|
|
|
|
username = process.env.SAUCE_ONDEMAND_USERNAME,
|
|
|
|
webdriver = require( 'selenium-webdriver' ),
|
2020-06-24 19:00:40 +00:00
|
|
|
TIMEOUT = 10 * 1000;
|
2020-04-17 21:55:45 +00:00
|
|
|
|
2020-06-25 17:46:29 +00:00
|
|
|
function createScreenshotEnvironment( test ) {
|
2020-04-17 21:55:45 +00:00
|
|
|
let clientSize, driver;
|
|
|
|
|
|
|
|
test.beforeEach( function () {
|
|
|
|
const lang = this.currentTest.parent.lang || 'en';
|
|
|
|
|
|
|
|
// Use Sauce Labs when running on Jenins
|
|
|
|
if ( process.env.JENKINS_URL ) {
|
|
|
|
driver = new webdriver.Builder().withCapabilities( {
|
|
|
|
browserName: process.env.BROWSER,
|
|
|
|
platform: process.env.PLATFORM,
|
|
|
|
username: username,
|
|
|
|
accessKey: accessKey
|
|
|
|
} ).usingServer( 'http://' + username + ':' + accessKey +
|
|
|
|
'@ondemand.saucelabs.com:80/wd/hub' ).build();
|
|
|
|
} else {
|
|
|
|
// If not running on Jenkins, use local browser
|
|
|
|
driver = new Builder().forBrowser( 'chrome' ).build();
|
|
|
|
}
|
2016-06-24 14:24:42 +00:00
|
|
|
|
2020-04-17 21:55:45 +00:00
|
|
|
driver.manage().timeouts().setScriptTimeout( TIMEOUT );
|
|
|
|
driver.manage().window().setSize( 1200, 1000 );
|
|
|
|
|
2020-06-25 15:10:23 +00:00
|
|
|
driver.get( 'https://en.wikipedia.org/wiki/Help:Sample_page?veaction=edit&vehidebetadialog=1&uselang=' + lang )
|
2020-06-24 19:00:40 +00:00
|
|
|
.then( null, function ( e ) {
|
|
|
|
console.error( e.message );
|
|
|
|
} );
|
2020-04-17 21:55:45 +00:00
|
|
|
driver.wait(
|
|
|
|
driver.executeAsyncScript(
|
|
|
|
require( './screenshots-client/utils.js' )
|
|
|
|
).then( function ( cs ) {
|
|
|
|
clientSize = cs;
|
|
|
|
}, function ( e ) {
|
|
|
|
// Log error (timeout)
|
|
|
|
console.error( e.message );
|
2020-06-25 17:58:46 +00:00
|
|
|
// Setup failed, set clientSize to null so no screenshots are generated
|
|
|
|
clientSize = null;
|
2020-04-17 21:55:45 +00:00
|
|
|
} )
|
|
|
|
);
|
|
|
|
} );
|
2017-03-28 17:15:25 +00:00
|
|
|
|
2020-04-17 21:55:45 +00:00
|
|
|
test.afterEach( function () {
|
2020-06-24 19:00:40 +00:00
|
|
|
driver.quit()
|
|
|
|
.then( null, function ( e ) {
|
|
|
|
console.error( e.message );
|
|
|
|
} );
|
2020-04-17 21:55:45 +00:00
|
|
|
} );
|
2017-03-28 17:15:25 +00:00
|
|
|
|
2020-04-17 21:55:45 +00:00
|
|
|
function cropScreenshot( filename, imageBuffer, rect, padding ) {
|
|
|
|
if ( padding === undefined ) {
|
|
|
|
padding = 5;
|
2017-03-28 17:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 21:55:45 +00:00
|
|
|
const left = Math.max( 0, rect.left - padding );
|
|
|
|
const top = Math.max( 0, rect.top - padding );
|
|
|
|
const right = Math.min( clientSize.width, rect.left + rect.width + padding );
|
|
|
|
const bottom = Math.min( clientSize.height, rect.top + rect.height + padding );
|
2017-03-28 17:15:25 +00:00
|
|
|
|
2020-06-26 20:44:31 +00:00
|
|
|
return Jimp.read( imageBuffer ).then( function ( jimpImage ) {
|
|
|
|
try {
|
2020-06-26 16:48:27 +00:00
|
|
|
jimpImage
|
|
|
|
.crop( left, top, right - left, bottom - top )
|
|
|
|
.write( filename );
|
2020-06-26 20:44:31 +00:00
|
|
|
} catch ( e ) {
|
|
|
|
// Log error (memory?)
|
|
|
|
console.error( e );
|
|
|
|
}
|
|
|
|
} );
|
2020-04-17 21:55:45 +00:00
|
|
|
}
|
2016-06-24 16:51:13 +00:00
|
|
|
|
2020-04-17 21:55:45 +00:00
|
|
|
function runScreenshotTest( name, lang, clientScript, padding ) {
|
2020-06-25 17:58:46 +00:00
|
|
|
if ( !clientSize ) {
|
|
|
|
// Setup failed, don't generated a broken screenshot
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-17 21:55:45 +00:00
|
|
|
const filename = './screenshots/' + name + '-' + lang + '.png';
|
|
|
|
|
|
|
|
driver.manage().timeouts().setScriptTimeout( TIMEOUT );
|
|
|
|
driver.wait(
|
|
|
|
driver.executeAsyncScript( clientScript ).then( function ( rect ) {
|
|
|
|
return driver.takeScreenshot().then( function ( base64Image ) {
|
|
|
|
if ( rect ) {
|
|
|
|
const imageBuffer = Buffer.from( base64Image, 'base64' );
|
|
|
|
return cropScreenshot( filename, imageBuffer, rect, padding );
|
|
|
|
} else {
|
|
|
|
fs.writeFile( filename, base64Image, 'base64' );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}, function ( e ) {
|
|
|
|
// Log error (timeout)
|
|
|
|
console.error( e );
|
|
|
|
} )
|
|
|
|
);
|
2017-02-03 22:57:47 +00:00
|
|
|
}
|
2017-03-28 17:15:25 +00:00
|
|
|
|
2020-04-17 21:55:45 +00:00
|
|
|
return runScreenshotTest;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.createScreenshotEnvironment = createScreenshotEnvironment;
|