`
xinklabi
  • 浏览: 1560270 次
  • 性别: Icon_minigender_1
  • 来自: 吉林
文章分类
社区版块
存档分类
最新评论

TestNG如何并行执行测试用例以及结合Selenium Grid(分布式测试)

 
阅读更多

1. testNG并发执行测试用例的方法
在配置testNG的test.xml中,通过suit标签的属性可以指定并发执行,例如:
<suite name="My suite" parallel="methods" thread-count="5">
指定每一个测试方法使用单独的线程,总线程数为5
parallel可以为:
  methods : 每个method使用一个线程
  tests :每个<test>标签中的所有方法使用一个线程
  classes : 每个class使用一个线程

 

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

 

Grid 只是提供运行脚本的环境,无法决定脚本以什么样的形式去跑(并行的方式,或者多环境的方式)。脚本以什么样的形式跑,由脚本本身和脚本的runner (一般用junit或者TestNG)的配置所决定.

并行和多环境的是怎么实现的。

 
第 一,并行。并行很简单。。脚本就是普通的脚本。然后如果你的runner支持并行运行的话。你就配置成并行。那运行起来就是并行的。。目前junit本身 不支持并行,一般都是用TestNG. 只要让TestNG 并行的运行测试,无论你的测试环境是由一个grid拖几个rc组成,还是 单一一个rc组成。。实际上 运行起来 测试就是并行的,唯一的区别是。如果用了grid的话,grid会把并发数量平均分配到不同的rc上去,然后每个rc会启动一个浏览器运行测试。而没有用 grid情况就是同一个rc直接跑多个,就是一个rc直接打开多个浏览器窗口运行多个测试。

 
第 二,多环境。 说多环境,这里也要澄清一下。 不是说给一个普通的测试脚本,丢给grid就可以自动实现多环境同时测试了。那是完全错误的。。这里需要几点。1. 一个普通脚本能测试一个某一种环境。所以如果你要测试多个环境就需要多个脚本。而这些脚本的区别仅在于setup的时候定义 DefaultSelenium("localhost",4444,"*firefox",Url); 这里。 因此两个测试方法也可以到同一个文件里面。写两个不同的方法,不同的定义DefaultSelenium部分,但调用同样的测试步骤。(就是 selenium.open...开始到selenium.stop...). 这样一个文件下就算包含测试两个不同环境的测试方法了。
2. 让这两个测试方法,并行运行。。那又是testng的事情了。配置testng的suite。不细说。可以查看testng的文档。
3. grid配置的测试的rc环境,里面需要有刚才两个测试方法所需要的环境。
 
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;

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");
 }
 
 【略】
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics