2021年最后一天,學點Selenium玩點新鮮~新的一年,讓分布式測試有更多玩法

? 前 言

我們都知道 Selenium 是一款在 Web 應用測試領域使用的自動化測試工具,而 Selenium Grid 是 Selenium 中的一大組件,通過它能夠實現分布式測試,能夠幫助團隊簡單快速在不同的環境中測試他們的 Web 應用。

分布式執行測試其實并不是一個非常難以理解的概念,簡單來說,就是能夠同時在不同的環境中同時執行測試。通過 Selenium Grid,我們可以通過在客戶端發送命令到遠程端的瀏覽器實例,在遠程端執行 WebDriver 腳本,可以在多臺遠程的機器上并行運行測試。

Selenium Grid 4 新特性介紹

在以往的 Selenium Grid 版本中(V1-V3)其架構都比較簡單。在全新的 Selenium Grid 4 版本中其架構變得有些復雜,但也更加靈活和強大了。

特性一:Hub 和 Node 使用同一個 jar 服務

在之前,我們要配置 Selenium Grid 時需要分別啟動 Hub 和 Node 節點,而在全新的 Selenium Grid 4 版本中 Hub 和 Node 都是用同一個 jar 文件,一旦服務啟動之后將會同時作為 Hub 和 Node 角色。

特性二:架構優化

在之前的 Selenium 版本中,Selenium Grid 由 Hub 和 Node 組成,Hub 作為總控中心負責 Node 節點注冊及轉發請求,Node 節點則負責來自 Hub 的請求及執行測試腳本,具體參考我之前的文章 Selenium Grid- 讓自動化分布式執行變得可能

文章鏈接:http://testingpai.com/article/1596527701728

今年最后一天 學點Selenium玩點新鮮 新一年讓分布式測試有更多玩法

在 Selenium Grid 4 版本的全新架構中劃分成了組件:Router、Distributor、Node、Session Map、Session Queue、Event Bus

Router - 監聽新會話請求

Distributor - 選擇合適的 Node 執行測試

Node - 在對應主機執行測試

Session Map - 給 Node 節點標記 session ID

Session Queue - 維護所有的會話隊列

Event Bus - 作為 Grid 各組件通訊橋梁

今年最后一天 學點Selenium玩點新鮮 新一年讓分布式測試有更多玩法

特性三:不同的 Gird 運行模式

在 Selenium Grid 3 及之前版本中只能使用 Hub 和 Node 工作模式,Selenium Grid 4 除了提供對經典的 Hub 和 Node 模式支持之外,還引入了另外兩種全新的模式 standalone、fully distributed。

Standalone 模式

standalone 模式能夠在一臺機器上面執行完整的分布式功能,是 Selenium Grid 的最簡單的模式,默認情況下,服務會在 http://localhost:4444 地址監聽,我們需要通過 RemoteWebDriver 指向這個地址。

Classical 模式

classical 模式也可以稱為 hub 和 node 模式,此項模式比較適用于中小型分布式執行要求,根據需要設置一個服務節點(hub)和多個代理節點(node)。

Fully Distributed 模式

對于大型的分布式要求,可以采用完全分布式模式,這是 Selenium Grid 4 中最高級的模式,這種模式下需要自己啟動每個組件。

Standalone 模式使用

在 standalone 模式下,Node 和 Hub 都是運行在相同的 Selenium Grid 服務中

Step1:準備需要的環境

因為后續下載的 Selenium 組件是 jar 文件,所以需要準備 Java 的環境去運行。如果沒有安裝 Java 的環境,可以進入到 https://www.oracle.com/java/technologies/downloads/下載安裝 JDK 并且配置環境變量,配置完成之后進入到命令行終端,輸入以下命令檢測:

java -version

如果 Java 環境配置成功,你會看到相關的 Java 版本信息。

Step2:下載瀏覽器驅動

下一步需要根據你當前的操作系統以及瀏覽器選擇下載合適的驅動。

Chrome Driver 地址:https://npm.taobao.org/mirrors/chromedriver

Firefox Driver 地址:https://npm.taobao.org/mirrors/geckodriver

驅動下載完成后放置到某個目錄下,并將目錄添加到系統的環境變量中。在 Selenium Grid 服務啟動時將會自動識別。如果不做此操作 Selenium Grid 將無法在在這些瀏覽器上執行測試。

今年最后一天 學點Selenium玩點新鮮 新一年讓分布式測試有更多玩法

Step3:創建 Selenium Grid

這一步我們需要下載 Selenium Server 對應地址:https://www.selenium.dev/downloads/

下載完畢之后將其放置到任意的目錄中,接下來在命令行終端運行以下命令啟動 Selenium Grid

java -jar selenium-server-4.1.1.jar standalone

你將會看到下面的信息輸出,我們可以看到 Chrome 以及 Firefox 都被注冊到了 Grid 中

D:\Selenium Grid>java -jar selenium-server-4.1.1.jar standalone

16:59:31.406 INFO [LogManager$RootLogger.log] - Using the system default encoding

16:59:31.409 INFO [OpenTelemetryTracer.createTracer] - Using OpenTelemetry for tracing

16:59:37.876 INFO [NodeOptions.getSessionFactories] - Detected 8 available processors

16:59:38.046 INFO [NodeOptions.discoverDrivers] - Discovered 2 driver(s)

16:59:38.071 INFO [NodeOptions.report] - Adding Firefox for {"browserName": "firefox"} 8 times

16:59:38.072 INFO [NodeOptions.report] - Adding Chrome for {"browserName": "chrome"} 8 times

我們可以通過訪問 http://localhost:4444/status 看到當前的狀態

{

? "value": {

? ? "ready": true,

? ? "message": "Selenium Grid ready.",

? ? "nodes": [

? ? ? {

? ? ? ? "id": "4bb7d6f7-466d-4537-87ca-1441023e0fff",

? ? ? ? "uri": "http:\u002f\u002f192.168.56.1:4444",

? ? ? ? "maxSessions": 8,

? ? ? ? "osInfo": {

? ? ? ? ? "arch": "amd64",

? ? ? ? ? "name": "Windows 10",

? ? ? ? ? "version": "10.0"

? ? ? ? },

? ? ? ? "heartbeatPeriod": 60000,

? ? ? ? "availability": "UP",

? ? ? ? "version": "4.1.1 (revision e8fcc2cecf)",

? ? ? ? "slots": [

? ? ? ? ? {

? ? ? ? ? ? "id": {

? ? ? ? ? ? ? "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",

? ? ? ? ? ? ? "id": "b8a3c42e-c8fb-493c-bb3c-7b6deaa2a02a"

? ? ? ? ? ? },

? ? ? ? ? ? "lastStarted": "1970-01-01T00:00:00Z",

? ? ? ? ? ? "session": null,

? ? ? ? ? ? "stereotype": {

? ? ? ? ? ? ? "browserName": "firefox",

? ? ? ? ? ? ? "platformName": "WIN10"

? ? ? ? ? ? }

? ? ? ? ? },

? ? ? ? ? {

? ? ? ? ? ? "id": {

? ? ? ? ? ? ? "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",

? ? ? ? ? ? ? "id": "0eaffe11-62a1-4593-a94f-80151a69c24b"

? ? ? ? ? ? },

? ? ? ? ? ? "lastStarted": "1970-01-01T00:00:00Z",

? ? ? ? ? ? "session": null,

? ? ? ? ? ? "stereotype": {

? ? ? ? ? ? ? "browserName": "chrome",

? ? ? ? ? ? ? "platformName": "WIN10"

? ? ? ? ? ? }

? ? ? ? ? },

? ? ? ? ? {

? ? ? ? ? ? "id": {

? ? ? ? ? ? ? "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",

? ? ? ? ? ? ? "id": "7c7211bf-58a7-4ddb-9246-effecbf910ce"

? ? ? ? ? ? },

? ? ? ? ? ? "lastStarted": "1970-01-01T00:00:00Z",

? ? ? ? ? ? "session": null,

? ? ? ? ? ? "stereotype": {

? ? ? ? ? ? ? "browserName": "firefox",

? ? ? ? ? ? ? "platformName": "WIN10"

? ? ? ? ? ? }

? ? ? ? ? },

? ? ? ? ? {

? ? ? ? ? ? "id": {

? ? ? ? ? ? ? "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",

? ? ? ? ? ? ? "id": "437be3d5-bdf0-44aa-b401-51f7b9136b15"

? ? ? ? ? ? },

? ? ? ? ? ? "lastStarted": "1970-01-01T00:00:00Z",

? ? ? ? ? ? "session": null,

? ? ? ? ? ? "stereotype": {

? ? ? ? ? ? ? "browserName": "chrome",

? ? ? ? ? ? ? "platformName": "WIN10"

? ? ? ? ? ? }

? ? ? ? ? },

? ? ? ? ? {

? ? ? ? ? ? "id": {

? ? ? ? ? ? ? "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",

? ? ? ? ? ? ? "id": "f799da78-4ac8-4799-8d4b-50314a954c01"

? ? ? ? ? ? },

? ? ? ? ? ? "lastStarted": "1970-01-01T00:00:00Z",

? ? ? ? ? ? "session": null,

? ? ? ? ? ? "stereotype": {

? ? ? ? ? ? ? "browserName": "chrome",

? ? ? ? ? ? ? "platformName": "WIN10"

? ? ? ? ? ? }

? ? ? ? ? },

? ? ? ? ? {

? ? ? ? ? ? "id": {

? ? ? ? ? ? ? "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",

? ? ? ? ? ? ? "id": "10bd31b1-acf6-4006-af7c-47ea448a6a33"

? ? ? ? ? ? },

? ? ? ? ? ? "lastStarted": "1970-01-01T00:00:00Z",

? ? ? ? ? ? "session": null,

? ? ? ? ? ? "stereotype": {

? ? ? ? ? ? ? "browserName": "chrome",

? ? ? ? ? ? ? "platformName": "WIN10"

? ? ? ? ? ? }

? ? ? ? ? },

? ? ? ? ? {

? ? ? ? ? ? "id": {

? ? ? ? ? ? ? "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",

? ? ? ? ? ? ? "id": "31c91763-4aef-4379-910c-647266806fce"

? ? ? ? ? ? },

? ? ? ? ? ? "lastStarted": "1970-01-01T00:00:00Z",

? ? ? ? ? ? "session": null,

? ? ? ? ? ? "stereotype": {

? ? ? ? ? ? ? "browserName": "chrome",

? ? ? ? ? ? ? "platformName": "WIN10"

? ? ? ? ? ? }

? ? ? ? ? },

? ? ? ? ? {

? ? ? ? ? ? "id": {

? ? ? ? ? ? ? "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",

? ? ? ? ? ? ? "id": "20caece4-cb26-4914-8dfa-80fffd37191f"

? ? ? ? ? ? },

? ? ? ? ? ? "lastStarted": "1970-01-01T00:00:00Z",

? ? ? ? ? ? "session": null,

? ? ? ? ? ? "stereotype": {

? ? ? ? ? ? ? "browserName": "firefox",

? ? ? ? ? ? ? "platformName": "WIN10"

? ? ? ? ? ? }

? ? ? ? ? },

? ? ? ? ? {

? ? ? ? ? ? "id": {

? ? ? ? ? ? ? "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",

? ? ? ? ? ? ? "id": "53a12fa7-63a4-474d-bab4-5e6fc6976ff1"

? ? ? ? ? ? },

? ? ? ? ? ? "lastStarted": "1970-01-01T00:00:00Z",

? ? ? ? ? ? "session": null,

? ? ? ? ? ? "stereotype": {

? ? ? ? ? ? ? "browserName": "chrome",

? ? ? ? ? ? ? "platformName": "WIN10"

? ? ? ? ? ? }

? ? ? ? ? },

? ? ? ? ? {

? ? ? ? ? ? "id": {

? ? ? ? ? ? ? "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",

? ? ? ? ? ? ? "id": "7d1bb113-1050-482f-bd5d-939362270fdf"

? ? ? ? ? ? },

? ? ? ? ? ? "lastStarted": "1970-01-01T00:00:00Z",

? ? ? ? ? ? "session": null,

? ? ? ? ? ? "stereotype": {

? ? ? ? ? ? ? "browserName": "firefox",

? ? ? ? ? ? ? "platformName": "WIN10"

? ? ? ? ? ? }

? ? ? ? ? },

? ? ? ? ? {

? ? ? ? ? ? "id": {

? ? ? ? ? ? ? "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",

? ? ? ? ? ? ? "id": "c4993662-f2f1-46e6-ab6c-6eae7727dd68"

? ? ? ? ? ? },

? ? ? ? ? ? "lastStarted": "1970-01-01T00:00:00Z",

? ? ? ? ? ? "session": null,

? ? ? ? ? ? "stereotype": {

? ? ? ? ? ? ? "browserName": "firefox",

? ? ? ? ? ? ? "platformName": "WIN10"

? ? ? ? ? ? }

? ? ? ? ? },

? ? ? ? ? {

? ? ? ? ? ? "id": {

? ? ? ? ? ? ? "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",

? ? ? ? ? ? ? "id": "d9fcf1d0-cba0-4a53-bba4-934f35be8ccb"

? ? ? ? ? ? },

? ? ? ? ? ? "lastStarted": "1970-01-01T00:00:00Z",

? ? ? ? ? ? "session": null,

? ? ? ? ? ? "stereotype": {

? ? ? ? ? ? ? "browserName": "chrome",

? ? ? ? ? ? ? "platformName": "WIN10"

? ? ? ? ? ? }

? ? ? ? ? },

? ? ? ? ? {

? ? ? ? ? ? "id": {

? ? ? ? ? ? ? "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",

? ? ? ? ? ? ? "id": "8b8bdd06-4c6f-4344-812a-3429b5d013c7"

? ? ? ? ? ? },

? ? ? ? ? ? "lastStarted": "1970-01-01T00:00:00Z",

? ? ? ? ? ? "session": null,

? ? ? ? ? ? "stereotype": {

? ? ? ? ? ? ? "browserName": "firefox",

? ? ? ? ? ? ? "platformName": "WIN10"

? ? ? ? ? ? }

? ? ? ? ? },

? ? ? ? ? {

? ? ? ? ? ? "id": {

? ? ? ? ? ? ? "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",

? ? ? ? ? ? ? "id": "f7ca236a-7c1f-48ba-93e1-25745ed18493"

? ? ? ? ? ? },

? ? ? ? ? ? "lastStarted": "1970-01-01T00:00:00Z",

? ? ? ? ? ? "session": null,

? ? ? ? ? ? "stereotype": {

? ? ? ? ? ? ? "browserName": "chrome",

? ? ? ? ? ? ? "platformName": "WIN10"

? ? ? ? ? ? }

? ? ? ? ? },

? ? ? ? ? {

? ? ? ? ? ? "id": {

? ? ? ? ? ? ? "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",

? ? ? ? ? ? ? "id": "3116789e-3fab-4bcc-89fe-9c0b788bb29f"

? ? ? ? ? ? },

? ? ? ? ? ? "lastStarted": "1970-01-01T00:00:00Z",

? ? ? ? ? ? "session": null,

? ? ? ? ? ? "stereotype": {

? ? ? ? ? ? ? "browserName": "firefox",

? ? ? ? ? ? ? "platformName": "WIN10"

? ? ? ? ? ? }

? ? ? ? ? },

? ? ? ? ? {

? ? ? ? ? ? "id": {

? ? ? ? ? ? ? "hostId": "4bb7d6f7-466d-4537-87ca-1441023e0fff",

? ? ? ? ? ? ? "id": "3abf1ce7-cd89-48ae-a33d-252abd9a29c5"

? ? ? ? ? ? },

? ? ? ? ? ? "lastStarted": "1970-01-01T00:00:00Z",

? ? ? ? ? ? "session": null,

? ? ? ? ? ? "stereotype": {

? ? ? ? ? ? ? "browserName": "firefox",

? ? ? ? ? ? ? "platformName": "WIN10"

? ? ? ? ? ? }

? ? ? ? ? }

? ? ? ? ]

? ? ? }

? ? ]

? }

}

我們還可以可以通過在瀏覽器端輸入 http://localhost:4444/grid/console 來查看 Grid 控制臺,也可以看到 standalone 模式下,在 node 節點中有對應的 Chrome 瀏覽器和 Firefox 瀏覽器。

今年最后一天 學點Selenium玩點新鮮 新一年讓分布式測試有更多玩法

Step4:使用 Selenium Grid 執行測試

在上述步驟中,我們通過最簡單的 standalone 模式配置好了 hub 以及 node,下一步我們將會執行我們的自動化測試。

在編寫測試腳本時,我們只需要通過 RemoteWebDriver 指向 http://localhost:4444/

@Test

public void testChrome() throws MalformedURLException {

? ? DesiredCapabilities caps = new DesiredCapabilities();

? ? caps.setCapability(CapabilityType.BROWSER_NAME,"chrome");

? ? WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/"), caps);

? ? driver.manage().window().maximize();

? ? driver.get("http://testingpai.com/");

}

@Test

public void testFirefox() throws MalformedURLException {

? ? DesiredCapabilities caps = new DesiredCapabilities();

? ? caps.setCapability(CapabilityType.BROWSER_NAME,"firefox");

? ? WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/"), caps);

? ? driver.manage().window().maximize();

? ? driver.get("http://testingpai.com/");

}

上述示例客戶端腳本和 Grid 服務都是在本機,當然在實際應用中我們可以將 Grid 服務和腳本分別部署在兩臺主機上,此時腳本中指向的地址:http://localhost:4444/換成 Grid 服務所在主機的對外 IP 地址即可。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容