1. 程式人生 > 其它 >基於Selenium Grid搭建自動化並行執行環境

基於Selenium Grid搭建自動化並行執行環境

每天進步一點點,關注我們哦,每天分享測試技術文章

本文章出自【碼同學軟體測試】

碼同學公眾號:自動化軟體測試,領取資料加:Matongxue_8

碼同學抖音號:小碼哥聊軟體測試

 

Selenium Grid元件專門用於遠端分散式測試或併發測試,通過併發執行測試用例的方式可以提高測試用例的執行速度和效率,解決介面自動化測試執行速度過慢的問題

它允許 Selenium 測試指令碼將命令通過hub路由到遠端 Web 瀏覽器。它的目的是提供一種在多臺機器上並行執行測試的簡單方法。使用 Selenium Grid,一臺伺服器充當樞紐,將測試命令路由到一個或多個註冊的 Grid 節點。hub有一個註冊伺服器列表,它提供訪問許可權,並允許控制這些node例項。Selenium Grid 允許我們在多臺機器上並行執行測試,並集中管理不同的瀏覽器版本和瀏覽器配置(而不是在每個單獨的測試中)。

selenium grids官網地址如下:

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

 

01   啟動hub

 

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

除錯指令碼

 

02   啟動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,表示node已經在hub上完成了註冊。

 

03   除錯指令碼

 

1、chrome

如下程式碼塊,首先設定chrome配置資訊“瀏覽器名稱”、“作業系統名稱”、“驅動檔案路徑”

RemoteWebDriver類物件初始化時使用hub地址以及chrome配置資訊物件,死等待3秒後將chrome瀏覽器視窗最大化,然後訪問網址:"http://www.mtxshop.com:3000/",最後關閉瀏覽器視窗。

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();}

執行後能夠成功訪問"http://www.mtxshop.com:3000/"

 

免費領取 碼同學軟體測試 課程筆記+超多學習資料+完整視訊+最新面試題,可以轉發文章 + 私信「碼同學666」獲取資料哦

 

2、firefox

如下程式碼塊,首先設定firefox配置資訊“瀏覽器名稱”、“作業系統名稱”、“驅動檔案路徑”

RemoteWebDriver類物件初始化時使用hub地址以及chrome配置資訊物件,死等待3秒後將chrome瀏覽器視窗最大化,然後訪問網址:"http://www.mtxshop.com:3000/",最後關閉瀏覽器視窗

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();}

執行後能夠成功訪問"http://www.mtxshop.com:3000/"。

 

04   分散式併發測試

 

testng+selenium grid3分散式併發測試。

testng配置檔案配置如下所示:

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

2個執行緒並行執行MyChrome和MyFirefox這兩個測試類。

同時啟動chrome和firefox瀏覽器,最大化視窗,訪問網址:

“http://www.mtxshop.com:3000/”,關閉視窗。

tp://www.mtxshop.com:3000/”,關閉視窗。

 

END

免費領取碼同學軟體測試課程筆記+超多學習資料+學習完整視訊,可以關注我們公眾號哦:自動化軟體測試

本文著作權歸作者所有,任何形式的轉載都請聯絡作者獲得授權並註明出處。