Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.
waitForLoadState(state[, options])
Caution
This method has known issues. For details, refer to #880.
This waits for the given load state to be reached. It will immediately unblock if that lifecycle event has already been received.
Parameter | Type | Default | Description |
---|---|---|---|
state | string | load | Optional load state to wait for. See Events for more details. |
options | object | null | |
options.timeout | number | 30000 | Maximum time in milliseconds. Pass 0 to disable the timeout. Default is overridden by the setDefaultTimeout option on BrowserContext or Page. |
Events
Caution
networkidle
is DISCOURAGED. Don’t use this method for testing especially with chatty websites where the event may never fire, rely on web assertions to assess readiness instead.
Events can be either:
'domcontentloaded'
- consider operation to be finished when theDOMContentLoaded
event is fired.'load'
- consider operation to be finished when theload
event is fired.'networkidle'
- Consider operation to be finished when there are no network connections for at least500
ms.
Example
import { check } from 'k6';
import { browser } from 'k6/experimental/browser';
export const options = {
scenarios: {
browser: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
export default async function () {
const page = browser.newPage();
try {
await page.goto('https://test.k6.io/my_messages.php');
page.locator('input[name="login"]').type('admin');
page.locator('input[name="password"]').type('123');
const submitButton = page.locator('input[type="submit"]');
await submitButton.click();
page.waitForLoadState(); // waits for the default `load` event
check(page, {
header: (p) => p.locator('h2').textContent() == 'Welcome, admin!',
});
} finally {
page.close();
}
}