📜 ⬆️ ⬇️

Headless testing in the browser. Advantages and disadvantages

Hello again. This publication is timed to the launch of the “Automation of Web Testing” course, which starts this week. Enjoy reading!

What is headless testing?

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


Why you may 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 a headless browser?

To follow the money is a cliche, but I still think that this is a key indicator of the emergence of trends, and therefore of 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 the people at Sause are smart people. They will not develop anything just like that. They need to get feedback from users to understand that any functionality is necessary.

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

Automation in software development

I am all for "automation", 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 conventional browsers is their speed. The fact is that when you load a page in a browser without a GUI, it doesn’t have to waste time loading CSS, JavaScript and rendering HTML.

I would not say that the difference would be enormous, but in some cases you can trace the speed increase by a factor of two or even 15. Thus, if performance is important to you, then a headless browser would 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 start 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 comparison on various sites.

Since you only need data, it does not make sense to run the full browser essence and unnecessarily load the machine, because the lower the load, the faster you will get the result. This may not necessarily be testing, you just need to use the right tool for the job. In general, I believe that testers in vain underestimate the headless browsers in their business.

Thus, if you need to get data from a web page, you do not need to open it and give an additional load, opening the 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 Frizel , the author of the PhantomJS CookBook, in an interview with TestTalks, explained why its developers use the headless browser PhantomJS:
“Although PhantomJS itself is not a testing framework, but it is a useful“ ray of light in a coal mine, ”which gives confidence; if all tests are passed, then you become much more confident that your code is really good. ”
Performance Monitoring with Headless Browser Scripts

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

Some even use them to automate the rendering and get screenshots of the website screen to test the markup in an automated way.

I think this is one of the reasons Google has developed a new headless Chrome API called Puppeteer . It is aimed primarily 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 now tell you about:

  1. Run tests on a machine without a monitor;
  2. Data setting;
  3. SSL testing;
  4. Simulation of multiple browsers on the same machine;
  5. Running tests on a headless system, such as Linux without a GUI;
  6. Receive and visualize PDF files;
  7. Testing markup, since headless browsers can render HTML and CSS as full browsers, so you can test styles with them.

Cases where you do not want to use the Headless browser

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

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

Popular Headless Browsers

  1. Google Puppeteer - Headless Browser Puppeteer is a Node library. It provides a high-level API for managing headless Chrome or Chromium using the DevTools protocol. It can also be configured to use high-grade (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 format, Canvas, and SVG. * Now it is not supported. Because of this, you may not want to use it.
  5. HtmlUnit is a browser without 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. Also, as in your "normal" browser.
  6. Splinter - Splinter is a Python-oriented headless 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 - Programmable, embeddable web browser driver, compatible with Selenium WebDriver spec - headless, based on WebKit, on pure Java.

When should I 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 issues - it means better ”

As we know, every position will find supporters and there will be opposition, but here it is only the question of choosing the right tools for solving various tasks.

Remember that you must use the best tool for the job and always ask yourself how it will affect the end user, ask yourself what the purpose of the test is 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 remind you that within a few hours an open door will take place, where you can learn more about our course.

Source: https://habr.com/ru/post/444248/


All Articles