Back to Test Automation
2026-03-307 min read

Grouping by browser (Test Automation)

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

Why This Matters

Test automation plays a crucial role in modern software development, ensuring code quality and consistency across various platforms and browsers. By grouping tests by browser, we can isolate issues specific to certain browsers, improving our application's cross-browser compatibility, particularly for testing user interfaces (UI). In this full guide, we will delve into test automation using JavaScript and popular libraries such as Selenium, Cypress, and Playwright. By the end of this lesson, you will have a strong understanding of creating efficient test suites that run across multiple browsers for various testing scenarios.

Prerequisites

Before diving into the core concept, it's essential to have a good understanding of:

  1. JavaScript basics, including variables, functions, and control structures
  2. HTML and CSS fundamentals for creating web pages
  3. Familiarity with at least one test automation library (Selenium, Cypress, or Playwright)
  4. Understanding the basic concepts of Continuous Integration/Continuous Deployment (CI/CD)
  5. Basic knowledge of Node.js and npm for managing project dependencies

Core Concept

Test automation libraries like Selenium, Cypress, and Playwright allow you to write tests in JavaScript that can be executed across multiple browsers. To group tests by browser, we'll create separate test files for each browser and execute them using CI/CD tools such as Jenkins or CircleCI.

Browser-specific Test Files

Create a new folder for each supported browser (e.g., firefox, chrome, edge) within your project directory. Inside each folder, create a JavaScript file containing the tests specific to that browser. For example:

// firefox/test_firefox.js
const { Builder } = require('selenium-webdriver');

(async function() {
const driver = await new Builder().forBrowser('firefox').build();
try {
await driver.get('http://example.com');
const title = await driver.getTitle();
console.log(`Firefox: ${title}`);
} catch (error) {
console.error(error);
} finally {
await driver.quit();
}
})();

Running Tests in Parallel

To run tests in parallel across multiple browsers, you'll need to configure your CI/CD pipeline to execute the browser-specific test files simultaneously. This can be achieved by using a test runner like Mocha or Jest, which support running tests in parallel out of the box.

For example, with Mocha, you can run all tests in parallel using the --parallel flag:

mocha --parallel

Worked Example

Let's create a simple test suite that verifies the title of a web page across Firefox and Chrome. First, install Selenium WebDriver for both browsers:

npm install selenium-webdriver selenium-webdriver-firefox selenium-webdriver-chrome

Next, create the test files for each browser:

firefox/test_firefox.js

const { Builder } = require('selenium-webdriver');

(async function() {
const driver = await new Builder().forBrowser('firefox').build();
try {
await driver.get('http://example.com');
const title = await driver.getTitle();
console.log(`Firefox: ${title}`);
await driver.quit();
} catch (error) {
console.error(error);
}
})();

chrome/test_chrome.js

const { Builder } = require('selenium-webdriver');

(async function() {
const driver = await new Builder().forBrowser('chrome').build();
try {
await driver.get('http://example.com');
const title = await driver.getTitle();
console.log(`Chrome: ${title}`);
await driver.quit();
} catch (error) {
console.error(error);
}
})();

Now, let's create a script to run both tests in parallel using Mocha:

run_tests.js

const mocha = require('mocha');
const fs = require('fs');
const path = require('path');

// Find all browser-specific test files
const testFiles = [];
const rootDir = __dirname;
const browsers = ['firefox', 'chrome'];
browsers.forEach(browser => {
const folderPath = path.join(rootDir, browser);
fs.readdirSync(folderPath).forEach(file => {
if (file.endsWith('.js')) testFiles.push(path.join(folderPath, file));
});
});

// Run tests in parallel using Mocha
mocha.run(testFiles, function(failures) {
if (failures > 0) {
console.error(`${failures} test failures`);
process.exit(1);
} else {
console.log('All tests passed');
process.exit(0);
}
});

To run the tests, execute:

node run_tests.js

Running Tests with Cypress and Playwright (Example Added)

You can also use Cypress or Playwright for test automation with JavaScript. Here's an example of how to write and run tests using these libraries:

Cypress Example

  1. Install Cypress: npm install cypress
  2. Create a new spec file (e.g., cypress/integration/test_firefox.spec.js) for Firefox tests:
describe('Firefox Tests', function() {
it('Visits example.com and verifies the title', function() {
cy.visit('http://example.com');
cy.title().should('include', 'Example Domain');
});
});
  1. Run Cypress tests: cypress run --browser firefox

Playwright Example

  1. Install Playwright: npm install playwright
  2. Create a new test file (e.g., playwright/test_firefox.js) for Firefox tests:
const { chromium, firefox, webkit } = require('playwright');

(async function() {
const browser = await firefox.launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('http://example.com');
const title = await page.title();
console.log(`Firefox: ${title}`);
await browser.close();
})();
  1. Run Playwright tests: npx playwright test firefox/test_firefox.js

Practice Questions

  1. How can you modify the example above to test the same web page using Playwright instead of Selenium? (Answer provided in the Worked Example section)
  2. What are some common browser-specific quirks that you should be aware of when writing cross-browser tests? (Some examples include different default font sizes, handling of CSS3 properties, and JavaScript engine behavior.)
  3. How can you handle timeouts in your tests to ensure they don't fail due to slow network conditions or server responses? (You can use custom timeouts, retries, or wait functions to handle slow network conditions or server responses.)
  4. What is the purpose of using a test runner like Mocha or Jest, and how do they help with running tests across multiple browsers? (Test runners provide features like test isolation, assertion libraries, and test organization, making it easier to manage and run tests in parallel across multiple browsers.)
  5. How can you improve the example above to handle different browser versions or custom browser configurations? (You can set environment variables or use configuration files to specify the desired browser version or custom browser configurations for your tests.)
  6. What are some best practices for organizing and maintaining a large test suite? (Some best practices include using modular test structures, keeping tests independent, using descriptive test names, and regularly updating and refactoring tests as needed.)

Common Mistakes

  1. Not isolating browser instances: Ensure each test runs in a separate browser instance to avoid interference between tests. You can achieve this by launching a new browser instance for each test or using a headless browser mode.
  2. Ignoring browser-specific quirks: Be aware of known issues and workarounds for specific browsers when writing tests. Use browser-specific APIs or polyfills to ensure compatibility across different browsers.
  3. Not handling exceptions gracefully: Properly handle errors and timeouts to ensure your tests don't fail due to unexpected conditions. You can use try/catch blocks, retries, or custom error handlers for this purpose.
  4. Not using a test runner: Use test runners like Mocha or Jest to manage tests, run them in parallel, and generate reports. Test runners also provide features like test isolation, assertion libraries, and test organization.
  5. Ignoring CI/CD configuration: Properly configure your CI/CD pipeline to execute tests across multiple browsers and platforms. This ensures that your tests are run consistently in various environments, improving the reliability of your test suite.
  6. Not setting up environment variables or custom browser configurations: Make sure to set up any necessary environment variables or custom browser configurations for your tests to run correctly. This may include setting the desired browser version, enabling or disabling headless mode, or configuring network conditions.
  7. Ignoring test maintenance and updates: Regularly maintain and update your test suite to ensure it stays relevant and accurate as your application evolves. This includes fixing any broken tests, updating test data, and adding new tests for new features or functionality.

FAQ

  1. How can I run my tests in parallel using Selenium WebDriver? To run tests in parallel with Selenium WebDriver, you'll need to use a test runner like Mocha or Jest that supports running tests in parallel out of the box. You can then configure your CI/CD pipeline to execute these tests simultaneously.
  2. What are some browser-specific quirks I should be aware of when writing cross-browser tests? Some common browser-specific quirks include different default font sizes, handling of CSS3 properties, and JavaScript engine behavior. It's essential to test your application across multiple browsers to ensure compatibility and identify any issues that may arise due to these differences.
  3. How can I handle timeouts in my tests to prevent failures caused by slow network conditions or server responses? You can use custom timeouts, retries, or wait functions to handle slow network conditions or server responses. This ensures your tests don't fail due to unexpected delays and helps maintain the reliability of your test suite.
  4. Why should I use a test runner like Mocha or Jest for my test automation? Test runners provide features like test isolation, assertion libraries, and test organization, making it easier to manage and run tests in parallel across multiple browsers. They also generate reports, which can help you identify issues and track the progress of your tests.
  5. How can I improve my test suite's maintainability and accuracy as my application evolves? To ensure your test suite remains relevant and accurate, regularly update and refactor your tests to reflect any changes in your application. This includes fixing broken tests, updating test data, and adding new tests for new features or functionality. You should also follow best practices like using modular test structures, keeping tests independent, and using descriptive test names.
Grouping by browser (Test Automation) | Test Automation | XQA Learn