3

An Introduction to Playwright

 3 years ago
source link: https://www.pmichaels.net/2021/07/31/an-introduction-to-playwright/?utm_campaign=an-introduction-to-playwright
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Pretty much the only end-to-end automation tool that I’ve used to date has been Selenium. I heard about Playwright in passing, and thought I’d have a play. This may end up being the start of a series of posts, because I’m very impressed so far.

Installation

You’ll need node.js installed, and then to install Playwright, it’s just:

npm -i -D playwright    

Running

You can actually have Playwright create the test (or at least some of it) for you.

npx playwright codegen

Launches a browser and an inspector window. Once you’ve create the test, you’ll end up with a script like this:

const { test, expect } = require('@playwright/test');
test('test', async ({ page }) => {
await page.goto('https://www.pmichaels.net/');
// Click a:has-text("About")
await page.click('a:has-text("About")');
expect(page.url()).toBe('https://www.pmichaels.net/about/');
});

You can then run the test using the following command:

npx playwright test

There are some very interesting concepts at play here: playwright will automatically wait for elements to be available, it’s also clever enough to work out if there’s some kind of CSS at work, and will wait for that to complete, too.

Caveats

This one caught me out: it appears that Playwright insists on the tests being named *.spec.js, if they are not, you’ll get:

No tests found

The other thing to note about Playwright is that it’s headless by default – this can be overridden, but it fits much better into the testing idea.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK