|
// Based on https://github.com/twolfson/multi-image-mergetool/blob/d2611b752060b0173587cfe33618130351e6ba25/bin/_build-demo-screenshots.js |
|
// Load in our dependencies |
|
var assert = require('assert'); |
|
var execSync = require('child_process').execSync; |
|
var functionToString = require('function-to-string'); |
|
var shellQuote = require('shell-quote').quote; |
|
var wd = require('wd'); |
|
|
|
// Resolve our Firefox URL |
|
// https://github.com/angular/protractor/issues/3750 |
|
assert(process.env.FIREFOX_BIN, 'Expected `FIREFOX_BIN` to be defined but it was not. ' + |
|
'Please define `FIREFOX_BIN` with a path to Firefox@47 (Firefox>=50 cannot be hooked on via Selenium at the moment)'); |
|
|
|
// Verify we have our required commands |
|
assert(execSync('which xwininfo')); |
|
assert(execSync('which import')); |
|
|
|
// Set our environment like our other scripts |
|
process.env.DISPLAY = ':99'; |
|
|
|
// Define a function to gather screenshots |
|
function gatherScreenshots(cb) { |
|
// Create our browser |
|
// DEV: Typically we would prefer callbacks over promises but chaining is quite nice |
|
var browser = wd.promiseChainRemote(); |
|
|
|
// Add logging for feedback |
|
browser.on('status', function handleStatus (info) { |
|
console.log('Status:', info.trim()); |
|
}); |
|
browser.on('command', function handleCommand (eventType, command, response) { |
|
// If this is a response, ignore it |
|
if (eventType === 'RESPONSE') { |
|
return; |
|
} |
|
console.log('Command:', eventType, command, (response || '')); |
|
}); |
|
|
|
// Verify there are no other Firefox instances in our display |
|
// xwininfo: Window id: 0x40 (the root window) (has no name) |
|
// Root window id: 0x40 (the root window) (has no name) |
|
// Parent window id: 0x0 (none) |
|
// 0 children. |
|
// OR |
|
// 7 children: |
|
// 0x200038 "Firefox": () 10x10+-100+-100 +-100+-100 |
|
// 0x200023 "Mozilla Firefox": ("Navigator" "Firefox") 2560x1944+0+0 +0+0 |
|
// 0x20001f "Firefox": ("firefox" "Firefox") 200x200+0+0 +0+0 |
|
// 0x20001b "Firefox": ("firefox" "Firefox") 200x200+0+0 +0+0 |
|
// 0x200009 (has no name): () 1x1+-1+-1 +-1+-1 |
|
// 0x200003 (has no name): ("Toplevel" "Firefox") 200x200+0+0 +0+0 |
|
// 0x200001 "Firefox": ("firefox" "Firefox") 10x10+10+10 +10+10 |
|
var xwininfoStr = execSync('xwininfo -root -children').toString('utf8'); |
|
assert.notEqual(xwininfoStr.indexOf('0 children'), -1, |
|
'Other windows are open in our Xvfb instance. Please close them to guarantee a clean screenshot\n' + |
|
xwininfoStr); |
|
|
|
// Create our browser and collect our Xvfb info |
|
var firefoxWindowId; |
|
browser = browser |
|
.init({browserName: 'firefox', firefox_binary: process.env.FIREFOX_BIN}) |
|
.then(function findWindowInDisplay () { |
|
// 0x200023 "Mozilla Firefox": ("Navigator" "Firefox") 2560x1944+0+0 +0+0 |
|
xwininfoStr = execSync('xwininfo -root -children').toString('utf8'); |
|
var firefoxWinInfo = xwininfoStr.match(/(0x[^ ]+) "Mozilla Firefox": \("Navigator" "Firefox"\)/); |
|
assert(firefoxWinInfo); |
|
firefoxWindowId = firefoxWinInfo[1]; |
|
}); |
|
|
|
|
|
// Perform our screenshot collection |
|
// DEV: Firefox will have a scrollbar if we don't make it tall enough (may require Xvfb size increase) |
|
// Additionally, Firefox's nav bar is showing but that should be consistent enough to clip in screenshots |
|
// If we ever get timing resize issues, see the original variant of the script |
|
// https://github.com/twolfson/multi-image-mergetool/blob/d2611b752060b0173587cfe33618130351e6ba25/bin/_build-demo-screenshots.js |
|
function captureWindowViaXvfb(filepath) { |
|
// Example: import -window 0x200023 out.png |
|
// DEV: Ideally we would use `spawnSync` instead of `execSync` + `shellQuote` but this automatically throws errors |
|
execSync(shellQuote(['import', '-window', firefoxWindowId, filepath])); |
|
} |
|
browser = browser |
|
.get('http://getbootstrap.com/') |
|
// https://github.com/admc/wd/blob/v1.1.1/lib/commands.js#L569-L577 |
|
.setWindowSize(1024, 768) |
|
.then(captureWindowViaXvfb.bind(this, 'large.png')) |
|
.setWindowSize(360, 480) |
|
.then(captureWindowViaXvfb.bind(this, 'small.png')); |
|
|
|
// Close our our browser on finish |
|
browser |
|
.fin(function handleFin () { return browser.quit(); }) |
|
.done(function handleDone () { cb(); }); |
|
} |
|
|
|
// Gather our screenshots |
|
gatherScreenshots(function handleError (err) { |
|
// If there was an error, throw it |
|
if (err) { |
|
throw err; |
|
} |
|
}); |