Skip to content

Instantly share code, notes, and snippets.

@msell
Forked from patoi/WebDriver_Mocha
Last active August 29, 2015 14:16
Show Gist options
  • Save msell/a1734d73ea0a29513811 to your computer and use it in GitHub Desktop.
Save msell/a1734d73ea0a29513811 to your computer and use it in GitHub Desktop.
/*
* Selenium WebDriver JavaScript test with Mocha and NodeJS
*
* Start with: SELENIUM=PATH_TO_SELENIUM_JAR/selenium-server-standalone-2.31.0.jar mocha -t 10000 -R list google-sample.js
*
* Download selenium-server-standalone-2.31.0.jar from https://selenium.googlecode.com/files/selenium-server-standalone-2.31.0.jar
* 'sudo su' and 'npm install -g colors mocha selenium-webdriver'
*
* http://visionmedia.github.io/mocha/
* https://code.google.com/p/selenium/wiki/WebDriverJs
* https://github.com/marak/colors.js/
*
* @author István Pató
*/
var assert = require('assert')
, fs = require('fs')
, webdriver = require('selenium-webdriver')
, colors = require('colors');
var driver;
// run it once before tests
before(function(done) {
driver = new webdriver
.Builder()
.usingServer('http://localhost:4444/wd/hub')
.withCapabilities(
{
'browserName': 'firefox'
})
.build();
// error handling - if you want do st
process.on('uncaughtException', function(err) {
console.log("My error handler... " + err);
if (driver) {
//recording screenshot
driver.takeScreenshot().then(function(img) {
fs.writeFileSync("/tmp/test.png", new Buffer(img, 'base64'));
});
}
});
// open start page
driver.get('http://google.com').then(function() {
console.log("open google.com...");
done();
});
});
// run it once after tests
after(function(done) {
// works with promise
driver.quit().then(done);
});
// tests
describe('Google Search', function() {
it('should work', function(done) {
var searchBox = driver.findElement(webdriver.By.name('q'));
searchBox.sendKeys('webdriver\n').then(function() {
return searchBox.getAttribute('value');
}).then(function(value) {
assert.equal(value, 'webxdriver');
done();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment