selenium 测试框架中使用grid

时间:2023-03-08 15:48:33

之前的测试框架:http://www.cnblogs.com/tobecrazy/p/4553444.html

配合Jenkins可持续集成:http://www.cnblogs.com/tobecrazy/p/4529399.html

在测试框架中使用Log4J 2 :http://www.cnblogs.com/tobecrazy/p/4557592.html

首先介绍一下grid ,selenium grid 是一种执行测试用例时使用的包含不同平台(windows、Linux、Android)的框架,并且

这些平台是由一个中心点控制,这个中心点称之为HUB,而那些不同的平台称之为NODE

其结构如下:

selenium 测试框架中使用grid

为什么使用selenium grid:

如果你的程序需要在不用的浏览器,不同的操作系统上测试,而且比较多的case需要多线程远程执行,那么一个比较好的solution就是使用grid.selenium-grid是用于设计帮助我们进行分布式测试的工具,其整个结构是由一个hub节点和若干个代理节点组成。hub用来管理各个代理节点的注册和状态信息,并且接受远程客户端代码的请求调用,然后把请求的命令再转发给代理节点来执行。

怎么使用:

首先启用HUB:

在A机器下载:selenium standalone 4.6:http://pan.baidu.com/s/1qWE7SD2

然后创建HUB.bat

内容为:

 java -jar selenium-server-standalone-2.46..jar -role hub 

其默认监听端口4444,默认IP localhost  如果要修改,只需要加-port 参数和-Hubhost

java -jar selenium-server-standalone-2.46..jar -role hub  -port 1235 -Hubhost 192.168.2.45

接下来在B机添加node ,创建Node.bat,这里使用的是默认的Hubhost Ip 和端口

 java -jar selenium-server-standalone-2.46..jar -role node -hub http://localhost:4444/grid/register

为了使用chrome和IE driver,我们需要这样设置

 java -Dwebdriver.ie.driver="C:\Users\workspace\Demo\webDriver\IEDriverServer.exe" -Dwebdriver.chrome.driver="C:\Users\workspace\Demo\webDriver\chromedriver.exe" -jar selenium-server-standalone-2.46..jar -role node -hub http://localhost:4444/grid/register

分别启动这两个bat

若使用remote Driver,需要设置这样的参数

DesiredCapabilities capability = DesiredCapabilities.firefox();
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
capability.setBrowserName("firefox" );
capability.setVersion("3.6");

所以我们索性创建一个bean

 /**
*
*/
package com.dbyl.libarary.utils; /**
* for remote browser bean
* @author Young
*
*/
public class RemoteBrowserBean {
private String browserName;
private String version;
private String[] platform;
private String hubURL;
public String getBrowserName() {
return browserName;
} public RemoteBrowserBean()
{
this.browserName="firefox";
this.version="38";
this.platform=new String[]{"VISTA", "windows 7"};
this.hubURL="http://localhost:4444/wd/hub"; } public RemoteBrowserBean(String browser)
{
this.browserName=browser;
this.version="42";
this.platform=new String[]{"VISTA", "windows 7"};
this.hubURL="http://localhost:4444/wd/hub"; } public void setBrowserName(String browserName) {
this.browserName = browserName;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
} public String[] getPlatform() {
return platform;
} public void setPlatform(String[] platform) {
this.platform = platform;
} public String getHubURL() {
return hubURL;
}
public void setHubURL(String hubURL) {
this.hubURL = hubURL;
} }

然后在DriverFactory里创建getRemoteDriver

 /**
* This method will create RemoteWebdriver
* @author Young
* @param remoteBrowserBean
* @return WebDriver
*/
public static WebDriver getRemoteDriver(RemoteBrowserBean remoteBrowserBean) {
DesiredCapabilities capability = null;
if (remoteBrowserBean.getBrowserName().contains("firefox")) {
capability = DesiredCapabilities.firefox();
} else if (remoteBrowserBean.getBrowserName().contains("chrome")) {
capability = DesiredCapabilities.chrome();
} WebDriver driver = null;
try {
driver = new RemoteWebDriver(
new URL(remoteBrowserBean.getHubURL()), capability);
} catch (MalformedURLException e) {
e.printStackTrace();
}
capability.setBrowserName(remoteBrowserBean.getBrowserName());
capability.setVersion(remoteBrowserBean.getVersion());
capability.setCapability(remoteBrowserBean.getPlatform()[0],
remoteBrowserBean.getPlatform()[1]);
driver.manage().window().maximize();
return driver;
}

接下来就可以在case里边使用

 @BeforeClass(alwaysRun=true)
public void beforeTest()
{
driver = DriverFactory.getRemoteDriver(new RemoteBrowserBean("chrome"));
}

下载地址:https://github.com/tobecrazy/Demo