应网友要求写一个用Selenium Grid控制多系统多浏览器并行执行test case的例子。
因为我这里有两台机子,我打算这样演示:
一台机子启动一个作为主点节的hub 和 一个作为次节点的hub(系统windows 浏览器为ie) ip为:192.168.40.24
另一台机子启动两个次节点hub,一个次节点浏览器为firefox,另一个浏览器为chrome (系统为windows),ip为:192.168.40.67
启动各节点hub
启动主节点hub命令:
java -jar selenium-server-standalone-2.24.1.jar -role hub
启动与主节点同一机子的次节点hub命令:
java -jar selenium-server-standalone-2.24.1.jar -role node -hub http://localhost:4444/grid/register -browser browserName="internet explorer"
启动另外一台机子的两个节点hub命令分别为:
java -jar selenium-server-standalone-2.24.1.jar -Dwebdriver.firefox.bin="D:/Program Files/Mozilla Firefox/firefox.exe" -role webdriver -hub http://192.168.40.24:4444/grid/register -browser browserName=firefox
其中-Dwebdriver.firefox.bin="D:/Program Files/Mozilla Firefox/firefox.exe"这个是传我的firefox浏览器安装地址,不然找不到。
java -jar selenium-server-standalone-2.24.1.jar -Dwebdriver.chrome.driver="D:/chromedriver.exe" -role webdriver -hub http://192.168.40.24:4444/grid/register -port 6666 -browser browserName=chrome
其中-Dwebdriver.chrome.driver="D:/chromedriver.exe",这个是传入我chromedriver.exe的放置地址。端口设为6666,防止和上面firefox的重复。
链接好后,可以在http://localhost:4444/grid/console可以查看到三个子节点的状态。启动详细方法可以参考前面的文章。
Selenium Grid只是提供多系统、多浏览器的执行环境,而不是说任务一个test case丢给它就能并行运行。并行的运行我这里就交给testng了。看下面代码和配制。
testcase 代码 和 testng.xml
testcase代码:
-
packagedriver;
-
-
importjava.net.MalformedURLException;
-
importjava.net.URL;
-
-
importorg.openqa.selenium.By;
-
importorg.openqa.selenium.WebDriver;
-
importorg.openqa.selenium.remote.DesiredCapabilities;
-
importorg.openqa.selenium.remote.RemoteWebDriver;
-
importorg.testng.annotations.AfterMethod;
-
importorg.testng.annotations.BeforeMethod;
-
importorg.testng.annotations.Parameters;
-
importorg.testng.annotations.Test;
-
-
publicclassGridParallelTests{
-
privateWebDriverdr;
-
DesiredCapabilitiestest;
-
StringbaseUrl;
-
-
@Parameters({"browser","nodeUrl","webSite"})
-
@BeforeMethod
-
publicvoidsetUp(Stringbrowser,StringnodeUrl,StringwebSite){
-
baseUrl=webSite;
-
-
if(browser.equals("ie"))test=DesiredCapabilities.internetExplorer();
-
elseif(browser.equals("ff"))test=DesiredCapabilities.firefox();
-
elseif(browser.equals("chrome"))test=DesiredCapabilities.chrome();
-
elseSystem.out.println("browser参数有误,只能为ie、ff、chrome");
-
-
Stringurl=nodeUrl+"/wd/hub";
-
URLurlInstance=null;
-
try{
-
urlInstance=newURL(url);
-
}catch(MalformedURLExceptione){
-
-
e.printStackTrace();
-
System.out.println("实例化url出错,检查一下url格式是否正确,格式为:http://192.168.40.67:5555");
-
}
-
dr=newRemoteWebDriver(urlInstance,test);
-
dr.get(webSite);
-
}
-
-
@Test
-
publicvoidtest(){
-
dr.get(baseUrl);
-
dr.findElement(By.id("kw")).sendKeys("selenium");
-
dr.findElement(By.id("su")).click();
-
try{
-
Thread.sleep(10000);
-
}catch(InterruptedExceptione){
-
-
e.printStackTrace();
-
}
-
System.out.println("title:"+dr.getTitle());
-
}
-
-
-
@AfterMethod
-
publicvoidquit(){
-
dr.close();
-
}
-
-
}
testng.xml:
-
<!DOCTYPEsuiteSYSTEM"http://beust.com/testng/testng-1.0.dtd">
-
-
<suitename="Suite1"parallel="tests"thread-count="5">
-
-
<testname="test1">
-
<parametername="browser"value="ff"/>
-
<parametername="nodeUrl"value="http://192.168.40.67:5555"/>
-
<parametername="webSite"value="http://www.baidu.com"/>
-
<classes>
-
<classname="driver.GridParallelTests"></class>
-
</classes>
-
</test>
-
-
<testname="test2">
-
<parametername="browser"value="chrome"/>
-
<parametername="nodeUrl"value="http://192.168.40.67:6666"/>
-
<parametername="webSite"value="http://www.baidu.com"/>
-
<classes>
-
<classname="driver.GridParallelTests"></class>
-
</classes>
-
</test>
-
-
<testname="test3">
-
<parametername="browser"value="ie"/>
-
<parametername="nodeUrl"value="http://192.168.40.24:5555"/>
-
<parametername="webSite"value="http://www.baidu.com"/>
-
<classes>
-
<classname="driver.GridParallelTests"></class>
-
</classes>
-
</test>
-
</suite>
运行testng.xml就可以了。
如果你是用ant把上面的参数传进去也是可以的。我这里就直接简单地放在testng.xml了。
http://jarvi.iteye.com/blog/1571262
分享到:
相关推荐
Selenium WebDriver Practical Guide will guide you through the various APIs of WebDriver which should be used in automation tests, followed by a discussion of the various WebDriver implementations ...
精通Selenium WebDriver 3.0,第二版 这是Packt发行的的代码库。... 并学习如何使用Docker容器快速启动Selenium Grid。 在最后的几章中,您将通过一系列场景来演示如何扩展Selenium以使其与外部库和应用程序一起使用,
Selenium WebDriver 3 Practical Guide will walk you through the various APIs of Selenium WebDriver, which are used in automation tests, followed by a discussion of the various WebDriver implementations...
selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium Remote Control)和测试的并行处理(Selenium Grid)。 Selenium的核心Selenium Core基于JsUnit,完全由...
开始使用Java进行Selenium WebDriver测试[视频] 这是发行的的代码存储库。 它包含从头到尾完成视频课程所需的所有支持项目文件。 关于视频课程 本课程将使您了解Selenium WebDriver的功能。 您将通过实践测试步骤并...
Moving on, the book shows you how to utilize the Advanced User Interactions API, how you can run any JavaScript you need through Selenium, and how to quickly spin up a Selenium Grid using Docker ...
Selenium Server (Grid) The Selenium Server is needed in order to run Remote Selenium WebDriver (Grid).
Cucumberjvm-webdriver-example ... 在本地启动 selenium Grid Hub(根据需要更改 selenium-server 版本) java -jar selenium-server-standalone-2.44.0.jar -role hub 在本地启动一个 selenium 网
通过将客户端发送的命令路由到远程浏览器实例,Selenium Grid允许在远程计算机(虚拟或真实)上执行WebDriver脚本。它旨在提供一种在多台计算机上并行运行测试的简便方法。 Selenium Grid允许我们在多台计算机上并行...
Explore the Selenium WebDriver API for easy implementations of small to complex operations on browsers and web applications Packed with easy and practical examples that get you started with Selenium...
The second edition of Mastering Selenium 3.0 WebDriver starts by showing you how to build your own Selenium framework with Maven. You’ll then look at how you can solve the difficult problems that you...
Selenium 也是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE、Mozilla Firefox、Google Chrome等。这个工具的主要功能包括:测试与浏览器的兼容性...
Selenium WebDriver TestNg 魅力2 Maven执行命令要运行测试并生成魅力报告: mvn clean site -P [singleThreaded,nogrid,grid,parallelSuite]singleThreaded-按顺序执行测试,省略并行执行nogrid-在本地执行测试grid...
The Selenium WebDriver 3.x Technology is an open source API available to test both Browser and Mobile applications. It is completely platform independent in that tests built for one browser or mobile ...
Selenium主要包括三部分:SeleniumIDE、SeleniumWebDriver和SeleniumGrid:SeleniumIDE:Firefox的一个扩展,它可以进行录制回放,并可以把录制的操作以多种语言(例如java,python等)的形式导出成测试用例。...
Selenium网格 安装selenium-webdriver节点包【需要设置 package.json】 启动集线器 java -jar ... 启动[many] phantom 实例phantomjs --webdriver=8910 --webdriver-selenium-grid-hub= 运行测试节点 index.js
Set up Selenium Grid for faster and parallel running of tests, increasing test coverage and reducing test execution time for cross-browser testing Build extended Selenium WebDriver tests for ...
使用Selenium Grid和Docker Swarm进行并发Web抓取 想学习如何建立这个项目? 查看。 要使用这个项目吗? 货叉/克隆 创建并激活虚拟环境 安装要求 Digital Ocean并访问令牌 将令牌添加到您的环境中: (env)$ ...
在karma.conf.js文件中,定义一些继承自SeleniumGrid自定义启动器,将Selenium Grid的URL作为gridUrl 。 module . exports = function ( karma ) { ... config . set ( { ... customLaunchers : {
Over 90 recipes to help you build and run automated tests for your web applications with Selenium WebDriver About This Book Learn to leverage the power of Selenium WebDriver with simple examples that...