Test Automation Full Swing with WebDriver.JS with Mocha and Chai
Kushan Shalindra Amarasiri
Head of Quality Engineering at Social Catfish
We are now in a stage where test automation has grown into its full-swing. There are abundance of test automation tools that we can select. The selection should be according to the test requirements of the project that we are going to automate.
In our previous articles we have looked into some of the Node.JS based test automation tools such as Protractor, NightWatch, Cypress.io, WebDriver.io.
In this article I want to enlighten you on the java script implementation of WebDriver. Its called WebDriver.JS. Its a created considering as a wrapper to our well-known Selenium WebDriver, so that all the commands available in WebDriver can be utilized.
To start automating with WebDriver.JS, we should first install the required Node packages.
npm install selenium-webdriver
npm install chromedriver
npm install -g mocha
npm install --save chai
After installing the above node packages we are ready with our test automation environment.
Our test automation framework will be mocha and assertion library will be chai.
Next lets try to create our test automation scenario.
var webdriver = require('./node_modules/selenium-webdriver');
var chrome = require('./node_modules/chromedriver');
var chai = require('./node_modules/chai');
describe( 'Test Suite - Google Search' , function(){
it( 'Test Case - Google Searching', function(){
var driver = new webdriver.Builder()
.withCapabilities( { 'browserName' : 'chrome' } )
.build();
driver.get("https://www.google.com");
driver.findElement(webdriver.By.id("lst-ib")).sendKeys("webdriverjs");
driver.findElement(webdriver.By.id("hplogo")).click();
driver.findElement(webdriver.By.name("btnK")).click();
driver.findElement(webdriver.By.linkText("webdriverjs - npm")).click();
var titlePromise = driver.getTitle();
titlePromise.then(function(title){
chai.expect (driver.getTitle()).equals("webdriverjs");
});
})
});
Lets execute the test scenario, and we can see that Chrome browser will come up and it will execute the test scenario specified.
There we go.. we had a smooth sailing with WebDriver.JS
Principal Quality Engineer at Gear4Music Ltd.
6 年Mahesh Joshi This is very similar to what I use for frontend automation, I use WebdriverIO along with mocha and chai. I'll have to give you a demo some time