mediawiki-extensions-Visual.../build/screenshots.js
Ed Sanders 085d2443d2 screenshots: Suppress welcome dialog with query string
The code approach appears to suffer from a race condition.

Change-Id: I1328907b8fb55265275493b3b0d2a9288afea53c
2020-06-25 16:10:23 +01:00

111 lines
3.1 KiB
JavaScript

'use strict';
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' ),
TIMEOUT = 10 * 1000;
function createScreenshotEnvironment( test, beforeEach ) {
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();
}
driver.manage().timeouts().setScriptTimeout( TIMEOUT );
driver.manage().window().setSize( 1200, 1000 );
driver.get( 'https://en.wikipedia.org/wiki/Help:Sample_page?veaction=edit&vehidebetadialog=1&uselang=' + lang )
.then( null, function ( e ) {
console.error( e.message );
} );
driver.wait(
driver.executeAsyncScript(
require( './screenshots-client/utils.js' )
).then( function ( cs ) {
clientSize = cs;
}, function ( e ) {
// Log error (timeout)
console.error( e.message );
} )
);
if ( beforeEach ) {
driver.manage().timeouts().setScriptTimeout( TIMEOUT );
driver.wait(
driver.executeAsyncScript( beforeEach ).then(
function () {},
function ( e ) {
// Log error (timeout)
console.error( e.message );
}
)
);
}
} );
test.afterEach( function () {
driver.quit()
.then( null, function ( e ) {
console.error( e.message );
} );
} );
function cropScreenshot( filename, imageBuffer, rect, padding ) {
if ( padding === undefined ) {
padding = 5;
}
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 );
return Jimp.read( imageBuffer ).then( function ( jimpImage ) {
jimpImage
.crop( left, top, right - left, bottom - top )
.write( filename );
} );
}
function runScreenshotTest( name, lang, clientScript, padding ) {
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 );
} )
);
}
return runScreenshotTest;
}
module.exports.createScreenshotEnvironment = createScreenshotEnvironment;