♥ Foreword
Everyone knows that when we execute web automation testing through Selenium, we need to start/close the browser every time. If it is multi-threaded execution, multiple browsers will be opened at the same time, which will affect the normal work. So is there a way to avoid letting the browser's automated execution interfere with our work?
Headless browser
A headless browser is a web browser without a graphical interface.
It can provide an environment similar to a common web browser, but it runs through command line or network communication.
What can a headless browser do?
At present, the main application scenarios of headless browsers are as follows
-
Do web automation testing
-
Take screenshots of web pages
-
web crawler
What are the headless browsers?
-
Chrome Headless, Chrome supports from version 59
-
Firefox Headless, Firefox supports from version 56
-
PhantomJS, a headless browser written in JavaScript, supports Windows, macOS, Linux
-
Splash, a headless browser written in Python, using WebKit as the engine
-
HtmlUnit, a headless browser written in Java, uses the Rhino engine as the engine
-
...
If you want to learn automated testing, here I recommend a set of videos for you. This video can be said to be the first interface automation testing tutorial on the entire network at station B. At the same time, the number of online users has reached 1,000, and there are notes to collect and various Lu Dashen Technical Exchange: 798478386
Operate Chrome headless with Selenium
On Mac and Linux platforms, Chrome supports headless mode since version 59. On the Windows platform, it is only supported from version 60.
https://developers.google.com/web/updates/2017/04/headless-chrome
Selenium WebDriver can control Chrome Headless through API, which is very simple to use:
ChromeOptions options = new ChromeOptions();
//设置无头模式
options.addArguments("--headless");
ChromeDriver driver = new ChromeDriver(options);
Operate Firefox headless with Selenium
Starting with Firefox 56, all platforms (Windows, Mac, Linux) support Firefox's headless mode
The following is a description from the official website:
https://hacks.mozilla.org/2017/12/using-headless-mode-in-firefox/?utm_source=testingpai.com
The code setting is also very simple
FirefoxOptions options = new FirefoxOptions();
options.addArguments("-headless");
FirefoxDriver driver = new FirefoxDriver(options);
Using Selenium to operate PhantomJS
It should be noted that the old version of Selenium can support PhantomJS, which is no longer supported in the newer version of Selenium
UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
So Selenium officially recommends using the headless browser mode of Chrome or Firefox.