On Github asmod3us / jstesting
by Achim Staebler @asmod3us
"It used to work in the last spring."
$ node -e 'console.log("hello world!");' hello world!
grab the code from github
$ git clone https://github.com/asmod3us/jstesting
install dependencies with npm
$ npm install
Apple's UI Automation framework uses Javascript.
Many test runners and frameworks available for NodeJS:
We chose mocha because of its flexibility and async support.
Given [1, 2,3]
When a number is not present in the array
Then indexOf should return -1 for it
var should = require('should'); describe('Array', function(){ describe('#indexOf()', function(){ it('should return -1 when not present', function(){ [1,2,3].indexOf(4).should.equal(-1); }); }); }); $ mocha -R spec bdd.js Array #indexOf() ✓ should return -1 when not present 1 test complete (6 ms)
var assert = require("assert"); describe('Array', function(){ describe('#indexOf()', function(){ it('should return -1 when the value is not present', function(){ assert.equal(-1, [1,2,3].indexOf(4)); }) }) }) $ mocha -R spec tdd.js
If you know selenium, this will look familiar. Look at the API docs. Example in Javascript:
… client.getTitle().then(function(title){ expect(title).to.equal('Google'); done(); }); …
or
phantomjs, a headless webkit browser
happens to implement webdriver protocol:
phantomjs --webdriver=4444 &
Requires phantomjs running in the background to provide the webdriver, ie
$ npm run-script webdriver $ mocha -R spec title.js … ✓ should see the correct title
Proof you say?
client.takeScreenshot().then(function(data){ var base64Data = data.replace(/^data:image\/png;base64,/,"") fs.writeFile("google.png", base64Data, 'base64', function(err) { if(err) { console.log(err); } }); }); $ mocha -R spec title.js
PhantomJS not only implements the WebDriver protocol, it also has its own API:
var page = require('webpage').create(); page.viewportSize = { width: 1024, height: 768 }; page.zoomFactor = 1; page.open('http://github.com/', function() { page.render('github.png'); phantom.exit(); }); $ phantom pic.js
For web-based, hybrid and native apps, there is a subset of the WebDriver protocol called the JSON Wire Protocol.
Appium from SauceLabs provides an implementation. Its awesomeness is best experienced in a demo!
You will need a Mac with XCode for this! Appium needs to be running in the background:
$ appium &
or
$ npm run-script appium
Automate a native app:
$ node demo.js
Automate a hybrid app:
$ node hybrid.js
Connect with Achim Staebler on LinkedIn.
Follow him on Twitter.