Headless browser testing means testing the UI of an application without actually opening up the browser. The program uses the HMTL and CSS files of the app to determine exactly what is on the page without rendering it.
Why would you want to test your application headlessly? Because without waiting for a webpage to load, your tests will be faster and less flaky. You might think that it's impossible to actually see the results of your testing by using the headless method, but that's not true! Headless testing applications can use a webpage's assets to determine what the page will look like and take a screenshot. You can also find the exact coordinates of elements on the page.
Something important to note is that headless browser testing is not browserless testing. When you run a headless test, you are actually running it in a specific browser; it's just that the browser isn't rendering. Chrome, Firefox, and other browsers have added code that makes it possible to run the browser headlessly.
To investigate headless browser testing, I tried out three different applications: Cypress, Puppeteer, and TestCafe. All three applications can run in either regular browser mode or headless mode, although Puppeteer is set to be headless by default. I found great tutorials for all three, and I was able to run a simple headless test with each very quickly.
Cypress is a really great UI testing tool that you can get up and running in literally minutes. (I'm so excited about this tool that it will be the subject of next week's blog post!) You can follow their excellent documentation to get started: https://docs.cypress.io/guides/getting-started/installing-cypress.html. Once you have a test up and running, you can try running it headlessly in Chrome by using this command: cypress run --headless --browser chrome.
Puppeteer is a node.js library that works specifically with Chrome. To learn how to install and run it, I used this awesome tutorial by Nick Chikovani. I simply installed Puppeteer with npm and tried out his first test example. It was so fun to see how easy it is to take a screenshot headlessly!
Finally, I tried out TestCafe. To install, I simply ran npm install -g testcafe. Then I created a basic test file with the instructions on this page. To run my test headlessly, I used this command: testcafe "chrome:headless" test1.js.
With only three basic tests, I barely scratched the surface of what these applications can do. But I was happy to learn just how easy it is to set up and start working with headless browser tests. I hope you find this helpful as you explore running your UI tests headlessly.