Generator-based WebDriver client, wrapper around Wd.js
Yiewd is a Wd.js wrapper that uses V8's new
generators for cleaner code! It's called
yiewd because it uses the new
yield syntax with
yiewd. Amazing, right? And a great
way to exercise vowel pronunciation.
Yiewd is made possible with the monocle-js library.
npm install yiewd
Let's say we want to write a webdriver test:
var wd = require'wd'driver = wdremote;driverinitdesiredCapsif err return postTesterr;driverget""if err return postTesterr;driverelementById"someId"if err return postTesterr;elclickif err return postTesterr;setTimeout // pause a bitdriverelementById"anotherThing"if err return postTesterr;el2textif err return postTesterr;textshouldequal"What the text should be";driverquitpostTest;;;1500; // what's this random number doing here? It goes with the pause!;;;;
Yeah, that sucks. Look at that callback pyramid! Look at all those repetitive error checks!
Let's all be a little more sane, shall we?
var yiewd = require'yiewd'driver = yiewdremote;driverrunvar sessionId el el2 text;sessionId = yield thisinitdesiredCaps;yield thisget"";yield thiselementById"someId"click;yield thissleep1.5;text = yield thiselementById"anotherThing"text;textshouldequal"What the text should be";yield thisquit;;
Basically, you get a driver object as a result of the call to
You can use this driver object inside a monocle o-routine to
asynchronous function execution rather than using callbacks. And you'll get the
result of the callback as the assignment to the yield expression!
Once you have a driver object, you can use
driver.run as a way to kick off
a series of commands inside a generator. Here you have access to the driver
this, so you can do things like
It's relatively easy to break up bits of sessions between testcases and so on. Here's what a simple mocha test suite could look like:
var yiewd = require'yiewd';describe'my cool feature'var driver = null; // driver object used across testcases// global setUp, tearDownbeforedriver = yiewdremote;driverrunyield thisinitdesiredCaps;done;;;afterdriverrunyield thisquit;done;;;it'should do some thing'driverrun// test logicdone;;;it'should do another thing'driverrun// test logicdone;;;;
Notice how you get a
driver object from
yiewd.remote(). You can hold onto
this and later use
driver.run() and pass it another generator which will take
over execution for the driver. Easy!
Using monocle-js, you can compose your own custom automation behaviors:
var o_O = require'monocle-js'o_Oyiewd = require'yiewd'driver = yiewdremote;var flow1 = o_Oyield driverget'';yield driverelementByCss'a'click;;var flow2 = o_Oyield driverelementByCss'input[type=text]'sendKeys"my text";yield driverelementById'submit'click;;describe'my cool feature'it'should do some things'driverrunyield thisinitdesiredCaps;yield flow1;yield flow2;yield flow1; // reuse flow1done;;;;
Often in WebDriver-land, you only want to find an element in order to do something with it. In those cases, it's a bit tedious to do something like this:
var el1 = yield driverelementById'someEl';var text = yield el1text;textshouldequal"hello world";
var text = yield yield driverelementById'someEl'text;textshouldequal"hello world";
But we have this goofy double-yield business. So Yiewd lets you do away with it:
var text = yield driverelementById'someEl'texttextshouldequal"hello world";
We've got some special sauce so you can sauce while you Sauce:
var yiewd = require'yiewd'driver = yiewdsauceuserName accessKey;driverrunyield thisinitdesiredCaps;yield thisget'';tryvar title = yield thistitle;titleshouldinclude"I am a page title";yield thisreportPass;catch eyield thisreportFail;yield thisquit;;
Probably the pass/fail reporting would be handled in some kind of global tearDown method, of course.
--harmonyflag; this might be non-trivial but for mocha, see below.
Any recent node
Run your test scripts through regenerator to get generator support, e.g.:
regenerator my_test.js > my_test_es5.js && mocha my_test_es5.js
npm install -g mocha
Make sure you have your chromedriver-enabled Selenium server running, then:
mocha -R spec -t 60000 --harmony test/es6/
This is a simple wrapper around Wd.js that is really easy to maintain: (a) new methods from Wd.js can be added with one word in Yiewd, (b) there's nothing really to maintain beyond the generator glue which should stabilize quickly.
Give it a whirl and contribute bugfixes! Pull requests welcome. Biggest area of need right now is filling out our testsuite to make sure everything works correctly.