Web automated testing learns this trick, get off work at least one hour earlier

♥ 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?

picture

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

  1. Do web automation testing

  2. Take screenshots of web pages

  3. web crawler

What are the headless browsers?

  1. Chrome Headless, Chrome supports from version 59

  2. Firefox Headless, Firefox supports from version 56

  3. PhantomJS, a headless browser written in JavaScript, supports Windows, macOS, Linux

  4. Splash, a headless browser written in Python, using WebKit as the engine

  5. HtmlUnit, a headless browser written in Java, uses the Rhino engine as the engine

  6. ...

  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  

[Updated] The most detailed collection of practical tutorials for automated testing of Python interfaces taught by station B (the latest version of actual combat)_哔哩哔哩_bilibili [Updated] The most detailed collection of practical tutorials for automated testing of Python interfaces taught by station B (actual combat) The latest version) has a total of 200 videos, including: 1. [Interface Automation] The current market situation of software testing and the ability standards of testers. , 2. [Interface Automation] Fully skilled in the Requests library and the underlying method call logic, 3. [Interface Automation] interface automation combat and the application of regular expressions and JsonPath extractors, etc. For more exciting videos, please pay attention to the UP account. https://www.bilibili.com/video/BV17p4y1B77x/?spm_id_from=333.337&vd_source=488d25e59e6c5b111f7a1a1a16ecbe9a

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.

Guess you like

Origin blog.csdn.net/caixiangting/article/details/132261647