- Selenium自動化測試之道
- Ping++測試團隊
- 1018字
- 2020-11-28 14:42:27
2.5 Selenium Grid
隨著對Selenium IDE和Selenium WebDriver的學習,我們已經了解了測試腳本的編寫方法。接下來,將學習Selenium Grid的用法,利用這一分布式測試執行工具來提升腳本執行效率。
2.5.1 工作原理
當測試用例需要同時在多個平臺和瀏覽器上執行時,就可以使用Selenium Grid。如圖2-44所示,其整個結構是由一個Hub節點和若干個Node(代理節點)組成的。Hub用來管理各個Node的狀態,并且接受遠程客戶端代碼的調用請求,再把請求命令轉發給Node來執行。使用Selenium Grid遠程執行測試代碼與直接調用Selenium Server是一樣的,只是環境啟動的方式不一樣,需要同時啟動一個Hub和至少一個Node。

圖2-44 Selenium Grid工作原理
2.5.2 環境搭建
首先需要為所有執行測試腳本的機器準備好Selenium WebDriver環境,可參考2.4.3小節。之后就可以開始準備Selenium Grid環境了。
1.啟動Hub
啟動命令為:
java -jar selenium-server-standalone-x.xx.x.jar -role hub
如圖2-45所示,這里使用的是selenium-server-standalone-2.42.2.jar。

圖2-45 Selenium Grid——啟動Hub
啟動Hub的機器可以是任意一臺有Java運行環境的機器,它是整個Selenium Grid的中樞節點,所有的遠程測試都會經由它轉發出去,之后在對應的測試機器上執行測試。
圖2-46顯示默認端口是4444,可在啟動時通過-port參數來指定端口。若可以成功訪問http://<your_hub_host>:4444/,則說明Hub已被成功啟動。單擊頁面中的console可以查看所有節點詳情(進入console的時候速度很慢,需要耐心等待一段時間),此時尚未啟動節點,因此里面是空白的,沒有節點信息。

圖2-46 查看默認端口的Hub是否啟動成功
2.啟動Node
在測試機器上打開一個終端,執行以下命令,如圖2-47所示。

圖2-47 Selenium Grid – 指定端口啟動
java -jar selenium-server-standalone-x.xx.x.jar -role node -hub http:// <your_hub_host>:4444/ grid/register -port 4000
-hub http:// <your_hub_host>:4444/grid/register用于Node注冊,即當前的測試機器作為上一步Hub的Node。-port參數是可選的,用于指定Node注冊時的端口,若不加-port端口號,則默認是從5555端口啟動。
若要為Hub啟動多個Node,要注意為每個Node分配不同的端口。
java -jar selenium-server-standalone-x.xx.x.jar -role node -port 4000 java -jar selenium-server-standalone-x.xx.x.jar -role node -port 4001 java -jar selenium-server-standalone-x.xx.x.jar -role node -port 4002
3.查看Selenium Grid狀態
當Hub與Node都啟動成功后,可以通過Hub的Console頁面查看當前Selenium Grid的狀態,直接訪問地址http:// <your_hub_host>:4444/grid/console。
如圖2-48所示,頁面顯示了可用于測試的Node數量和類型,這里顯示的數量與類型和啟動Node時所帶的配置參數有關。啟動Node其實就是一個注冊過程,啟動時所帶的參數會被Hub記錄為注冊信息,所以頁面中所看到的信息就是Node注冊信息的匯總。

圖2-48 查看所有節點的注冊信息
4.代碼運行
當Hub和Node都已設置完畢,且WebDriver環境已搭建完成時,便可以在代碼里進行調用了。下面以簡單的必應搜索功能為例來說明如何調用使用4000端口的Node機器,代碼如下:
package com.selenium.test; import java.net.MalformedURLException; import java.net.URL; import org.junit.*; import org.openqa.selenium.*; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class TestGrid { private String baseUrl; @Test public void testGrid() throws Exception { baseUrl = "http://cn.bing.com/"; DesiredCapabilities capability =new DesiredCapabilities(); capability.setBrowserName("firefox"); capability.setPlatform(Platform.VISTA); WebDriver driver=null; try { driver = new RemoteWebDriver(new URL("http://192.168.1.111:4000/wd/hub"),capability); //your_node_ip : port } catch (MalformedURLException e) { e.printStackTrace(); } driver.get(baseUrl); WebElement searchElement = (new WebDriverWait(driver,10)).until(new ExpectedCondition <WebElement>(){ public WebElement apply (WebDriver wd){ return wd.findElement(By.id("sb_form_q")); } }); searchElement.clear(); searchElement.sendKeys("WebDriver"); driver.findElement(By.id("sb_form_go")).click(); driver.quit(); } }
- 從零開始:數字圖像處理的編程基礎與應用
- OpenCV for Secret Agents
- Python網絡爬蟲從入門到實踐(第2版)
- C語言程序設計
- Windows Presentation Foundation Development Cookbook
- Backbone.js Blueprints
- 軟件架構:Python語言實現
- Mastering KnockoutJS
- 劍指MySQL:架構、調優與運維
- Java實戰(第2版)
- 深入淺出React和Redux
- Maker基地嘉年華:玩轉樂動魔盒學Scratch
- Python一行流:像專家一樣寫代碼
- Python+Office:輕松實現Python辦公自動化
- HTML5 Canvas核心技術:圖形、動畫與游戲開發