Headless browser testing is becoming more popular in software testing these days. With Chrome and Firefox having launched their versions with support for headless flags, it has become even much easier. In this blog, let’s explore what exactly is ‘Headless browser testing’, its benefits and tools available for headless browser testing.
The ultimate aim of test automation is to increase the speed of testing. We use tools such as Selenium to automate tests for web applications to run on real browsers such as Chrome, Firefox, Safari, etc to make sure the application’s users will experience a flawless experience. But running your tests on these real browsers have some limitations or challenges.
- You cannot run these tests on any remote Unix/Linux servers since these tests always need a display for execution.
- The GUI and other features in a real browser that enriches the browsing experience actually make them heavier, more resource intensive, and slower to execute your automated commands.
- It’s quite difficult and complex to scale a large number of real browser tests execution in a Continuous Integration environment. This is because; to run the tests on CI servers you need additional configuration for a display server.
The solution for the challenges mentioned above is to use a ‘Headless Browser’ for running your automated tests. So, what is a headless browser?
What is Headless Browser?
Some of the popular uses for headless browsers are:
- Test automation of web applications
- Take screenshots of web pages
- Scraping websites for data
There are many headless browsers providing a complete or near-complete headless implementation. Some of the popular ones are- Google Chrome (since version 59), Firefox (since version 55), PhantomJS, HtmlUnit, Splash, Nightmare, TrifleJS, etc.
Headless browser testing
Another use case for using headless browsers is when you need to scrape some website or extract data to help you with a test. For scraping a website, you need not render a full browser; rather you can go to it heedlessly and just scrape the HTML. When you use headless you have less overhead, and hence you will get the results quicker.
Thus, headless browsers save a lot of time in testing, especially for developers while unit testing their code changes for websites and mobile apps. It is a quick way to ensure that your code is ok without spending a lot of time is testing. Headless browsers can even be used to automate the rendering and screen capturing of website images to perform layout checks in an automated way.
Besides the ones we have discussed so far, headless browsers are a great solution for many other instances like running tests on machines without display, testing SSL, simulating multiple browsers on a single machine, running tests on headless systems like Linux OS without GUI, retrieve and render PDFs, etc.
Thus, automated headless browser testing stands out to be a great tool for testing software quickly across various environments, and covers the majority of your software testing needs. However, you cannot use it if you need to visually see the test run or you need to mimic real users. And at the end of the day, your real users wouldn’t be using headless browsers anyway. So, to ensure the complete quality of your application, you cannot solely depend on headless browser testing. You would have to go with running your tests on real browsers itself. But, like I already mentioned before, it is a great time saver for developers who want to run unit tests and can even be used for running smoke tests in production quickly. So, choose the options based on your test goals.
Tools for Headless Browser testing
It was only recently, about a year back, that both Chrome and Firefox added support for a native headless mode. Before that, for headless testing, we had to use open source headless browsers such as PhantomJS or ZombieJS. These browsers were not up to date and many times did not support the latest web platform features that real users use. But, with Chrome and Firefox having added headless mode, you can easily get real browsers running with fast startup.
There are various browser engines and libraries out there to help you with headless browser testing these days. Here is a list of some of the most popular headless engines and libraries which are used for headless browser testing.
- Headless Chrome: Chrome version 59 or higher allows you to work in headless mode. It is a lightweight headless browser and saves a lot of memory and enhances performance. Headless Chrome provides a number of tools that can help you do various tasks in the headless mode.
- Headless Firefox: Headless testing is offered by Mozilla for version 56 of Firefox.
- Nightmare: Nightmare is a high-level browser automation library built as an easier alternative to PhantomJS. It runs on the Electron engine.
- HtmlUnit: HtmlUnit is a "GUI less browser for Java programs". Written in Java, HtmlUnit allows you to use Java code to automate many of the basic ways in which users interact with websites.
- Puppeteer: Puppeteer is an easy to use Node.js library to control headless chrome Node API from the Chrome DevTools team.
- Selenium WebDriver: Not to forget the most popular test web testing automation tool out there. It can very well be used to run your tests in headless browsers like HTMLUnit, PhantomJS, Headless Chrome, Headless Firefox, etc.
That was all about headless browser testing. Hope the article helped you get some idea on what is headless browser testing all about. Enjoy automating headless!