Headless testing in a browser. Pros and cons

Original author: https://www.joecolantonio.com
  • Transfer
Hello again. This publication is dedicated to the launch of the Automation of Web Testing course , which starts this week. Enjoy reading!

What is headless testing?

Headless testing is a launch in the browser of a test for the performance of any components of the UI without displaying this UI itself. Such testing launches scripts in the browser, but does not launch and does not show the UI by itself.



Why might you need to use Headless browsers? This approach has many pros and cons. Using a headless browser may not be very useful for surfing the net, but it is indispensable for automating tasks and testing.

Why use headless browser?

Following the money is a cliche, however, I still think that this is a key indicator of the emergence of trends, and therefore what you should definitely pay attention to. For example, Sauce Labs has just released a new service called Sauce Headless, a cloud-based solution for headless testing your solutions.

I know that people in Sause are smart people. They will not develop anything for nothing. They need to get feedback from users in order to understand that any functionality is necessary.

I am sure that they will not be alone in their quest to develop headless testing.
As we move further through the software development life cycle, testers need to provide feedback to developers faster and faster. One way to achieve this is to conduct quick checks with a headless browser.

Automation in software development

I am in favor of automation with all my hands, but automation is not only about testing.
This is a story about all aspects that you can automate to save time or effort in the life cycle of software development, quality control or testing, DevOps or installation. Headless browsers are one of the powerful automation tools.

Headless browsers are faster than regular browsers

One of the most important differences between headless browsers and regular browsers is their speed. The fact is that when you load a page in a browser without a GUI, it does not need to waste time loading CSS, JavaScript and rendering HTML.

I would not say that the difference will be enormous, but in some cases you can track the increase in speed by half or even 15. Thus, if performance is important to you, then a headless browser will be a good choice.

Scraping with a headless browser

Another advantage of headless browsers is their ability to collect data from websites . To do this, you do not have to manually launch the browser. You can use headless technology to simply scrap HTML. And rendering the entire browser is completely optional.

Let's say that your case requires sports statistics or price comparisons on various sites.

Since you only need data, it makes no sense to launch a full-fledged essence of the browser and unnecessarily load the machine, because the less load, the faster you get the result. It may not necessarily be testing, you just need to use the right tool to work. In general, I believe that testers in vain underestimate headless browsers in their field.

Thus, if you need to get data from a web page, you do not need to open it and give extra load by opening a browser, all the necessary functionality is able to provide a headless browser.

Save developer time

I know that many developers use headless browsers to unit test their sites and applications. The ability to use it from the command line without manually updating or launching the browser saves them a lot of time and effort. For example, Rob Frizzel , the author of PhantomJS CookBook in an interview with TestTalks explained why his developers use the headless PhantomJS browser:
“Although PhantomJS itself is not a testing framework, it is a useful“ ray of light in a coal mine ”that gives confidence; if all the tests are passed, then you will become much more confident that your code is really good. ”
Monitoring Performance Using Headless Browser Scripts

Another option for using a headless browser is to monitor the performance of network applications.

Some even use them to automate rendering and take screenshots of a website’s screen in order to test layouts in an automated way.

I think this is one of the reasons Google has developed a new headless Chrome API called Puppeteer . It is primarily aimed at helping developers.

Key ideas Headless testing browser

In addition to the previous idea, there are also other options for using headless browsers, which I will tell you about now:

  1. Run tests on a machine without a monitor;
  2. Data setting;
  3. SSL testing;
  4. Simulation of multiple browsers on one machine;
  5. Running tests on a headless system, such as Linux without a GUI;
  6. Receiving and visualization of PDF files;
  7. Markup testing, since headless browsers can render HTML and CSS as full-fledged browsers, so you can test them with styles.

Cases When You Don't Want To Use A Headless Browser

Of course, there are times when you might want to use a full browser, instead of a headless browser. For instance:

  1. You need to simulate real users;
  2. You need to see how the test works;
  3. If you need to debug, this can be tricky with a headless browser.

Popular Headless Browsers

  1. Google Puppeteer - Headless Browser Puppeteer is a Node library. It provides a high-level API for controlling headless Chrome or Chromium using the DevTools protocol. It can also be configured to use full-fledged (non-headless) Chrome or Chromium.
  2. Google Chrome from version 59;
  3. Firefox versions 55 and 56;
  4. PhantomJS - headless webkit on JavaScript API. It has fast and native support for various web standards: DOM tree processing, CSS selectors, JSON, Canvas and SVG format. * Now it is not supported. Because of this, you may not want to use it.
  5. HtmlUnit is a browser without a GUI for Java code. It models HTML documents and provides an API that allows you to open pages, fill out forms, click on links, etc. As well as in your "normal" browser.
  6. Splinter - Splinter is a headless Python-oriented browser. It is open source and is used to test web applications using Python. For example, you can use it to automate browser actions, such as visiting web pages and interacting with their elements.
  7. jBrowserDriver - A programmable, embedded web browser driver compatible with the Selenium WebDriver spec - headless, based on WebKit, in pure Java.

When should you use a headless browser for testing?

The answer to this question depends on the purpose of your testing.

On the one hand, people often say: “Never use headless browsers. A real user will never use it, so why should you? ” Although, on the other hand, we hear: “You should always use a headless browser, because it is faster, and faster in testing matters, it is better.”

As we know, each position will have supporters and opposition, but here it is only a matter of choice the right tools for solving certain problems.

Remember that you should use the optimal tool for the job and always ask yourself how this will affect the end user, you should ask yourself what is the purpose of testing every time you make a choice between these two points of view.

According to the established tradition, we are waiting for your comments, and also recall that in a few hours there will be an open day , where you can learn in detail about our course.

Also popular now: