Did you know that Chrome now ships with the feature Headless Chrome it is a tool for automated testing. For example, you may want to run some tests against a real web page, create a PDF of a page, or just inspect how the browser renders an URL, you can use these techniques to test your Oracle APEX applications.
What is Headless Chrome
Headless Chrome allows you to run the browser from the command line without actually opening a Chrome window, it is also possible to watch your script running in the browser.
Headless Chrome has a rather low-level API, it’s preferred to access it via a library. For this example I am using Puppeteer. Puppeteer is maintained by the Chrome DevTools team.
To use Puppeteer you need to have Node.js installed. You can find out how to do it here. Puppeteer requires at least Node v6.4.0, but the examples below use async/await which is only supported in Node v7.6.0 or greater.
You can check by using the following command in your terminal:
Create yourself a directory where you want Puppeteer installed. Open a terminal inside the directory you just created and Install Puppeteer using the following command in your terminal.
The installation may take a couple of minutes. Because it will automatically download a version of Chromium(browser) for you. So you don’t have to setup and maintain a local instance of Chrome manually.
How to run the script
Create yourself a .js file with code of one of the examples. You need to be in the same folder as the file in order to run the code.
Taking screenshots with puppeteer
When Puppeteer is taking a screenshot it will simulate a browser window with the desired width. If the page you are testing is responsive, You will see a snapshot of what it looks like at that viewport. You can change its dimensions via the setViewport method.
Running this script will generate 4 images inside the folder of the script.
Interacting with Items on the page
With Puppeteer you can access all the elements on the page. Using Puppeteer you can scrape websites, test links, validate forms, etc.
By default headless mode is activated, you have to turn it off in your script. When you run your script it will now open Chromium.
in this example you log into the application and will add a customer and turn headless mode off so that you can see what’s going on.