Selenium + Node
Introduction to Selenium Webdriver Introduction to Node.js Installation and Environment set up NPM – Package Manage for Node.js Directory layout of Selenium Webdriver Node.js package Chrome Chrome with options Chrome in Mobile Emulation IE Firefox Element identification methodsAssertions in Selenium in Node.js Interacting with elements in Selenium in Node.js Basic Browser window automation Sending keys in Selenium in Node.js Synchronization in Selenium Check if Element exists Working with Tables using Selenium Performing advanced actions using Selenium in Node.js Executing JavaScript in Selenium in Node.js Working with multiple Browser Windows or tabs Working with multiple frames Handling alerts Common exceptions in Selenium Taking a screenshot in selenium Mocha – Unit testing framework Selenium gridAssertions in Selenium in node
We would be using built in Assertion library in node.js Below code snippet shows how to use built-in assert module in Node.js
var assert = require(‘assert’);
Below assertion will pass if the first argument is true
assert(value[, message])
assert(1==1,”Check that 2 values are equal”);
Below assertion functions are used to verify that 2 variables contains same data or not.
assert.equal(actual, expected[, message])
assert.notEqual(actual, expected[, message])
Below assertion functions are used to verify that 2 variables contains same data or not. 2 Objects are strictly equal when they refer to the same object.
assert.strictEqual(actual, expected[, message])
assert.notStrictEqual(actual, expected[, message])
Below assertion functions are used to verify that 2 variables contains same data or not recursively. This is useful in comparing the JSON objects.
assert.deepEqual(actual, expected[, message])
assert.notDeepEqual(actual, expected[, message])
Below assertion functions are used to verify that 2 variables contains same data or not recursively and strictly.
assert.deepStrictEqual(actual, expected[, message])
assert.notDeepStrictEqual(actual, expected[, message])
Below assertion will pass if the exception is thrown in the code block.
assert.throws(block[, error][, message])
assert.doesNotThrow(block[, error][, message])
We would be using promises to add the sync points. There are many other third party assertion libraries available in Node.js So you can use any of them.- should.js
- expect.js
- chai
- better-assert
- unexpected
var assert = require(‘assert’);
var webdriver = require(‘selenium-webdriver’),
By = webdriver.By,
until = webdriver.until;
var driver = new webdriver.Builder()
.forBrowser(‘chrome’)
.build();
driver.get(‘https://www.softpost.org/selenium-test-page/’);
driver.getTitle().then(function(title) {
console.log(“title is ” + title);
//Below assertion is successful as the title contains substring – selenium test page
assert(title.toLowerCase().indexOf(“selenium test page”)!==1);
});
/*
Here is an alternative syntax to write promises.
var promise = driver.getTitle();
promise.then(function(title) {
console.log(“title is ” + title);
});
*/
driver.quit();
Web development and Automation testing
solutions delivered!!