Building an automatic parallel execution environment based on Selenium Grid

Make a little progress every day, pay attention to us, and share test technology articles every day

This article comes from [code classmate software test]

Code student official account: automated software testing, receiving data plus: Matongxue_ eight

Code brother tiktok: little brother, chat with software test.

 

Selenium Grid component is specially used for remote distributed testing or concurrent testing. By executing test cases concurrently, the execution speed and efficiency of test cases can be improved, and the problem of too slow execution speed of interface automation testing can be solved

It allows selenium test scripts to route commands through a hub to a remote Web browser. Its purpose is to provide a simple method to run tests in parallel on multiple machines. Using selenium Grid, a server acts as a hub to route test commands to one or more registered Grid nodes. Hub has a list of registered servers that provide access rights and allow control of these node instances. Selenium Grid allows us to run tests in parallel on multiple machines and centrally manage different browser versions and browser configurations (rather than in each individual test).

The official website address of selenium grids is as follows:

https://www.selenium.dev/documentation/en/grid/grid_3/

 

01. Start hub

 

java -jar selenium-server-standalone-3.141.59.jar -role hub -port 4445

Debug script

 

02. Start node

 

java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.2.161:4445/wd/hub

The node is registered to the hub appears, indicating that the node has completed registration on the hub.

 

03. Debugging script

 

1,chrome

In the following code block, first set the chrome configuration information "browser name", "operating system name" and "driver file path"

When initializing the RemoteWebDriver class object, use the hub address and the chrome configuration information object. After waiting for 3 seconds, maximize the Chrome browser window, and then visit the website:“ http://www.mtxshop.com:3000/ ", and finally close the browser window.

ChromeOptions chromeOptions = new ChromeOptions();chromeOptions.setCapability("browserName","chrome");chromeOptions.setCapability("platform","WINDOWS");chromeOptions.setCapability("webdriver.chrome.driver","D:\\BrowserDriver\\chromedriver.exe");String url = "http://192.168.2.161:4445/wd/hub";try {WebDriver driver = new RemoteWebDriver(new URL(url), chromeOptions);FindElementDemo.think(3000);driver.manage().window().maximize();driver.get("http://www.mtxshop.com:3000/");driver.close();} catch (MalformedURLException e) {    e.printStackTrace();}

Successful access after execution“ http://www.mtxshop.com:3000/ "

 

Get free code classmate software test course notes + super learning materials + complete video + the latest interview questions. You can forward articles + private letter "code classmate 666" to obtain information

 

2,firefox

In the following code block, first set the firefox configuration information "browser name", "operating system name" and "driver file path"

When initializing the RemoteWebDriver class object, use the hub address and the chrome configuration information object. After waiting for 3 seconds, maximize the Chrome browser window, and then visit the website:“ http://www.mtxshop.com:3000/ ", and finally close the browser window

FirefoxOptions firefoxOptions = new FirefoxOptions();firefoxOptions.setCapability("browserName","firefox");firefoxOptions.setCapability("platform","WINDOWS");firefoxOptions.setCapability("webdriver.firefox.driver","D:\\BrowserDriver\\geckodriver.exe");String url = "http://192.168.2.161:4445/wd/hub";try {WebDriver driver = new RemoteWebDriver(new URL(url), firefoxOptions);FindElementDemo.think(3000);driver.manage().window().maximize();driver.get("http://www.mtxshop.com:3000/");driver.quit();} catch (MalformedURLException e) {    e.printStackTrace();}

Successful access after execution“ http://www.mtxshop.com:3000/ ".

 

04. Distributed concurrency test

 

testng+selenium grid3 distributed concurrent testing.

The testng configuration file is configured as follows:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"><suite name="All Test Suite" parallel="classes" thread-count="2">    <test verbose="2" preserve-order="true" name="C:/Users/18611/IdeaProjects/UIautotest20210331/UIautotest20210331">        <classes>            <class name="com.mtx.study1.MyChrome"></class>            <class name="com.mtx.study1.MyFirefox"></class>        </classes>    </test></suite>

Two threads execute MyChrome and MyFirefox test classes in parallel.

Start chrome and firefox browsers at the same time, maximize the window, and visit the website:

“ http://www.mtxshop.com:3000/ ”, close the window.

tp://www.mtxshop.com:3000 /, close the window.

 

END

Free code, students' software test notes + super learning materials + Learning complete videos, we can pay attention to our official account number: automated software testing.

The copyright of this article belongs to the author. For any form of reprint, please contact the author for authorization and indicate the source.

 

Added by rohithmr on Thu, 10 Mar 2022 03:45:06 +0200