Summary
We covered one of the most powerful features of Puppeteer in this chapter. Most web automation tools let you run JavaScript code somehow, but Puppeteer makes it super easy to implement.
We started this chapter by talking about some basic JavaScript concepts. We learned about variable scopes and closures. That helped us understand how variables and closures work (or don't work) in Puppeteer. If you learned those differences, you will be able to answer 20% of the Puppeteer questions on Stack Overflow.
Then, we learned about JSHandles
and ElementHandles
. You don't see these classes being used a lot by the community, but they are very helpful if you know how to use them, and now you know.
The waitForFunction
completed our "wait" toolbox. You will use that wait function a lot. We also learned how to expose functions and listen to HTML changes using MutationObserver
. Exposing functions and listening to HTML changes is not used much in UI testing, but...