Back to Test Automation
2025-12-155 min read

Browser (Test Automation)

Learn Browser (Test Automation) step by step with clear examples and exercises.

Title: Test Automation with JavaScript: Browser Testing Using Selenium, Cypress, and Playwright


Why This Matters

In today's fast-paced digital world, ensuring software quality is paramount for a successful product launch. Manual testing can be time-consuming, error-prone, and expensive. That's where test automation comes into play, helping developers to run tests more efficiently, accurately, and consistently. This lesson will focus on browser testing using popular JavaScript libraries: Selenium, Cypress, and Playwright.


Prerequisites

To follow this tutorial, you should have a basic understanding of the following:

  • HTML, CSS, and JavaScript fundamentals
  • Node.js and npm (Node Package Manager) installed on your machine
  • Familiarity with browser development tools like DevTools for Chrome or Firefox

Core Concept

Selenium

Selenium is a popular open-source tool used for automating web applications for testing purposes. It provides a browser driver that allows JavaScript to control and interact with the browser just as a user would.

WebDriver

The WebDriver API is the foundation of Selenium. It allows you to create, locate, manipulate, and interact with web elements on a web page. The WebDriver can be implemented in several programming languages, including Java, C#, Python, Ruby, and JavaScript.

Selenium Grid

Selenium Grid is a distributed testing framework that enables parallel execution of tests across multiple platforms and browsers. It allows you to run your tests on different operating systems, browser versions, and screen resolutions.

Cypress

Cypress is an end-to-end testing solution designed specifically for modern web applications. Unlike Selenium, it does not rely on selenium-webdriver or other external libraries. Instead, it uses its own internal implementation to interact with the browser. This results in faster test execution times and more reliable test results.

Cypress Commands

Cypress provides a set of commands that allow you to perform various actions like visiting a URL, clicking on elements, filling forms, and asserting the state of your application. These commands can be chained together to create complex test scenarios.

Playwright

Playwright is a Node.js library for web testing and automation. It supports Chromium, Firefox, and WebKit browsers and offers features similar to Selenium and Cypress. One of its key advantages is the ability to automate native applications on Windows, macOS, and Linux platforms.

Playwright Commands

Playwright provides a comprehensive set of commands for navigating, interacting with, and asserting the state of web pages. It also offers features like screenshots, video recording, and network interception.


Worked Example

In this example, we will create a simple test using each of the three libraries: Selenium, Cypress, and Playwright. We'll write tests to verify that a basic web page displays the correct title and content.

Selenium

const {Builder, By, Key, until} = require('selenium-webdriver');

(async function example() {
let driver = await new Builder().forBrowser('chrome').build();
try {
await driver.get('http://www.example.com');
let title = await driver.getTitle();
console.log(`Page title is: ${title}`);
let content = await driver.findElement(By.css('#content')).getText();
console.log(`Content on the page is: ${content}`);
} catch (error) {
console.error('Error occurred while running test:', error);
} finally {
await driver.quit();
}
})();

Cypress

describe('Example Test', function() {
it('Visits the example page and checks title and content', function() {
cy.visit('http://www.example.com');
cy.title().should('eq', 'Example Domain');
cy.get('#content').should('contain', 'Necessarily, this is an example of a Web page.');
});
});

Playwright

const { chromium } = require('playwright');

(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
try {
await page.goto('http://www.example.com');
const title = await page.title();
console.log(`Page title is: ${title}`);
const content = await page.$eval('#content', el => el.textContent);
console.log(`Content on the page is: ${content}`);
} catch (error) {
console.error('Error occurred while running test:', error);
} finally {
await browser.close();
}
})();

Common Mistakes

Selenium

  1. Not waiting for page elements to load before interacting with them
  2. Using outdated or unsupported browser versions
  3. Ignoring browser-specific quirks and compatibility issues
  4. Failing to properly handle exceptions and errors
  5. Testing on a local machine without considering network latency and other external factors

Cypress

  1. Not using the cy.wait() command to ensure asynchronous actions have completed before continuing with the test
  2. Using CSS selectors that are too specific or prone to changing, causing tests to fail unexpectedly
  3. Ignoring browser-specific quirks and compatibility issues
  4. Not handling network errors gracefully
  5. Failing to properly clean up after tests (e.g., clearing cookies, deleting local storage)

Playwright

  1. Not waiting for page elements to load before interacting with them
  2. Using outdated or unsupported browser versions
  3. Ignoring browser-specific quirks and compatibility issues
  4. Failing to properly handle exceptions and errors
  5. Testing on a local machine without considering network latency and other external factors

Practice Questions

  1. Write a test using Selenium that verifies the login functionality of a web application.
  2. Implement a Cypress test that checks the correctness of form validation in a registration page.
  3. Create a Playwright test to verify the correct display of a responsive design on multiple screen sizes.

FAQ

What is the difference between Selenium, Cypress, and Playwright?

  • Selenium is an older, more general-purpose testing framework that supports various programming languages. It relies on selenium-webdriver or other external libraries for browser interaction.
  • Cypress is a newer, more lightweight testing solution designed specifically for modern web applications. It uses its own internal implementation to interact with the browser.
  • Playwright is a Node.js library that supports Chromium, Firefox, and WebKit browsers. It offers features similar to Selenium and Cypress, including native application automation on various platforms.

Can I use Selenium, Cypress, or Playwright for mobile testing?

  • While Selenium can be used for mobile testing through Appium, both Cypress and Playwright primarily focus on web-based testing. However, Playwright does offer support for native application automation on various platforms.

Is it necessary to use a testing framework like Selenium, Cypress, or Playwright for every project?

  • Not necessarily. The choice of whether to use a testing framework depends on factors such as the size and complexity of the project, available resources, and the desired level of automation. Manual testing may still be appropriate for smaller projects or certain aspects of larger ones.

How can I improve the performance of my test suite using Selenium, Cypress, or Playwright?

  • To improve the performance of your test suite, consider the following best practices:
  • Use parallel execution to run tests concurrently on multiple machines or browsers.
  • Minimize the number of tests by focusing on critical functionality and regression testing.
  • Optimize test setup and teardown times by reusing resources where possible.
  • Implement intelligent wait strategies using cy.wait() in Cypress or explicit waits in Selenium and Playwright.

How can I troubleshoot issues with my tests using Selenium, Cypress, or Playwright?

  • To troubleshoot issues with your tests, consider the following steps:
  • Review the test log files for any error messages or stack traces.
  • Use browser developer tools to inspect the state of the application during test execution.
  • Simulate user interactions manually to identify potential causes of failures.
  • Consult online resources and community forums for solutions to common problems.
Browser (Test Automation) | Test Automation | XQA Learn