This is a shortened version of the Playwright Page documention with methods that may be most frequently used when testing with GuardianTest.
The Page class provides methods to interact with a tab within a browser instance.
Returns the result of running a function in the page context.
You can pass an argument to the function or you can evaluate strings as functions.
const result = await page.evaluate(([a, b]) => {
return Promise.resolve(a + b);
}, [1, 2]);
console.log(result); // 3
const result = await page.evaluate("1 + 2");
console.log(result); // 3
Locate elements by their alt text
<img alt="GuardianUI Example">
page.getByAltText("GuardianUI Example").click();
Locate elements by their label
, or aria-label
<input aria-label="Token Input">
page.getByLabel("Token Input").fill("100");
Locate elements by their placeholder content
<input type="text" placeholder="name">
page.getByPlaceholder("name").fill("John");
Locate element by the data-testid
<button data-testid="submit">
page.getByTestId("submit").click();
Locate elements that have certain text
<div>Hello</div>
page.getByText("Hello");
Locate elements that have a certain title attribute
<span title="Test Title">
page.getByTitle("Test Title");
Navigate to a URL
await page.goto("https://www.guardianui.com");
This is a very commonly used method in tests. It returns an element locator object that can be used to perform actions on the page
page.locator("button:has-text('Demo Button')");
await page.locator("text=Connect").click();
Provides the ability to modify network requests. Every request matching a URL pattern will stall unless it's continued, fulfilled, or aborted.
// Mock a Graph response
await page.route("https://gateway.thegraph.com/api/**", route => {
route.fulfill({
body: {
data: {
"mocked-data"
}
}
});
});
Takes a screenshot
await page.screenshot();
await page.screenshot(options);
Emitted when the page calls either console.log
, console.error
, console.warn
, or console.dir
page.on("console", msg => {
console.log(msg);
});