Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Example of automated tests on Saucelabs.
// Required Modules
var webdriver = require('wd')
, assert = require('assert')
, fs = require('fs')
, mkdirp = require('mkdirp');
// Define the strings for landscape & portrait
var PORTRAIT = "PORTRAIT";
var LANDSCAPE = "LANDSCAPE";
var iOSonfigurations = [
{
browserName: 'iphone',
version: '6',
platform: 'OS X 10.8',
name: "iPhone"
},
{
browserName: 'ipad',
version: '6',
platform: 'OS X 10.8',
name: "iPad-locahost:9000"
}
];
var androidConfigurations = [
{
browserName: 'android',
version: '4',
platform: 'Linux',
deviceType: 'tablet',
name: "Android-Tablet"
},
{
browserName: 'android',
version: '4',
platform: 'Linux',
name: "Android-Phone"
}
];
var remoteWebDriver = webdriver.remote(
"ondemand.saucelabs.com"
, 80
, "<Username>"
, "<API Key>"
);
remoteWebDriver.on('status', function(info){
console.log('\x1b[36m%s\x1b[0m', info);
});
remoteWebDriver.on('command', function(meth, path){
console.log(' > \x1b[33m%s\x1b[0m: %s', meth, path);
});
performTests(0, androidConfigurations);
function performTests(currentConfig, configurations) {
var date = new Date();
var dateString = '' + date.getMonth() + date.getDay() +
date.getFullYear() + date.getHours() +
date.getMinutes() + date.getSeconds();
var screenshotConfig = {
directory: __dirname+'/screenshots/'+dateString+'/'
};
startTest(currentConfig, configurations, screenshotConfig)();
}
function startTest(currentIndex, configurations, screenshotConfig) {
return function() {
if(currentIndex < configurations.length) {
var config = configurations[currentIndex];
config.orientation = PORTRAIT;
mkdirp(screenshotConfig.directory + config.name + '/',function(err){
runTest(
config,
screenshotConfig,
startTest(currentIndex+1, configurations, screenshotConfig)
);
});
}
}
}
function runTest(config, screenshotConfig, cb) {
console.log("==============================================");
console.log("New Test");
console.log("config.orientation = "+config.orientation);
console.log("config.name = "+config.name);
console.log("==============================================");
remoteWebDriver.chain()
.init(config)
.get('http://localhost:9001/')
.setOrientation(config.orientation, function(err) {
console.log("Setting orientation to "+config.orientation);
if(err) {
console.log("setOrientation() Error = "+err);
}
})
.execute('window.scrollTo(0, 0);', function() {
console.log("Scrolled window to 0, 0");
takeScreenshot(config, screenshotConfig, function() {
checkNavigation(function() {
remoteWebDriver.quit(function() {
if(config.orientation == PORTRAIT) {
config.orientation = LANDSCAPE;
config.currentScrollY = 0;
runTest(config, screenshotConfig, cb);
} else {
cb();
}
});
});
});
});
}
function takeScreenshot(config, screenshotConfig, cb) {
console.log("Taking Screenshot");
var screenshotLocation = getScreenShotLocation(config, screenshotConfig);
remoteWebDriver.takeScreenshot(saveScreenshot(screenshotLocation, cb));
}
function getScreenShotLocation(config, screenshotConfig) {
var fileName = 'screenshot';
if(config.orientation) {
fileName += '-'+config.orientation;
}
fileName += '.png';
return screenshotConfig.directory+config.name+"/"+fileName;
}
function saveScreenshot(directory, cb) {
return function(err, screenshot) {
console.log("Saving Screenshot");
// Write to file
if(!screenshot) {
console.log("ERROR: no screenshot received");
cb();
return;
}
var buffer = new Buffer(screenshot, "base64");
fs.writeFile(directory, buffer, function(err) {
if(err) {
console.log("ERROR: screenshot couldn't be saved: "+err);
}
cb();
});
};
}
function checkNavigation(cb) {
remoteWebDriver.elementByCssSelector('#blog > a', function(err, el) {
assert.ok(el, "There is no #blog > a element");
remoteWebDriver.clickElement(el, function() {
console.log("Click the blog element");
remoteWebDriver.eval("window.location.href", function(err, href) {
assert.ok(~href.indexOf('blog.gauntface.co.uk'), 'Wrong URL for blog!');
cb();
})
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment