[转]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,启动remotecontrol时告知hub的位置,这样这些rc就可以注册到hub上,测试程序与hub通讯,当测试被并发地发给hub时,hub会自动将这些测试命令分发给已经注册的rc,rc接到命令后执行测试。

到这里D:\opensource\selenium\selenium-grid-1.0.4\doc\website\download.html下个最新版的seleniumgrid,里面的文档有详细的使用方法,现在简诉如下:

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 staticcom.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessio nStorage.closeSeleniumSession;
import staticcom.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessio nStorage.session;
import staticcom.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessio nStorage.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");
  }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值