What is Headless Browser and Headless Browser Testing?
By Sanghita Ganguly, Community Contributor - August 27, 2023
Headless, as the name suggests, means a Web Browser without a User Interface. Headless browser testing increases the efficiency of testing your web applications and provides ease of testing those apps. Let us learn what a headless browser is, its advantages, and why headless browser testing is essential.
What is a Headless Browser?
We know that any software’s User Interface or UI is its most integral part. So, when it comes to Headless Browsers (Yep! You heard it right, it is called “headless”), it means a browser without a user interface or “head.” So, when the browser is headless, the GUI is hidden. Therefore, you can’t see anything when you use a headless browser to access any website. However, the program runs in the background.
- To elaborate, Headless Browser is just like any other browser, with the only difference being that nothing is visible on the screen.
- A headless browser is similar to a normal browser that performs functions such as navigating pages, clicking links, downloading content, and many more.
- But, with a normal browser, you can check each step and proceed with the help of a GUI presentation. At the same time, you will use Command-line or Console Interface to keep track of changes.
Headless Browser Examples
- Chromium
- Headless Chrome
- Firefox Headless
- Apple Safari (Webkit)
- Splash
- PhantomJS
- Zombie.JS
- HTML Unit
Importance Of Headless Browser
One clear advantage while using headless browsers is that they are faster than your typical browsers, as you can bypass all the time you take to load the CSS. But this is just one advantage.
Other advantages include:
- Scraping Websites: You can scrape the HTML of a website without rendering the full browser.
- Shorter Development Time: Checking the code changes for websites from the command line saves developers time and effort.
- Performance Monitoring with Headless Scripts: You can monitor the performance of network applications using headless browsers. Many developers automate screen capture of the website image to check the layouts of their website.
Advantages and Disadvantages of Headless Browser
Advantages of Headless Browsers
- Resource Efficiency: Headless browsers don’t require the resources to render and display web content, such as graphical elements and animations. This makes them suitable for environments with limited resources.
- Speed: Without rendering and displaying content, headless browsers can often load and interact with web pages more quickly than their GUI counterparts.
- Scalability: They are easier to scale since they can run in the background without consuming graphical resources.
- Automation: Ideal for automating interactions with web pages, such as filling out forms, clicking buttons, and navigating through pages.
Disadvantages of Headless Browsers
- Lack of Visual Feedback: One of the most significant drawbacks of headless browsers is the need for a visual interface, making debugging and troubleshooting challenging.
- Limited JavaScript Interactions: Some complex websites heavily rely on JavaScript to load and display content dynamically. Headless browsers have limitations in handling these dynamic interactions, leading to incomplete or inaccurate rendering.
- Compatibility Issues: Headless browsers cannot replicate the behavior of real user agents or GUI-based browsers. This can lead to compatibility issues, where a website functions differently or behaves unexpectedly in a headless browser.
- Maintenance: They require updates and maintenance to keep up with web technology changes and fix bugs or vulnerabilities like any other software.
What is Headless Browser Testing?
Developers have been using UI-driven testing to ensure the proper working of their programs for quite some time now. However, there are many problems with UI-driven testing. The biggest of them is stability. Sometimes UI-driven testing will fail to interact with the browser. The other problem is the prolonged performance that you will encounter. The answer to this problem is headless browser testing.
With headless browser testing, you will perform end-to-end tests where the browser will not load the application’s user interface. Therefore, everything runs faster, and the tests interact with the page directly, eliminating any chances of instability. Your tests become more reliable, faster, and efficient.
When to Use Headless Browser Testing?
Depending on your testing goals, you can choose headless browser testing for your project. It is not resource-intensive, has scripted automation, is relatively weightless, and allows rapid execution. Moreover, you can write a UI test and integrate it with the build process instead of checking each of them manually, giving you better results.
Some cases where you may use headless browser testing are:
- Automation of HTML responses like form submission, mouse-clicking, etc.
- Handling JavaScript execution
- Scraping the content from the website
- Monitoring the network
- Handling Ajax Calls
- Generating screenshots of webpages
This was just the tip of the iceberg, and many more use-cases exist. However, you should know that headless browser testing has its usage, whereas normal testing has its own. Use the combination of the two to ensure you get the best of both worlds.
Learn More: Web Application Testing: A Detailed Guide
Frameworks used for Headless Browser Testing
Let’s delve deeper into Selenium, Playwright, and Puppeteer in the following sections:
1. Selenium
Selenium is a free and open-source tool that is great for automation. It supports various browsers that run on different operating systems. Selenium web driver delivers enhanced support to dynamic web pages, and using Selenium headless can deliver great results. Moreover, you can use either Headless Chrome or Headless Firefox to execute the headless browser Selenium.
- Using Chrome for Selenium: With Chrome, you need Selenium WebDriver, which will provide you with a class called “ChromeOptions”. This class can provide headless configurations to your Chrome. Use the below code to create an instance for ChromeDriver.
ChromeOptions options = new ChromeOptions() options.addArgument("headless"); ChromeDriver driver = new ChromeDriver(options);
With the addArgument() method, you can execute the headless mode from the ChromeOptions class delivered by the Selenium WebDriver.
- Using Firefox for Selenium: The process is similar to that of Headless Chrome. But here, we will be setting a path for Gecko Driver to execute the headless mode. With the Selenium WebDriver, we will be issuing the setHeadless (true) of the class FirefoxOptions. Use the below code for running Headless mode in Firefox.
FirefoxOptions options = new FirefoxOptions(); options.setHeadless(true); WebDriver driver = new FirefoxDriver(options); driver.get("https://demoqa.com/"); System.out.println("Title of the page is -> " + driver.getTitle()); driver.close(); } }
2. Playwright
Playwright, for automating several browsers, has a high level of APIs. Before you set up Playwright for headless browser testing, ensure you have the latest versions of Node.JS and npm on your system. Create a package for this project using the command.
npm init --yes
You would be having package.json and run the given command to install Playwright on your system
npm install playwright@0.13.0
Let us now take a look at a script to run that on a test website
const playwright = require('playwright'); (async () => { const browser = await playwright.chromium.launch(); const page = await browser.newPage(); await page.goto('https://native-land.ca/'); await page.screenshot({ path: 'example.png' }); await browser.close(); })(); Or you can also use the following script- import { test, expect } from '@playwright/test'; test('basic test', async ({ page }) => { await page.goto('https://playwright.dev/'); const title = page.locator('.navbar__inner .navbar__title'); await expect(title).toHaveText('Playwright'); });
Now run the following command as an example
npx playwright test
Moreover, we recommend you disable the headless mode when you write your first headless script. You can see what your script is doing by disabling the headless mode.
Follow-Up Read: Playwright Framework Tutorial
3. Puppeteer
Puppeteer is undoubtedly one of the most popular headless frameworks out there. Similar to Playwright it comes with its own browser profile. Create a script to navigate to your test website by using the following code:
const puppeteer = require('puppeteer') ;(async () => { const browser = await puppeteer.launch() const page = await browser.newPage() await page.goto('https://bstackdemo.com/') await browser.close() })()
Remember that by default, Puppeteer is running on headless mode, so you won’t see anything happening in the browser even if you are running your script.
Note: You should know that Playwright and Puppeteer will be resetting their states at the end of every session, and no runs will be interfering with each other.
How to Execute Headless Browser Testing?
You can enable Headless Browser Testing using codes in different browsers. Here’s an example using Cypress:
If you wish to execute headless mode in Cypress, execute the command
cypress run
If you are looking to run only one specific document, you can pass that as an argument by using the “cypress run” command as given below,
cypress run --spec cypress/integration/example.spec.js
Below is a test script
{ "name": "sample-project", "version": "1.0.0", "description": "Sample project for the Pinches of Cypress series", "main": "index.js", "scripts": { "test": "cypress run" }, "keywords": ['cypress.io', 'testing', 'cypress'], "author": "Walmyr Filho <wlsf82@gmail.com> (https://walmyr.dev)", "license": "MIT", "devDependencies": { "cypress": "^6.4.0" } }
You can now have as many scripts as you want. For example
"scripts": { "cypress:open": "cypress open", "cypress:ci": "cypress run", "cypress:smoke-test": "cypress run --spec cypress/integration/smoke-test.spec.js" },
If the above commands are not npm scripts, adding npx as a prefix is necessary to execute them.
Wrapping It Up
Headless Browser Testing is a faster, more reliable, and more efficient way of testing your web applications on a browser. However, a real desktop browser provides a real representation of your website when you are using it for testing. So, you can switch between real and headless browsers depending on the use and scenario for better results.
Automation testing with Puppeteer allows testers to achieve precisely this, creating better applications in shorter durations. By integrating Puppeteer with BrowserStack, QAs can expand test coverage by running parallel tests.