Scraping & asserting on page elements
Any standard Node.js script that successfully finishes an execution is a valid, passing browser check. However, in many cases, you want to validate whether the session shows the right stuff to the user.
This is a two-step process:
- Identify the relevant element on the current page.
- Assert that it shows what you expect.
Playwright Test offers many ways to scrape elements like buttons, forms or any arbitrary HTML element. We’ve listed the most common ones below, together with some tips on how to use them effectively.
Check out this video for a quick explainer:
When it comes to assertions, Playwright Test uses Jest’s expect library. Playwright also extends the expect
library with its own, recommended web-first assertions. These will re-fetch an element and check it until the
condition is met or the test times out. The full list of web-first assertions can be found here.
Scraping text values
For the text assertions, you can use expect().toHaveText()
or expect().toContainText()
. The first one will look for an exact match, while the latter will check for a substring match. Both methods accept regex patterns too. The example below shows how these could be used in a real-world scenario:
Ok, let’s break this down:
- We require
@playwright/test
library, declare a test block and go to the Checkly docs page. - We use
page.getByRole()
and pass it two arguments:
- A role of the element
- An object with
name
property - it will narrow the search to an element with a matching text, or in this case,aria-label
attribute
- We expect the
searchBox
element to containCTRL-K
text (a keyboard shortcut info)
Scraping a list of values
Playwright Test makes it easy to work with lists of elements. The getByX()
methods will return a list, if multiple elements match the parameters. You could then assert the count, text or perform additional filtering on the elements:
- We select all elements that have the CSS class
menu__link
. - In the same line, we filter these elements to those that contain
Advanced
word - We assert the elements count
- We assert that the exact text of these elements (
expect().toHaveText()
accepts arrays too!)
Scraping form input elements
Text input fields
You can use the locator.inputValue()
method to get the text from a standard text input field, or even better, an expect(locator).toHaveValue()
to assert it right away.
Checkboxes, radio buttons and dropdown selects
Scraping the values of other common form elements is pretty similar to scraping text inputs. See the examples below for specific use cases.
Checkboxes:
Radio buttons:
Select menu:
Key takeaways are:
- You can use
locator.check()
andexpect(locator).toBeChecked()
methods to interact with checkboxes and radio buttons locator.selectOption()
will select a new value.expect(locator).toHaveValue()
will assert that the desired value is chosen in a select menu component.
Built-in shortcuts
Playwright Test offers some built-in shortcuts to access common elements of a typical webpage which you can use in Checkly. For the full details see
the Playwright docs on Page
class.
More resources
Last updated on December 13, 2024. You can contribute to this documentation by editing this page on Github