Selenium Web Driver Automation with Cabbie
Kushan Shalindra Amarasiri
Director Quality Engineering at Social Catfish
We are now at the stage of learning test automation via NodeJS and also en-lighting our knowledge on creating test automation frameworks via NodeJS architecture.
There are so many node modules which we can utilize to automating web applications. WebDriver.io, Protractor, Nightwatch, Cypress.io are some of these. Most tools are wrappers of Selenium WebDriver.
This tool that I'm going to en-lighten you on this wonderful blog is called, Cabbie. Cabbie is also based on Selenium WebDriver. It has both asynchronous as well as synchronous implementations. We can run Cabbie on any browser which supports Selenium WebDriver.
Lets install Cabbie, Chrome Driver and Type Script with the following NPM commands.
npm install cabbie-sync@latest
npm install -g typescript
npm install -g chromedriver
Next lets create the following type script scenario
import * as assert from 'assert';
import cabbie, {startChromedriver} from 'cabbie-sync';
import {Element} from 'cabbie-sync';
// Start the chromedriver server, this provides a local selenium server
// You must install chromedriver to use this.
startChromedriver();
// connect to chromedriver, adding {debug: true} makes cabbie log each method call.
const driver = cabbie('chromedriver', {debug: true});
try {
// navigate to a url in the currently active window
driver.activeWindow.navigateTo('https://www.google.lk');
var element = driver.activeWindow.getElement('[name="q"]');
element.sendKeys("Cabbie NPM");
element.submit();
// get an element, and check that its text equals some expected value
assert.equal(
driver.activeWindow.getElement('[title]').getText(),
'Cabbie NPM - Google Search',
);
} finally {
// whether tests pass or fail, dispose of the driver
driver.dispose();
}
Lets save the file with .ts extension and convert the typescript to Java script with the following command.
tsc cabbie1.ts
A java script file which is similar to the name of the typescript file will be created in the same folder.
Lets execute the .js file with the node command.
node cabbie1.js
The test case will be executed opening the Chrome browser and the command line will show the execution steps.