Assertions 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
Below example shows how you can add the assertions in the test.
 
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!!