Navigation
A usual web test do three main things:
- Navigate to a web page
- Interact with elements
- Check the current state
Navigating using page.goto()
One way to make the test to navigate to another wen page is using the page.goto() function.
page.goto(url);
When executed, the page.goto() function will auto-wait for the load event on the page to be triggered. There are other useful options that can be used:
await page.goto('http://www.google.com')
waits for load event (default)
await page.goto('http://www.google.com', { waitUntil: 'networkidle' } )
waits for the network traffic to be idle
await page.goto('http://www.google.com', { waitUntil: 'domcontentloaded' } )
waits for all the DOM content to be loaded
Wait for an element
On some pages, it is useful to wait for a specific element to be loaded and visible. On these cases, a page.waitForSelector(selector) is used.
await page.goto('https://example.com');
await page.waitForSelector('text=Example Domain');
Wait for async navigation
Some modern pages trigger aditional events before initiating a navigation. On these cases, a page.waitforNavigation() should be used.
await page.click('a');
await page.waitForNavigation();
Other navigation functions
These are less common navigation funcions:
Reloads the page:
await page.reload();
Navigates back and forward history
await page.goBack();
await page.goForward()