- Content Monitoring: Checking if specific text appears or disappears from web pages
- Error Detection: Looking for error messages or specific warning text
- Compliance: Ensuring required disclaimers or legal text remains present
- Competition Tracking: Monitoring competitor websites for specific product names or features
Tl;dr this is the playwright code to check for a keyword on your page
Playwright.test.ts
Creating a keyword assertion in Playwright
In this example from the Danube Web Shop, we need to make sure that ‘Haben oder haben’ is always visible. It’s not enough for the page to load, we need our best seller displayed at the top, so we’ll create an assertion looking for the keyword.
BestSeller.test.ts
KEYWORD
. It makes no expectation of the element’s type, so it could be a button or a div or a span or anything else. Further, this doesn’t require that the complete text of the element be this, it could be a button that says ‘click here for Keyword’ and this test would pass. As a final note, getByText
isn’t case sensitive so ‘Keyword,’ ‘KEYWORD’ or ‘KeYwOrD’ would all pass.
Configuring a keyword monitor in Checkly
Next, we’ll add this code to Checkly and set it up to monitor on a set frequency. We have two options for how to set up a page monitor with Checkly:- Through the Checkly Web UI
- As a direct deployment with the Checkly CLI, implementing the monitoring as code model
Creating a page monitor in the Checkly web interface
With our test code from the section above, we can log in to checkly and create a new browser check, select an empty template, and paste in our code.
- Since my ecommerce shop sees dozens of sales per minute, I’m going to set the frequency of runs to every 2 minutes.
- Since I have users in the US and Europe, I’ll select geographic locations there to run checks from.
- The failures I’ve experienced are rarely localized to one region, so I’ll select ‘round robin’ to run one check every 2 minutes from one of my geographic locations.

- A fleeting failure isn’t a big issue to me. If a single page load didn’t have the best seller for some reason, that’s fine as long as it’s there when you check again a minute later, so I’ll set the retry time to ‘linear,’ retrying 2 more times with a minute between them
- Finally I’ll use the global notification settings, and get notified via email, slack, SMS and a via a webhook to Rootly

A complete code example to monitor for a keyword on your page.
The code above lacks the wrapper code to be run as a Checkly monitor, here’s a bare-bones example of that code:KeywordMonitor.test.ts
wait
step after loading the page. Here as in so many page checks we benefit from Playwright’s auto-waiting feature, meaning the code will wait for the element to become visible, and then continue executing as soon as it does. This has a lot of advantages over any manual wait time, aka ‘hard waits’ that can make your page monitors both flakier and slower.
If you’d like to see the benefit of auto-waiting, run the script above from the Checkly editor with the ‘Run Script’ command at the bottom right, then take a look at the trace captured from this check run. The report viewer at the left is also where we can view the screenshot we captured with the last line of our check.

- The page load
- When our target text was visible

Narrowing text requirements: exact matching of keywords
If you want to find an element that has an exact match of the text we searched for, this is an option with the PlaywrightgetByText
method.
KeywordMonitor.test.ts
<span>Keyword!</span>
and matching the capitalization.
Matching multiple elements with a keyword
Taking a look back at our storefront:
KeywordMonitor.test.ts
expect.toBeVisible: Error: strict mode violation: getByText('9.95') resolved to 30 elements
This isn’t a bug in Playwright, it’s just saying: ‘you wanted me to test something about an element, I found many elements that matched the parameters you gave me, so I don’t know which one you were talking about.’ For example, how would we evaluate this test if some of the matches were visible, but others weren’t? Since we can’t give a simple binary answer with multiple results, this check will fail.

page.getByText()
.
Solving strict mode violation
when matching keywords
Tl;dr you can suppress all these resolved to [number] elements
by only looking at the first result:
KeywordMonitor.test.ts
.first()
preemptively since I’m really only worried that the element appears somewhere and don’t care about multiple matches. However this may not make sense for every page monitor!
On the web shop above, the page has a top banner offering a sale. We may want to check if this banner is visible by doing a keyword monitor for the text in the banner, but it wouldn’t be good if this banner was showing up twice.

Checking for multiple possible keywords
In another scenario, we might be checking to make sure that each page has a ‘sign up’ call to action somewhere on the page. We start with an assertion like:KeywordMonitor.test.ts
Checking for keywords with a regular expression
almost any place an assertion or locator is taking a string within Playwright, a regex pattern can also be used.KeywordMonitor.test.ts
/i
config, meaning they will be case sensitive. When I first went to write the example above I used /sign up|register/
and it failed. Made more confusing since the text on the page has text-transform:uppercase
🤣
Regex can be quite useful for detailed matching. For example, if I only want to accept correct capitalization I could write:
KeywordMonitor.test.ts
Checking for multiple conditions with .or()
Since we can return multiple matches on a Playwright locator with .or()
the other way to match on multiple keywords is with a check like this:
KeywordMonitor.test.ts
.first()
so our test will not fail with multiple matches, and examine the first item found for further check conditions:
KeywordMonitor.test.ts
Putting it all together: complete Playwright and Checkly Code for Keyword Monitoring
Let’s cover the case where we want to make sure that users see a ‘sign up’ button on the page we’re checking.BestSellerOnFrontPage.test.ts
BestSellerOnFrontPage.check.ts