标签:des style blog class code java
selenium grid Quick Start
selenium-grid是用于设计帮助我们进行分布式测试的工具,其整个结构是由一个hub节点和若干个代理节点组成。hub用来管理各个代理节点的注册和状态信息,并且接受远程客户端代码的请求调用,然后把请求的命令再转发给代理节点来执行。
1、启动Selenium-Server
1)下载 selenium-server-standalone-*.jar,地址:http://selenium-release.storage.googleapis.com/index.html?path=2.41/
2)启动selenium server并注册hub (假设hub服务器机器selenium-server版本为2.41,本机IP为192.32.50.26)
到selenium-server-standalone-*.jar 包放置目录下执行:java -jar selenium-server-standalone-2.41.0.jar -role hub -port 4441 (未指定port时,hub默认4444)
3)本地或者远程机器, 都可以启动多个node
a.本地启动:
默认: java -jar selenium-server-standalone-2.41.0.jar -role node (默认的注册地址:http://localhost:4444/grid/register,node默认端口:5555)
指定hub: java -jar selenium-server-standalone-2.41.0.jar -role node -hub http://localhost:4441/grid/register
b.远程启动:
远程同样需要下载selenium-Server jar包,启动如下: (假设远程版本为2.29)
java -jar selenium-server-standalone-2.29.0.jar -role node -hub http://192.32.50.26:4441/grid/register -Dwebdriver.chrome.driver=chromedriver29.exe -browser browserName=chrome -port 6666
2、具体应用
public class RemoteWebDriverDesiredCapTest { @Test public void myTest() throws MalformedURLException{ /** * 1、本地机器启动了主节点hub (假設本台机器IP:192.32.50.26,http://localhost:4444/grid/console 查看节点连接情况) * java -jar selenium-server-standalone-2.41.0.jar -role hub (默认端口4444) * 2、本地也启动了一个该hub 次节点A * java -jar selenium-server-standalone-2.41.0.jar -role node * (默认的注册地址:http://localhost:4444/grid/register,node默认端口:5555) * (具体设置:java -jar selenium-server-standalone-2.41.0.jar -role node -port 5556 -hub http://localhost:4441/grid/register) * 3、本机器装有firefox, chrome,所以通过该次节点A, 可以用firefox来运行脚本, 也可以用chrome * FF驱动默认在webDriver中了, 而chrome驱动需要自己指定, chromeDriver.exe ,运行chromeDriver(默认使用端口9515) */
//node:java -jar selenium-server-standalone-2.41.0.jar -role node DesiredCapabilities capability = DesiredCapabilities.firefox(); capability.setPlatform(Platform.VISTA); WebDriver driver = new RemoteWebDriver(new URL("http://192.32.50.26:5555/wd/hub"), capability); driver.get("http://www.baidu.com"); System.out.println(driver.getCurrentUrl()); driver.quit(); // node: 直接运行chromedriver.exe时, 通过http://xxx.xxx.xxx.xxx:9515 就可以取得chrome; 或者像 下面指定-Dwebdriver.chrome.driver -port,形式 DesiredCapabilities aDesiredcap =new DesiredCapabilities("chrome","",Platform.VISTA); WebDriver wd = new RemoteWebDriver(new URL("http://localhost:9515"), aDesiredcap); wd.get("https://code.google.com/p/selenium/wiki/Grid2"); System.out.println(wd.getCurrentUrl()); wd.quit(); //node:java -jar selenium-server-standalone-2.41.0.jar -role node -hub http://192.32.50.26:4444/grid/register -Dwebdriver.ie.driver=IEDriverServer.exe -browser browserName=iexplore -port 7776 DesiredCapabilities iDesiredcap =new DesiredCapabilities("iexplore","",Platform.VISTA); WebDriver wdE = new RemoteWebDriver(new URL("http://192.32.50.26:7776/wd/hub"), iDesiredcap); wdE.get("https://code.google.com/p/selenium/wiki/Grid2"); System.out.println(wdE.getCurrentUrl()); wdE.quit(); //用另一台机器作为次节点连接 , (假設IP為: 192.32.52.51 ) //java -jar selenium-server-standalone-2.29.0.jar -role node -hub http://192.32.50.26:4444/grid/register -Dwebdriver.chrome.driver=chromedriver29.exe -browser browserName=chrome -port 6666 DesiredCapabilities chrome =new DesiredCapabilities("chrome","",Platform.XP); WebDriver driverChrome = new RemoteWebDriver(new URL("http://192.32.52.51:6666/wd/hub"), chrome); driverChrome.get("http://www.cnblogs.com/"); driverChrome.quit(); } }
3、做成多线程方式
Selenium Grid只是提供多系统、多浏览器的执行环境,而不是说任务一个test case丢给它就能并行运行。并行的运行我这里就交给testng了
如下代码和配置:
1)testcase 代码:
package com.jennifer.test; import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class GridParallelTests { private WebDriver dr; DesiredCapabilities desireCap; @Parameters({ "browser", "nodeUrl"}) @BeforeMethod public void setUp(String browser, String nodeUrl) { if (browser.equals("ie")){ desireCap = DesiredCapabilities.internetExplorer(); } else if (browser.equals("ff")) desireCap = DesiredCapabilities.firefox(); else if (browser.equals("chrome")) desireCap = DesiredCapabilities.chrome(); else System.out.println("browser parameter wrong , can only be :ie、 ff、chrome"); String url = nodeUrl + "/wd/hub"; URL urlInstance = null; try { urlInstance = new URL(url); } catch (MalformedURLException e) { e.printStackTrace(); } dr = new RemoteWebDriver(urlInstance, desireCap); } @Test public void test() { dr.get("http://www.baidu.com/"); dr.findElement(By.id("kw1")).sendKeys("selenium"); dr.findElement(By.id("su1")).click(); System.out.println("title:" + dr.getTitle()); } @AfterMethod public void quit() { dr.close(); } }
2)testng.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite1" parallel="tests" thread-count="5"> <test name="test1"> <parameter name="browser" value="ff" /> <parameter name="nodeUrl" value="http://192.32.50.26:5555"/> <classes> <class name="com.jennifer.test.GridParallelTests"></class> </classes> </test> <test name="test2"> <!-- java -jar selenium-server-standalone-2.41.0.jar -role node -hub http://192.32.50.26:4444/grid/register -Dwebdriver.chrome.driver=chromedriver29.exe -browser browserName=chrome -port 6666 --> <parameter name="browser" value="chrome" /> <parameter name="nodeUrl" value="http://192.32.50.26:6666" /> <classes> <class name="com.jennifer.test.GridParallelTests"></class> </classes> </test> <test name="test3"> <!-- java -jar selenium-server-standalone-2.41.0.jar -role node -hub http://192.32.50.26:4444/grid/register -Dwebdriver.ie.driver=IEDriverServer.exe -browser browserName=iexplore -port 7776 --> <parameter name="browser" value="ie" /> <parameter name="nodeUrl" value="http://192.32.50.26:7776" /> <classes> <class name="com.jennifer.test.GridParallelTests"></class> </classes> </test> </suite>
selenium Grid(一),布布扣,bubuko.com
标签:des style blog class code java
原文地址:http://www.cnblogs.com/jenniferhuang/p/3728658.html