- Navigating to some web page
- Waiting for something
- Possibly getting a timeout 😐
Navigating
Initial navigation to any page is pretty much the same for both frameworks and can happen in multiple ways.- Whenever your code does a
page.goto()
, or apage.click()
on a link, you explicitly trigger a navigation. - The webpage you are on can also trigger a navigation by executing
location.href= 'https://example.com'
or using thehistory.pushState()
API.
- The initial load of the page.
- A navigation to the shopping cart by clicking a link
basic-browser-navigation.spec.ts
Waiting
waiting for something to happen is a crucial part of any automation script. In most cases, this is handled automatically by Playwright. For example, when you click a button, Playwright will wait for that button to be clickable before it actually clicks it. In the example below, we type an email address into an input field on a login modal. Playwright’sfill
method comes with
built-in waiting functionality.
basic-browser-waiting.spec.ts
page.waitForSelector()
method.
This method waits for an element to appear in the page. This is your bread and butter and should be used whenever something
needs to be loaded after clicking, hovering, navigating etc. You can pass it an object with a timeout
attribute
to override the default 30 seconds.
This works exactly the same for the page.waitForXpath()
function is you are using XPath selectors instead of CSS selectors.
Timeouts
Thepage.waitForNavigation()
method — but also similar methods like page.reload()
and page.goBack()
— all take some
options that determine “how” it should wait and what the timeout limits are.
These options come in two flavors:
1. Hard timeout
The time in milliseconds passed as the timeout
property e.g.
page.waitForNavigation({ timeout: 2000 })
. We do not recommend
using this if you do not explicitly need to.
2a. DOM event based
These two options are directly related to the events your browser emits when it has reached a certain loading stage.
load
: This is the default and very strict: your whole page including all dependent resources, i.e. images, scripts, css etc.domcontentloaded
: less strict: when your HTML has loaded.
networkidle0
: consider navigation to be finished when there are no more than 0 network connections for at least 500 ms.networkidle2
: consider navigation to be finished when there are no more than 2 network connections for at least 500 ms.
networkidle0
and networkidle2
and just has:
networkidle
: consider navigation to be finished when there are no more than 0 network connections for at least 500 ms.
waitUntil
property, e.g. page.waitForNavigation({ waitUntil: 'networkidle2' })
Which of these options is useful to you depends on your situation:
- Does your SPA need to be fully rendered and finish all XHR calls? Go with
load
- You server render and load in some non-crucial element in a lazy fashion? go for one of the
networkidle
variant.