應領導要求使用 selenium grid 搭建分布式測試平台,於是有了以下操作:
第一步:准備2台電腦,且2台電腦都安裝好jdk,都准備好selenium-server-standalone-2.40.0.jar,IEDriver, ChromeDriver等工具,注意chrome版本與chromedriver需要匹配,詳見我的另一篇博客:http://www.cnblogs.com/cherrysu/p/7815245.html
第二步:其中一台電腦作為hub,也就是老大,另一台作為node,也就是小弟,兩台電腦一定要能ping通。
第三步:hub機上新建HUB.bat, 內容參考:
java -jar C:\\Software\\selenium\\selenium-server-standalone-2.40.0.jar -role hub
其中selenium-server-standalone-2.40.0.jar 的版本和路徑需要改成自己的。
第四步:node機上新建node.bat,內容參考:
java -Dwebdriver.chrome.driver="C:\\software\\chromedriver.exe" -Dwebdriver.ie.driver="C:\\software\\IEDriverServer-x64.exe" -jar C:\\libs\\selenium-server-standalone-2.40.0.jar -role node -nodeConfig node.json -hub http://hub機IP:4444/grid/register
其中 chromedriver.exe,IEDriverServer-x64.exe,standalone都需要改為node機上對應的地址, -nodeConfig node.json可以修改node機上的grid參數,具體內容參考:
{
"capabilities": [
{
"browserName": "chrome",
"maxInstances": 5,
"platform": "WINDOWS",
"version":"51"
},
{
"browserName": "internet explorer",
"maxInstances": 2,
"platform": "WINDOWS",
"webdriver.ie.driver": "IEDriverServer.exe"
}
],
"configuration": {
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"maxSession": 5,
"port": 5555,
"register": true,
"registerCycle": 5000,
"hub": "http://192.168.84.209:4444"
}
}
第五步:先運行hub.bat,在運行node.bat,在hub機上打開 http://localhost:4444/grid/console# 查看鏈接狀態
第六步:編寫java腳本
package test; import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.Platform; import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; /** * * @author Rong * */ public class RemoteWebDriverUtil { static WebDriver driver; // 遠程調用ie瀏覽器 public static WebDriver createRemoteIEDriver() { // 指定調用IE進行測試 DesiredCapabilities capability = DesiredCapabilities.internetExplorer(); // 避免IE安全設置里,各個域的安全級別不一致導致的錯誤 capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); // 連接到selenium hub,遠程啟動瀏覽器 capability.setPlatform(Platform.XP); try { driver = new RemoteWebDriver(new URL("http://node機IP:5555/wd/hub"), capability); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return driver; } // 啟用遠程調用chrome public static WebDriver createRemoteChromeDriver() { DesiredCapabilities capability = DesiredCapabilities.chrome(); capability.setBrowserName("chrome"); capability.setVersion("62"); capability.setPlatform(Platform.WINDOWS); try { driver = new RemoteWebDriver(new URL("http://node機IP:5555/wd/hub"), capability); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return driver; } // 啟用遠程調用firefox public static WebDriver createRemoteFirefoxDriver() { DesiredCapabilities capability = DesiredCapabilities.firefox(); capability.setBrowserName("firefox"); capability.setPlatform(Platform.XP); try { driver = new RemoteWebDriver(new URL("http://node機IP:5555/wd/hub"), capability); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return driver; } }
package test; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; public class test extends RemoteWebDriverUtil{ @Test public void testBaiduSearch() throws Exception{ WebDriver driver = createRemoteChromeDriver(); driver.get("http://www.baidu.com"); WebElement baiduInput = driver.findElement(By.id("kw")); baiduInput.sendKeys("Cherry_chrome"); WebElement btnSearch = driver.findElement(By.id("su")); btnSearch.click(); try { WebElement CherrySearchResult = driver.findElement(By.cssSelector(".op_dict3_font24.op_dict3_marginRight")); System.out.println("Find search result Successfully!"); } catch (NoSuchElementException e) { System.out.println("Can't find search result!, Search failed!"); } } }
第七步:運行test.java,node機上的chromedriver.exe運行起來了,遠程控制搭建搭建完成。
后續請參考:Selenium grid 分布式測試搭建(二)
第六步代碼轉自:http://www.cnblogs.com/longronglang/p/6220277.html
第三步配置文件轉自:http://www.360doc.com/content/16/1121/14/36343398_608254453.shtml