Selenium + JavaScript E2E Testing

Jagadish Hiremath
3 min readSep 3, 2019

--

Selenium + JavaScript

Including unit tests in your web app projects leads to various benefits: Primarily, an effective and measurable way of proving code quality to developers and other project stakeholders. Browser automation takes these concepts even further and applies the practice to running integration and regression tests live within the presentation layer user interface (UI). From there on, we can codify integration test scripts that do all the work for us — proving that the web app works as expected in the platform in which it will ultimately run.

One of the most known and implemented tools used for web browser automation is Selenium. It is not only intended to be used in automating web applications for testing purposes, but also for automation of web-based administration tasks. In fact, it is a set of different software tools, each with a different approach to supporting test automation. Learning all of its tools will give you many different options for approaching different test automation problems.

JavaScript frameworks

Many software applications are written as web-based applications that run in an Internet browser. This is the main reason that web frameworks are becoming more and more popular among other frameworks. Nowadays, JavaScript is a widely used language for web application development, and even back-end developers prefer JS rather than any other language. It is simple and flexible, but at the same time, it also allows building complex web solutions. The advantages of using JavaScript frameworks are as follows:

  • Efficiency — Projects that used to take months and hundreds of lines of code, can now be achieved much faster with well-structured pre-built patterns and functions.
  • Safety — Top JavaScript frameworks have firm security arrangements and are supported by large communities where members and users also act as testers.
  • Cost — Most frameworks are open source and free. Since they help programmers to build custom solutions faster, the ultimate price for web apps will be lower.

Combining Selenium and JavaScript

One of the most popular JavaScript software stacks for building dynamic web sites and web applications is MEAN stack (MEAN stands for Mongo DB, Express, Angular and Node.js). Here, I’ll use Selenium integration with Node.js, since it’s simple, well organized and very suitable to be used for test automation purposes.

Node.js framework and Selenium setup

The code example below creates a simple validation test for the google web page. To start writing Selenium tests with Node.js and javascript, we need to have a Node.js framework with a Selenium plugin added to it.
If you already installed Node.js, then open a command line and type the following commands to create a new Node.js project and init the basic project structure:

> mkdir sunbird-dev-test

> cd sunbird-dev-test

> npm init

NOTE: If you haven’t already installed Node.js, download it from its official website.

Our next step is to install the node browser automation library called “selenium-webdriver“. In the console you opened previously, use Node’s built-in package manager (NPM) and type the following command to get the package:

npm install –save selenium-webdriver

The option “–save” creates a new package that will be saved to the project’s package.json file.

Before we can start to control the browser automatically, we will need Selenium WebDriver for the specific web browser that we need to automate (IE/FireFox/Chrome/Edage): Please refer below video for setting up

Configuring Node.js, Selenium, and Chrome webdriver on Windows 10

Writing tests using Selenium

Manual site regression testing can take quite a while since it requires a tester to run each test one at a time across different browsers. Running tests asynchronously across various browsers will save a lot of time, and Selenium WebDriver provides this functionality. Selenium handles asynchronous testing by using JavaScript promises.

Once setup is done for chrome-webdriver test simple code by executing below code:

  1. Go to the project(sunbird-dev-test) open command prompt/terminal
  2. Check nodeJs installed or not by hitting node -v
  3. create new file let say app.js and include below code

var webdriver = require(‘selenium-webdriver’);
var driver = new webdriver.Builder().forBrowser(‘chrome’).build();
driver.get(‘https://www.sunbird.org/');

4. run command node app.js

Sample test

--

--

No responses yet