Rules

Secret ingredients to quality software

Do you do automated UI testing?

Last updated by Luke Parker [SSW] on 15 Oct 2021 06:14 am (about 2 months ago) See History

Automated UI testing (aka end-to-end testing) is an awesome way to automate the process of browser based testing.

In the old days, Selenium was the gold standard, but these days it has been overtaken by Playwright which lets you write tests in many popular languages including .NET, Java, Python and Node.js

Playwright has a few advantages over Selenium:

//Store the ID of the original window
const originalWindow = await driver.getWindowHandle();

//Check we don't have other windows open already
assert((await driver.getAllWindowHandles()).length === 1);

//Click the link which opens in a new window
await driver.findElement(By.linkText('new window')).click();

//Wait for the new window or tab
await driver.wait(
    async () => (await driver.getAllWindowHandles()).length === 2,
    10000
  );

//Loop through until we find a new window handle
const windows = await driver.getAllWindowHandles();
windows.forEach(async handle => {
  if (handle !== originalWindow) {
    await driver.switchTo().window(handle);
  }
});

//Wait for the new tab to finish loading content
await driver.wait(until.titleIs('Selenium documentation'), 10000);

Figure: Bad Example - Selenium only lets you have one window focused at a time meaning you can't do parallel testing easily

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

// Create a Chromium browser instance
const browser = await chromium.launch();

// Create two isolated browser contexts
const userContext = await browser.newContext();
const adminContext = await browser.newContext();

// Create pages and interact with contexts independently

Figure: Good Example - Playwright makes it easy to spin up independent browser contexts for parallel testing

Playwright codegen

Playwright offers a super cool feature that lets developers record actions in the browser to automatically generate the code for tests.

Related Rules

  1. Do you follow BDD?
Matt GoldmanMatt Goldman
Jake BaylissJake Bayliss
Piers SinclairPiers Sinclair

We open source. Powered by GitHub