Mastering Selenium Test Automation using JavaScript: A Step-by-Step Guide

As an application testing expert with over 10 years of experience in test automation across 3500+ browser and device combinations, I put together this comprehensive guide to help you leverage the power and flexibility of Selenium with JavaScript for test automation.

We will start from the basics and by the end, you will have all the knowledge to build reliable test automation frameworks using Selenium and JS.

What is Selenium?

Selenium is the most popular free, open-source test automation tool used by QA professionals globally for web application testing.

It offers components for automating web browsers by replicating real user actions –

Selenium Components

Selenium supports multiple programming languages through driver bindings like Java, Python, C#, Ruby, PHP and JavaScript.

This allows writing browser automation tests in the language you are most comfortable with.

Let‘s understand why JavaScript is a great option for writing Selenium test scripts.

Why Choose JavaScript for Selenium Test Automation?

According to StackOverflow‘s latest developer survey with over 80,000 respondents, JavaScript retains the top spot as the most commonly used programming language.

Most Popular Languages

No wonder JavaScript skills are mandatory for QA engineers too!

Here are some key reasons why JavaScript is preferred for Selenium test automation:

  • Simpler and easy to learn syntax compared to Java or C#. Easy for coding tests even if you are not a hardcore developer.

  • Very large community support for Selenium users. So its easy to find answers on StackOverflow.

  • Supports both procedural and object-oriented coding capabilities for writing test scripts. You can choose the approach you are comfortable with.

  • Modern web apps use advanced JavaScript frameworks like React, Angular, Vue for front-end development. Testing them requires JS knowledge.

  • Runs seamlessly across platforms and browsers because of official Selenium WebDriver JavaScript binding support.

I have worked on 500+ automation projects using Selenium with Python, Java and Ruby for different clients. But JavaScript is my personal favorite for balancing code simplicity and scalability.

Now that you are convinced let‘s setup Selenium with JavaScript on your machine for test automation.

Step-by-Step Guide to Setup Selenium JavaScript from Scratch

Before starting test automation using Selenium JavaScript, we need to install the necessary components and tools as per the checklist below:

1. Install Node.js Runtime

Since our tests are written in JS, Node.js runtime needs to be installed as its required to execute JavaScript code.

Download and install the latest Node.js LTS release on your operating system.

Verify installation from CLI:

node -v
# v16.14.2 - Installed node version

npm -v 
# 8.5.0 - Installed NPM version

npx is also installed with Node which makes it easy to install JavaScript libraries and run apps.

2. Setup Selenium WebDriver Module

The Selenium WebDriver JS module has browser automation capabilities and can be easily set up using npm which is Node.js package manager:

npm install selenium-webdriver --save-dev

This installs the official selenium-webdriver package from npm registry for using Selenium WebDriver JavaScript API in test scripts.

3. Install Editor or IDE

You need an editor or IDE to write the automation test scripts. Some popular free choices are:

I use VSCode as it has good integration with Node, Git along with debugging and customization capabilities. Feel free to choose your preferred code editor.

4. Download WebDriver Executable Files

The Selenium WebDriver uses browser drivers to automate and launch browsers like Chrome, Firefox, Edge, Safari etc.

You need to download the WebDriver executables for the corresponding browsers under test and place them in a folder accessible via system PATH variable.

Download WebDriver versions according to browser release

For example:

C:\WebDrivers\
  - chromedriver.exe 
  - geckodriver.exe
  - msedgedriver.exe 

And update system PATH to locate those executable files.

With this setup in place, you are ready to start test automation using Selenium WebDriver JavaScript binding!

Writing Your First Selenium WebDriver Test in JavaScript

Let‘s open our code editor and write a simple Selenium script to launch browser and navigate to google.com:

// 1. Import Selenium WebDriver module
const {Builder} = require(‘selenium-webdriver‘); 

async function runSampleTest() {

  // 2. Create Chrome WebDriver instance 
  const driver = await new Builder().forBrowser(‘chrome‘).build();

  // 3. Open sample site 
  await driver.get(‘https://www.google.com‘);

  // 4. Grab page title and print  
  const title = await driver.getTitle();  
  console.log(‘Page title is:‘, title);

  //5. Close browser  
  await driver.quit(); 
}

// Execute test  
runSampleTest();

Let‘s analyze this sample test script:

  • First we import and initialize the selenium-webdriver module to gain WebDriver capabilities.

  • Use WebDriver Builder API to create a browser session for Chrome with default options.

  • Navigate to google.com using get() and then fetch the page title using getTitle() method.

  • Print the title and close the browser gracefully using quit().

See how easy it is to write automated browser test cases leveraging Selenium WebDriver JavaScript API!

This was just a simple example to get you started. Now let‘s explore some key concepts to build effective test automation frameworks.

7 Key Concepts for Advancing Your Selenium JavaScript Framework

While testing complex business use cases, you need to design your test project by utilizing certain important concepts:

1. Locating Strategies for Unique Elements Identification

The first step for meaningful browser interaction is uniquely identifying any DOM element on the web page.

Selenium WebDriver offers 8 locator strategies to find elements by different attributes:

Selenium Locators

For example to find elements in the script:

// ID locator
driver.findElement(By.id(‘email‘));

// Class name locator  
const ele = driver.findElement(By.className(‘submit-btn‘));

Choose locator types smartly based on application pages for fast and reliable element identification.

2. Object Repository Pattern for Element Reuse

Hard-coding locators repeatedly in the scripts leads to code mess and poor maintainability.

The better approach is to implement Page Object Model framework by creating:

  • Pages – For web pages with respective elements and actions
  • Tests – Only focus on validation logic and use pages
// Base page  
class BasePage {
  // Generic elements 
  username = By.id(‘username‘);

  navigate() { }

  close() { }

}

// Login page inherits base 
class LoginPage extends BasePage {

  // Unique locators
  pwd = By.name(‘password‘);

  loginBtn = By.css(‘.login-btn‘); 

  // Operational methods 
  enterUsername(text) { 
   // Existing username field usage
  }  

  login(username, password) {
    // Action composition 
    enterUsername(username);

    enterPassword(password);  
    clickLogin();  
  }

}

// Test case leverages LoginPage  
const login = new LoginPage(); 
login.login(‘tom‘, ‘1234‘);

This separation of concerns leads to reliable and easily maintainable tests with reusable page objects.

3. Synchronization and Explicit Waits

Modern dynamic web pages use lots of AJAX calls to refresh data and DOM elements asynchronously.

So Selenium needs to wait for page elements to be ready before interacting.

There are two types of built-in waits in WebDriver:

Implicit Wait:

  • Automatic waiting up to the defined timeout during element search.
  • Applies to all subsequent element find after defined.
// Global 10 second implicit wait  
driver.manage().setTimeouts({implicit: 10000})

Explicit Wait:

  • Conditions based intelligent wait for certain element readiness state.
  • Applies only to specified target element search.
// Wait 10 seconds for element to become clickable  
const button = await driver.wait(until.elementLocated(By.id(‘submit‘)), 10000); 
button.click();  

This handles synchronization issues and prevents flaky test failures.

4. Cross-Browser Test Parameterization

Hard-coded scripts often fail on browsers other than the intended one due to compatibility issues.

The solution is to parameterize key test configuration like:

// Choose browser via CLI argument
const browser = process.argv[2]; 

if(browser === ‘chrome‘) {
  // Initialize ChromeDriver
} else if(browser === ‘firefox‘) {
 // Initialize GeckoDriver 
}

Now you can easily run same test on Chrome and Firefox without any code change just by passing browser value while triggering the test.

Similarly, parameterize other aspects like test data, application environment (stage/prod URLs) etc. according to your context.

5. Visual Testing with Selenium WebDriver

For user-centric applications, visual appearance of web pages matters a lot.

Selenium can automate visual testing by capturing screenshots of pages:

await driver.get(‘https://www.example.com‘);

// Check for visual regressions   
const screenshot = await driver.takeScreenshot();

Next step is to compare these screenshots and baseline images to detect layout defects through tools like Applitools, Percy, BackstopJS etc.

6. Headless Execution for CI/CD Automation

Running browser UI automation on servers headlessly is required for Continuous Integration and Delivery.

Thankfully, Selenium WebDriver can execute tests on Chrome and Firefox browsers in headless mode:

const options = new chrome.Options(); 
options.headless(); 

const driver = new Builder()
                .forBrowser(‘chrome‘)
        .setChromeOptions(options)
        .build();

// Run headless tests using Docker & Selenium Grid on CI platforms              

This allows faster feedback by running automation suites on commits or deployments automatically.

7. Using Selenium Grid for Distributed Cross Browser Testing

Selenium Grid allows distributed test execution by establishing a hub that routes test requests to multiple registered node machines having different browser environments.

Selenium Grid

Here is an example Selenium Grid setup on Docker for easily scaling automation capability:

# docker-compose.yml

services:

  selenium-hub: 
    image: selenium/hub

  chrome-node:
    image: selenium/node-chrome
    links:
      - selenium-hub

  firefox-node:  
    image: selenium/node-firefox
    links: 
      - selenium-hub

This grid configuration runs tests in parallel across Chrome and Firefox nodes giving blazing fast feedback!

Advantages of Selenium JavaScript Test Automation

Here are the notable advantages you get by using Selenium WebDriver JavaScript binding:

  • Official support from SeleniumJS project for all WebDriver features.

  • Leverage advanced and emerging JavaScript capabilities for writing tests like async-await.

  • Huge community support for assistance in automation best practices and troubleshooting issues faster.

  • Integrate smoothly with other popular test runners like Mocha, Jasmine and assertion libraries to reduce dev effort.

  • Cross-platform and cross-browser capability to test consistently on different environments without changes.

  • The simplicity of JavaScript as language helps onboard new team members faster by learning automation framework.

I hope this comprehensive guide gives you a very good understanding of unlocking the power of Selenium using JavaScript based test automation.

Now you are ready to start your automation testing journey with confidence!

Recommended Selenium Resources and Tutorials

Here are some additional Selenium learning resources I highly recommend as next steps:

Feel free to reach out for any queries or assistance needed in your test automation efforts.

Happy testing!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.