Selenium 第一炮--Selenium Grid

哈~作为技术小牛,向来不写技术文章,现在发现还是不太行哈,找个文章老是往回找找不到,so开始写自己的blog吧,虽然没有创新,但集众家所长还是可以的吧。第一篇是要今天又要查Selenium Grid命令,原来那篇文章找半天,so决定吧它摘过来啦!

 

Selenium Grid允许同时并行地、在不同的环境上运行多个测试任务。

参考:

 

http://code.google.com/p/selenium/wiki/Grid2

 

原理:

Selenium Grid 架构如上图所示,所以需要一个Hub做主节点,多个作为子节点。

Sample:

Grid需要一台机子做为主节点,然后其它机子做为子节点连接到这个主节点上来。所以首先要启动主节点。

 

  • 启动主节点:

打开命令行,cd至selenium-server-standalone-*.jar包的目录下,然后用下面的命令启动主节点服务:

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

 

默认启动默认端口为4444。如果要改这个端口,可以再上面的命令后面加上 -port XXXX。启动完后,你可以用浏览器打开http://localhost:4444/grid/console这个网址查看主节点的状态。

可加参数:

-hubConfig hubconfig.json ;json 格式的配置
http://code.google.com/p/selenium/source/browse/trunk/java/server/src/org/openqa/grid/common/defaults/DefaultHub.json 
-host localhost ;主机地址
-port 4444 ;端口
-newSessionWaitTimeout -1 ;会话等待时间
-servlets []
-prioritizer null ;优先权
-capabilityMatcher org.openqa.grid.internal.utils.DefaultCapabilityMatcher
-nodePolling 5000 ;刷新子节点(毫秒)
-cleanUpCycle 5000
-timeout 300000
-browserTimeout 0
-maxSession 5


 

 

启动完主节点之后,要做的就是启动子节点。

  • 启动子节点:

其他机子做为子节点。同样打开命令行,cd至selenium-server-standalone-*.jar包的目录下,然后用下面的命令启动次节点服务:

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

 

其中localhost为主节点机子的ip地址,可以使用ipconfig命令在命令行查看得到。上面命令默认启动5555端口,可使用-port 更改。启动完成连接到主节点后,可以在主节点机子上 http://localhost:4444/grid/console网址查看到这个子节点状态。使用同样的方法,可以链接其它的子节点。

可加参数:

-nodeConfig nodeconfig.json ;json 格式的配置
http://code.google.com/p/selenium/source/browse/trunk/java/server/src/org/openqa/grid/common/defaults/DefaultNode.json

 

 

-browser browserName=firefox,version=3.6,firefox_binary=/home/myhomedir/firefox36/firefox,maxInstances=3,platform=LINUX 
-browser browserName=firefox,version=4,firefox_binary=/home/myhomedir/firefox4/firefox,maxInstances=4,platform=LINUX ;定义本机浏览器

-role node/webdriver 
-host ip ;主机地址
-port 5555 ;端口
-hubHost ip ;Hub地址
-hubPort 4444 ;Hub端口
-proxy org.openqa.grid.selenium.proxy.DefaultRemoteProxy
-registerCycle 5000 ;刷新子节点(毫秒)
-register true
-maxSession 5
-Dwebdriver.chrome.driver="D:/chromedriver.exe" ;Chromedirver
-Dwebdriver.firefox.bin="D:/Program Files/Mozilla Firefox/firefox.exe" ;firfoxdirver

例:启动chrome(需要去下载chrome webdriver 驱动)

 

java -jar selenium-server-standalone-2.24.1.jar   -Dwebdriver.chrome.driver="D:/chromedriver.exe" -role webdriver  -hub http://localhost:4444/grid/register -port 6666 -browser browserName=chrome

启动firfox

 

java -jar selenium-server-standalone-2.24.1.jar   -Dwebdriver.firefox.bin="D:/Program Files/Mozilla Firefox/firefox.exe" -role webdriver  -hub http://localhost:4444/grid/register -browser browserName=firefox

  • 运行一个简单的例子:
上面已经把grid弄成功了,现在我们用Grid来运行一个很简单的例子。代码如下:
import java.net.MalformedURLException; 
import java.net.URL; 

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.remote.DesiredCapabilities; 
import org.openqa.selenium.remote.RemoteWebDriver; 

public class GridTest
{ 
  /**
  * @throws MalformedURLException 
  */ 
  public static void main(String[] args) throws MalformedURLException
  { 
  	String browser = "firefox";
  	DesiredCapabilities test = DesiredCapabilities.firefox(); 
  	if(browser.equals("ie"))
  		test = DesiredCapabilities.internetExplorer(); 
    else if(browser.equals("firefox"))
    	test = DesiredCapabilities.firefox(); 
    else if(browser.equals("chrome"))
    	test = DesiredCapabilities.chrome();
   
    WebDriver dr = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),test); 
    dr.get("http://www.google.com"); 
    dr.findElement(By.id("gs_htif0")).sendKeys("selenium"); 
    dr.findElement(By.id("gbqfba")).click();
   
    dr.close(); 
  } 
} 

在主节点机子上运行上面的代码,你可以在次节点机子上看到firefox浏览器被启动,然后打开了google这个网址。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值