DEV Community

Mauro Laurenzi
Mauro Laurenzi

Posted on

How to test Chrome extensions in headless mode with WebdriverIO

Image description
Hi! In this article I will try to explain, step by step, how we can load an extension in a Google Chrome browser for test automation and then run our tests in headless mode using the WebdriverIO framework.
First, let's understand what headless mode means and why it could be beneficial for running automated tests on a Chrome extension. “Headless mode” refers to executing a test with no graphical user interface (GUI), meaning that the browser works in the background, without displaying any windows or UI elements.

Why would we want to execute tests this way?

The principal reasons are speed, resource efficiency and compatibility.

  1. Speed: Tests running in headless mode tend to be faster because the test-runner doesn’t have to waste time rendering the UI, it only focuses on executing test commands.
  2. Resource efficiency: Given that UI elements don’t need to be rendered, tests running in headless mode tend to use fewer system resources like memory and CPU. This becomes particularly important when running several tests in parallel or on machines with limited resources.
  3. Compatibility: Tests can behave differently in different browsers or environments. Headless mode can help mitigate this by providing a consistent testing environment across different platforms and browsers, since there is no UI rendering involved.

How can we run automated tests in headless mode with a browser extension installed?

Now that we understand the benefits of headless mode for test automation, let’s see how we can implement this approach when testing a Chrome extension with WebdriverIO:

  1. The first thing you will need is the .dist folder containing the extension you want to test. This is a generated folder that normally contains files like a background.js, content.css, content.js, assets; and most importantly, a manifest.json that has all the necessary parameters and values for launching the extension.

  2. Once you obtain the .dist folder, you can manually make sure that it works by opening a new Chrome instance, clicking on Extensions -> Manage Extensions, turning in the Developer mode toggle, clicking Load unpacked and selecting the .dist folder. If the extension loads without any errors, then the .dist folder is ready to be used for test automation.

  3. Go to the WebdriverIO config file from your project and edit the capabilities section to look something like this:

capabilities: [
    {
        maxInstances: 1,
        browserName: "chrome",
        browserVersion: "stable",
        "goog:chromeOptions": {
            args: [
                "--load-extension=path/to/extension",
                "--disable-gpu",
                "--headless=new",
            ],
        },
    },
]
Enter fullscreen mode Exit fullscreen mode

For this to work, it’s important that you use the --headless=new flag instead of the old --headless one. For more information on how the --headless=new command flag supports test automation of Chrome extensions, unlike the old one, refer to this Chrome for Developers article.
It is also important that you use the WDIO ChromeDriver Service version 8 or above for the supported --headless=new command flag.

That 's it! you’re now ready to run your tests in headless mode with your extension installed in your Chrome browser.

Top comments (0)