testNG可以设置为并发执行测试用例。selenium grid可以通过grid hub将测试用例分别转发给不同的remote control/浏览器对,而且这些remote control/浏览器对可以位于不同的机器上,这样两者结合就可以实现可伸缩的自动web测试。
1. testNG并发执行测试用例的方法
在配置testNG的test.xml中,通过suit标签的属性可以指定并发执行,例如:
<suite name="My suite" parallel="methods" thread-count="5">
指定每一个测试方法使用单独的线程,总线程数为5
parallel可以为:
methods : 每个method使用一个线程
tests :每个<test>标签中的所有方法使用一个线程
classes : 每个class使用一个线程
可以参看这里:http://testng.org/doc/documentation-main.html#parallel-running
如果是使用ant启动测试的话,还可以在ant脚本中指定该参数,可以参照selenium grid中build.xml的代码:
<target name="run-demo-for-multiple-environments"
description="Run Selenium tests in parallel for multiple environments">
<java classpathref="demo.classpath" classname="org.testng.TestNG" failonerror="true" >
【略】
<arg value="-d" />
<arg value="${basedir}/target/reports" />
<arg value="-suitename" />
<arg value="Selenium Grid Demo In Parallel For Multiple Environments" />
【在这里】
<arg value="-parallel"/>
<arg value="methods"/>
<arg value="-threadcount"/>
<arg value="10"/>
<arg value="-testclass"/>
<arg value="com.thoughtworks.selenium.grid.demo.WebTestInvolvingMultiEnvironments"/>
</java>
</target>
2 使用selenium grid
grid的机制是,启动一个hub,然后启动多个remote control,启动remote control时告知hub的位置,这样这些rc就可以注册到hub上,测试程序与hub通讯,当测试被并发地发给hub时,hub会自动将这些测试命令分发给已经注册的rc,rc接到命令后执行测试。
到这里D:\opensource\selenium\selenium-grid-1.0.4\doc\website\download.html下个最新版的selenium grid,里面的文档有详细的使用方法,现在简诉如下:
selenium grid要求安装jdk和ant,其目录下有一build.xml文件,其中定义的主要target如下:
检查配置:ant sanity-check
启动hub:ant launch-hub
其中hub的配置是在grid_configuration.yml文件里,该文件要在classpath的根里
启动后在http://host:port/console可以看到hub当前的状态
启动rc(按默认配置):ant run-demo-in-sequence
启动rc(指定参数):ant -Dport=5555 -Dhost=192.168.1.16 -DhubURL=http://192.168.1.1:4444 launch-remote-control
其中host和port是rc的地址,hubURL是hub的地址
正常启动后,可以在http://host:port/console看到hub中当前注册的rc
3 测试代码的写法
测试代码使用grid tool中
的ThreadSafeSeleniumSessionStorage在每个用例前实例化一selenium(实际是一DefaultSelenium),在用例后将其关闭。初始化selenium时与一般初始化DefaultSelenium一样,指定host,port,browser和indexPage,这里的host和port不是rc中的server,而是hub。
以下是示例代码:
import static com.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessionStorage.closeSeleniumSession;
import static com.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessionStorage.session;
import static com.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessionStorage.startSeleniumSession;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.thoughtworks.selenium.Selenium;
/**
* Tests for the footer of a page. This footer always contains a copyright string and a
* timestamp for the time the target was generated.
* @author pmorales
*/
public class Sample {
private Selenium selenium;
@BeforeMethod
@Parameters({"seleniumHost", "seleniumPort", "browser", "webSite"})
protected void startSession(String seleniumHost, int seleniumPort, String browser, String webSite) throws Exception {
startSeleniumSession(seleniumHost, seleniumPort, browser, webSite);
selenium = session();
selenium.setTimeout("120000");
}
@AfterMethod
protected void closeSession() throws Exception {
closeSeleniumSession();
}
@Test
public void test1() {
selenium.open("/");
selenium.type("q", "test1");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
}
【略】
}
在testNG的配置文件test.xml中指定seleniumHost, seleniumPort, browser, webSite等参数,当有多个测试用例和多个rc时,跑测试时就会看到测试在多个rc上并发运行了。
分享到:
相关推荐
java+selenium+maven+testng自动化测试框架实例(实际项目)
ant+selenium+testng自动化测试方案
Selenium+TestNG 框架的WEB自动化源码 Selenium+TestNG 框架的WEB自动化源码
讲解如何使用Selenium 和 TestNG 进行编程式测试
使用Java+Maven+Selenium+TestNG+Jedis+Jenkins搭建的WebUI自动化测试框架,资源的大体介绍如下链接http://note.youdao.com/noteshare?id=dc564343fd126f497074f6d7560c9f5e&sub=387EB3B1BAC945CEA71A5BDBC6484473
Selenium+TestNG自动化测试
selenium+java+testng+maven测试用例执行,testng执行
仅仅用于初学者交流,意在与自动化测试初学者进行相互学习交流,简单的实现了java+selenium+testng参数化打开百度搜索
全栈自动化测试实战 基于testng,httpclient,selenium.appium 书本上的东西
个人觉得这个工具比任何自动化测试框架都好使,使用关键字和数据双驱动,不需要写一行代码,无需维护脚本,只需要维护用例数据。Web元素只需要在Chrome中复制xPath即可,定位非常高效。 工具安全无木马,目的只为...
终极自动化测试环境搭建:Selenium+Eclipse+Junit+TestNG+Python。 使用最新版本软件,所有步骤均有截图,5个代码实例。
采用Maven+Selenium+TestNG+TestNG-xslt实现数据驱动自动化测试框架的实现,思想主要是采用PO模式进行代码封装以及对数据文件的读取使用。
本文主要介绍了关于docker利用selenium+testng实现web自动化的相关内容,下面话不多说了,来一起看看详细的介绍吧 Windows下selenium+testng的web自动化环境搭建 做过自动化的人,肯定对selenium web环境的搭建非常...
Web自动使用Selenium Webdriver&TestNG建立一个Web测试框架。
通过Selenium Ide录制脚本,修改format导出tesng/webdriver格式的java文件,支持测试失败自动截图。
ant + Selenium2 + testNG 整合的相关例子,可供参考
1、支持多并行测试 2、支持所有的云解决方案,如 BrowserStack,SauceLabs,Testingbot 3、数据驱动测试 4、一步级报告生成 5、自动屏幕快照的失败的测试步骤 ...8、自动重新运行失败的测试用例...
该源码包是含有selenium+testng框架的web购物项目源代码,框架中采用PageObject设计模式
终极自动化测试环境搭建:Selenium+Eclipse+Junit+TestNG+Python