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 = '[email protected]' | |
// 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) |
Cool. I'd give this a shot.
It'd be nice to add a pinch of sugar to
var seq = Sequence(),
_ = seq._
and maybe do something like,
var _ = sequence.create()
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Take 2, this time I've just abandoned using Persona directly for testing because it's just so hard to get right across browsers and am using StubbyID instead to bypass logins (see here: https://gist.github.com/rvagg/6362798).
I've also refactored so that multiple concurrent tests can run so you can do many browsers simultaneously.
persona-sequence.js
This is the test runner that optionally uses SauceConnect to connect to Saucelabs to run the tests. if you set the WEBDRIVER_REMOTE environment variable.
persona-test.js