JS Automation testing review
Requirements
- selenium-server-standalone-2.x.x.jar from the Selenium releases page: https://selenium-release.storage.googleapis.com/index.html
- Node - https://nodejs.org/en/
- Nightwatch - https://github.com/nightwatchjs/nightwatch
e2e testing using the Selenium web driver. You can also specify other webdrivers for different browsers. Either run tests from 1 browser, or all browsers as defined in the test_settings object, for example, ie,chrome,safari
nightwatch -t nightwatch-test.js -e chrome
or
nightwatch -t nightwatch-test.js -e ie,chrome,firefox
to run the tests in multiple browser environments.
Example of a Nightwatch.json file:
Config
{
"src_folders" : ["tests"],
"output_folder" : "reports",
"selenium" : {
"start_process" : true,
"server_path" : "bin/selenium-server-standalone-3.2.0.jar",
"log_path" : "",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "bin/chromedriver.exe",
"webdriver.ie.driver" : "bin/IEDriverServer.exe",
"webdriver.gecko.driver" : "bin/geckodriver.exe"
}
},
"test_settings" : {
"default" : {
"launch_url" : "http://localhost/",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent" : true,
"screenshots" : {
"enabled" : true,
"path" : "./screenshots/"
},
"desiredCapabilities": {
"browserName": "internet explorer",
"elementScrollBehavior": 1,
"javascriptEnabled": true,
"acceptSslCerts": true
}
},
"ie" : {
"desiredCapabilities": {
"browserName": "internet explorer",
"elementScrollBehavior": 1,
"javascriptEnabled": true,
"acceptSslCerts": true
}
},
"safari" : {
"desiredCapabilities": {
"browserName": "safari"
}
},
"chrome" : {
"desiredCapabilities": {
"browserName": "chrome",
"chromeOptions": {
"args":["disable-web-security", "ignore-certificate-errors"]
}
}
},
"firefox" : {
"desiredCapabilities": {
"browserName": "firefox"
}
}
}
}
After running the tests:
Example Test
module.exports = {
'Homepage' : function (client) {
client
.url('http://test.com')
.waitForElementVisible('body', 1000)
.assert.title("My website title")
.click('a[href="/home/"]')
.pause(500)
.assert.title("Home")
.assert.cssClassPresent('nav ul li:nth-child(2)', "active")
// Get number of elements.
.elements('css selector', '.case-study', function (elements) {
this.assert.equal(elements.value.length, 3, 'There should be 3 case-study divs.');
})
// Get number of elements.
.elements('css selector', 'nav ul li', function (elements) {
this.assert.equal(elements.value.length, 6, 'There should be 6 navigation items');
})
.end();
},
'Blank Test' : function(client) {
client
.end();
}
};
Run with
nightwatch -t nightwatch-test.js -e chrome
Site: casperjs.org
Wrapper for Phantom JS
Defining a function for testing 1 particular page:
function HomepageTests(test){
casper.start(URLS.HOME, function() {
this.test.info('Current location is ' + this.getCurrentUrl());
test.assertTitle('My Website Title', 'has the correct title');
test.assertEvalEquals(function () {
return $('nav ul li').length
}, 6, '6 navigation links');
test.assertEvalEquals(function () {
return $('.case-study').length
}, 3, '3 case studies are listed.');
test.assertEvalEquals(function () {
return $('.section-box ').length
}, 2, '2 section boxes.');
});
casper.run(function() {
test.done();
});
}
Then in the same file, the test runner:
/*
--------------------------------------------------
Run the Tests
--------------------------------------------------
*/
casper.test.begin('Testing the homepage', 4, function suite(test) {
HomepageTests(test);
casper.run(function() {
test.done();
});
casper.echo(lines);
});