Last active
December 21, 2015 18:49
-
-
Save rvagg/6350237 to your computer and use it in GitHub Desktop.
Selenese (Selenium scripting) in node: experimenting with a DSLish interface to WebDriver. Can connect to saucelabs or a local server or other Selenium installation. Looks sync but is heavily async, lots of talking via WebDriver.
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
const assert = require('referee').assert | |
, refute = require('referee').refute | |
, fail = require('referee').fail | |
// magic pixie dust to get us initialised | |
const $ = require('selenese_node/sequence')(module) | |
const loginEmail = 'testmystuff@mockmyid.com' | |
// some state to maintain along the way | |
var mainWindow | |
, personaWindow | |
// GO! | |
$.title(/some title regex here/i) | |
$.windowIds(function (ids) { | |
assert.equals(ids.length, 1, 'just have one window open') | |
mainWindow = ids[0] // save state for later | |
}) | |
$.click('.persona-identify') | |
// function for this implemented below, simple helper because this is | |
// done twice | |
waitForWindowIdCriteria(function (ids) { | |
if (ids.length > 1) { | |
personaWindow = ids.filter(function (id) { | |
return id != mainWindow | |
})[0] | |
// we can sub-queue additional interactions within calls to create a | |
// hierarchical async call structure, execution won't continue until | |
// this call | |
$.selectWindow(personaWindow) | |
return true | |
} | |
return false | |
}) | |
// now we're in the persona popup window | |
$.waitForVisible('#authentication_email', 30 * 1000) // timeout will cause a fail | |
$.wait(1000) // I don't know why we need to wait here, but I blame persona... | |
$.type('#authentication_email', loginEmail) // enter some text | |
$.click('.buttonrow .isDesktop.isStart.isAddressInfo') // click a button | |
// wait for only one window, i.e. the persona window ought to disappear | |
waitForWindowIdCriteria(function (ids) { | |
if (ids.length == 1) { | |
$.selectWindow(ids[0]) | |
return true | |
} | |
return false | |
}) | |
// and back at the main window, checking for a proper login | |
$.waitForElement('.signed-in a', 20 * 1000) | |
// this would tell us that we're logged in because the page has our email | |
$.textPresent('.signed-in a', loginEmail) | |
// and here we end! | |
$.wait(function (next) { | |
console.log('waiting 1s before we finish, for no good reason') | |
setTimeout(next, 1000) | |
}) | |
// utility $.wait() function | |
function waitForWindowIdCriteria (criteria) { | |
$.wait(function (next) { | |
function windows () { | |
$.windowIds(function (ids) { | |
if (criteria(ids)) | |
return next() | |
setTimeout(windows, 100) | |
}) | |
} | |
windows() | |
}) | |
} |
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
// execute the sequence with this invocation | |
const run = require('selenese_node/run') | |
, personaSequence = require('./persona-sequence') | |
run('http://localhost:3000' , { browserName: 'firefox' }, personaSequence) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the problem with just
var _ = sequence.create()
is that I need to return something, but you're right, it's boilerplate overhead and I need to remove it somehow.