Waiting for page events
Events are messages that a class sends when something happens. As a consumer, you can attach a function to those events, so you can listen to those events and react accordingly. You can find the code examples of these demos in the page-event-demos.js
file inside the Chapter5
directory. To run that demo, you just need to run node page-event-demos.js
.
This is how you could listen to responses without the waitForResponse
:
page.on('response', response => console.log('Response URL: ' + response.url())); await page.goto('https://www.packtpub.com/');
In the first line, we say that we want to listen to the response
event, and when a new response arrives, we want to print the URL in the console. Then, we call the goto
function, and all the responses will start being written in the console.
Using the arrow (=>
) is a simple way to write single-line functions. But, if you open a bracket, you can write more complex...