BreadcrumbHomeResourcesBlog Headless Browser Testing With Selenium & Python September 12, 2021 Headless Browser Testing with Selenium & PythonOpen Source AutomationBy Eliran ShaniHeadless browser testing is critical. Why?I assume you’ve all heard Google’s announcement from last year about shipping Headless Chrome in Chrome 59. If not, it’s time to catch up, because since then, headless testing has been available through Chrome. As Google put it, it’s like “running Chrome without Chrome!”. This provides testers with all the speed advantages of the Chrome V8 engine, but without having to use a GUI.In this blog post, I will show you how to do headless browser testing on Chrome with Selenium WebDriver, Python and Allure. But first, let’s learn a bit more about the pros and cons of headless browser testing in general, and Chrome headless in particular. Table of ContentsWhat is a Headless Browser?What is Headless Browser Testing?Headless Browser Testing Pros and ConsHow to Run Headless Browser Testing With Selenium & Python on ChromeRunning Your Headless Browser Tests in BlazeMeterTable of Contents1 - What is a Headless Browser?2 - What is Headless Browser Testing?3 - Headless Browser Testing Pros and Cons4 - How to Run Headless Browser Testing With Selenium & Python on Chrome5 - Running Your Headless Browser Tests in BlazeMeterBack to topWhat is a Headless Browser?A headless browser is a web browser that does not contain a graphical user interface (GUI). It offers automated control of a web page similar to popular web browsers, but is operated by network communication or command-line function.Back to topWhat is Headless Browser Testing?Headless browser testing is useful for verifying text, elements, or other static data on a certain webpage. Just like a regular browser, a headless browser understands HTML and CSS. It can also execute JavaScript like AJAX requests.Generally speaking, headless Chrome browsers are web browsers that are used for testing usability and browser interactions. Since headless browsers don’t have a graphical user interface (GUI), they save resources and time. Headless browser tests are executed from a command-line interface or by using network communication, and they run the functions in the backend.Back to topHeadless Browser Testing Pros and ConsHeadless Chrome Pros1. Resource usage is reducedRendering and opening HTML, CSS, Javascript, and images take up a lot of resources. This is a huge problem especially when you are testing multiple browsers and a number of tabs. The big advantage of headless testing is that it starts performing functions even if your page hasn’t finished loading, taking up fewer resources. This will enable you to run more tests, and your tests to run faster and smoother.2. Headless browsers are fasterFor the same reason as the previous point, headless browsers are also quicker to start running tests. In my experience, this will not always be much faster, but it is faster than real browsers.3. Running headless browser tests is quicker for developersBeing able to run tests straight from the command line can save a lot of time for developers who want to verify their code quickly and don’t need GUI intervention. This way, developers, can quickly feel confident that their code didn’t break the webpage’s functionality.4. Headless is ideal for HTML scrapingIf you wish to scrape the HTML of a webpage regardless of its CSS and JavaScript, for example, if you want to scrape an HTML for future data that will be used later, headless browser testing can be a great solution. Since it’s just the data you’re looking for, it doesn’t make sense to start up a full instance of a browser.5. Headless is ideal for monitoring your network application performanceIf you wish to monitor your network application performance, headless can be a good solution for you. You can use headless testing to automate the rendering and screen capturing of your website images and perform layout checks in an automated fashion. Chrome headless API has recently developed another cool open-source tool designed to handle these types of tests, called Puppeteer.Headless Chrome ConsChrome headless has limitations compared to other headless browsers. First of all, it’s not open source. Unlike Chromium or PhantomJS, headless browsers that the community contributed to their development, Google is the only entity that will contribute to Chrome headless development. Second, since headless Chrome is rather new in the headless section, it may raise unknown issues for the community.In addition, if you are used to running your tests in regular Chrome, you will also need to adjust, and transferring your tests will require additional work and tweaking.Headless Browser Testing ConsHeadless testing itself also has disadvantages. As it does not mimic real users interactions and workflows as well as regular chromeDriver mode, it’s difficult to catch bugs that are related to crashing images, because the UI isn’t displayed. In addition, headless is not the perfect solution for debugging purposes, since the test will not be visible to you.Back to topHow to Run Headless Browser Testing With Selenium & Python on ChromeBefore we actually get started with running our headless browser tests, let’s make sure we prepare our environment properly.Here is a list of prerequisites:Python virtualenvChrome browser v59 (at least). It’s recommended to install the latest versionChromeDriver v.2.38 with the latest fixes and updates, for supporting headlessPytest - for writing your testSelenium Webdriver for Python - for interacting with the browserNow, follow these steps:Create a new folder designated for your repositoryCreate a new environment next to itMake sure to put latest ChromeDriver inside the virtual environment bin folderActivate the virtual environmentWe are now ready to run our tests! In order to run headless chrome you simply need to add the following to your chrome instance:chrome_options.add_argument("headless")According to the example given in the public repo, the chrome instance is initiated from the conftest.py file.Once that is done you can run your tests headlessly as follows:“PytestPATH_TO_TEST--headless=true” Integrating Chrome Headless with Jenkins, Slack, and AllureI integrated Allure reports and Slack notifications into Jenkins. Each night, all of our tests are executed, and I receive an Allure report and Slack notifications about the test results with links to Jenkins.On Allure you can click on the different tests to get more details about it, including screenshots, errors, trends, etc.Back to topRunning Your Headless Browser Tests in BlazeMeterAnother easy way to run your Headless test is by simply uploading your file to BlazeMeter and running it. Then, you can easily analyze results and share your tests with team members and managers. You can also manage user permissions and assign tests.This blog was originally published on May 19, 2018, and has since been updated for accuracy and relevance.START TESTING NOWBack to top
Eliran Shani Automation Engineer Eliran Shani is an Automation Engineer at BlazeMeter and holds an MBA and B.Sc in Software Engineering. Since 2009, Eliran has held multiple technological roles, including software testing, technical support, and automation engineering. In 2012, Eliran started his own venture: 'EATV' - a food ordering application directly on the SAMSUNG Smart TV. He also worked in Kaltura for three years, where he had experience with Fortune 500 Companies. Eliran is an eternal optimist and passionate about UX and creating high quality products.