什么是selenium-grid,官方解释:takes Selenium Remote Control to another level by running tests on many servers at the same time, cutting down on the time it takes to test multiple browsers or operating systems。
个人理解是分布式测试,即在不同的操作系统、浏览器上并发执行测试用例,那如何使用呢,这两天研究了一下,初步能调用起来,也算刚入门,今天做下总结。
首先要知道grid是有一个hub和若干个node组成,hub负责管理node和接收代码的request,并把request分发给node,让node去执行。
1.下载selenium-server-standalone-XXX.jar,可以去官网下载最新的,我这里是2.47版本的,下载地址:http://pan.baidu.com/s/1kT3rK5T
2.选一台机器作为hun,启动dos,输入命令:
172.16.28.34为hub的ip
4.如果电脑有限,可使用虚拟机或者在hub所在的机器,也启动一个node,启动dos,输入命令
java -jar selenium-server-standalone-2.45.0.jar -role node -hub http://localhost:4444/grid/register
5.启动成功后,可在hub所在的机器的浏览器中输入http://localhost:4444/grid/console,查看所连接的node信息
6.测试代码
/* 启动火狐浏览器 */ public static void testFireFox() { String key = "webdriver.firefox.bin"; String value = "C:/Program Files (x86)/Mozilla Firefox/firefox.exe"; System.setProperty(key, value); //DesiredCapabilities aDesiredcap = DesiredCapabilities("firefox", "22", Platform.LINUX) DesiredCapabilities test = DesiredCapabilities.firefox(); test.setVersion(""); test.setPlatform(Platform.WINDOWS); WebDriver dr; try { dr = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),test); dr.get("http://www.baidu.com"); Thread.sleep(3000); dr.quit(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/* 启动火狐浏览器 */ public static void testChrome() { DesiredCapabilities aDesiredcap = DesiredCapabilities.chrome(); //aDesiredcap.setBrowserName("chrome"); aDesiredcap.setPlatform(Platform.WINDOWS); WebDriver dr; try { dr = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), aDesiredcap); dr.get("http://www.baidu.com"); dr.findElement(By.id("kw")).sendKeys("test"); Thread.sleep(3000); dr.quit(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/* 启动IE浏览器 */ public static void testIE() { DesiredCapabilities aDesiredcap = DesiredCapabilities.internetExplorer(); aDesiredcap.setPlatform(Platform.WINDOWS); WebDriver dr; try { dr = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), aDesiredcap); dr.get("http://www.baidu.com"); Thread.sleep(3000); dr.quit(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
调用以上3个方法,hun会自动分配由哪个node去执行。
需要注意的是:
1.node所在机器的chromedriver.exe、IEDriverServer.exe需放在C盘的system32下,或者配置classpath,再或者用命令行指定路径
java -jar selenium-server-standalone-2.45.0.jar -D webdriver.firefox.bin=”D:/Program Files/Mozilla Firefox/firefox.exe” -role node -hub http://172.16.28.34:4444/grid/register
java -jar selenium-server-standalone-2.45.0.jar -D webdriver.chrome.driver="D:/chromedriver.exe" -role node -hub http://172.16.28.34:4444/grid/register
2.关闭hub和node机器的防火墙
3.异常
selenium.common.exceptions.WebDriverException: Message:
Error forwarding the new session cannot find : Capabilities [{browserName=htmlunit, javascriptEnabled=true, version=, platform=ANY}]
指定一下 browsername:
java -jar selenium-server-standalone-2.44.0.jar -role node -hub http://IP:4444/grid/register -browser browserName=htmlunit
关于grid的官网介绍:
https://github.com/SeleniumHQ/selenium/wiki/Grid2