To perform a location check in Playwright, you can use the expect
function along with matchers like .toHaveText()
or .toHaveAttribute()
. First, create a locator using the Locators API, such as page.locator()
, to locate elements on the page. For example, to locate a link with the text "Get started", use:
import { test, expect } from '@playwright/test';
test('Location check example', async ({ page }) => {
await page.goto('https://ray.run');
const getStarted = page.locator('a:text("Get started")');
});
Next, perform an action on the locator, like clicking:
await getStarted.click();
Now, you can use the expect
function to check if an element has certain text content or attribute values. For example, to check if the URL has changed after clicking the "Get started" link:
await expect(page).toHaveURL('https://ray.run/get-started');
Or, to check if an element has a specific attribute value:
const exampleElement = page.locator('#example-element');
await expect(exampleElement).toHaveAttribute('data-attribute', 'expected-value');
Remember that if your expectation is not met within a certain timeout period (configurable), your test will fail. For more information on writing efficient Playwright test scripts, check out Tips for Writing Efficient Playwright Test Scripts.
If you still have questions, please ask a question and I will try to answer it.
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].