testNG中独立出公共模块的做法
上一篇 / 下一篇 2013-01-24 18:40:29 / 个人分类:testNG
最近在学习testNG+selenium+java自动化测试工具,但过程中遇到了如下问题,现在已经基本解决,主要是换了一下思路。写此日志以便日后找出更好的解决方法。51Testing软件测试网#r?8s,G ~6Z#]4N
问题:51Testing软件测试网#gHz\dU
在执行测试过程中要执行如下用例:
UW;m6{'dd*l|Q D01.打开网站首页(如:m.test.com)51Testing软件测试网&`h&X| o u4mo
2.在首页的搜索框中输入某个关键词,如:鞋子51Testing软件测试网 K8O.c.]Mv
3.检查打开的鞋子商品列表显示的数据列表是否为每页10条。
-ac,s2j&}-\)~0 以上这个步骤仅仅是众多用例中的一个,但是对于第一步的这个动作则是众多测试用例中都要首先进行操作的步骤。我想要把这个步骤独立出来,做成一个方法,让这个方法可以返回当前打开的网页的WebDriver于是我想到了testNG中的dataProvider但是经过N次的测试,此方法都不可以返回WebDriver这种对象类型。51Testing软件测试网So7R!DE"W]
为什么要将这个打开网站的方法参数化:51Testing软件测试网!ev-})M2nd5T~#oCj
1.因为多数用例都要使用这个步骤
*Gt-kdd;L0M)eK02.如果重新定义一个WebDriver,则会重新在新的WebDriver中找页面对象,但是页面对象却存在于第一次打开的页面中。
yUyY1m0原本的代码如下:
2["El"|L@1o^0@DataProvider(name="driver")
5nHYr!m']e0public static WebDriver BaseTest(){
`;g g~ALe l:D0 WebDriver driver=new FirefoxDriver();51Testing软件测试网0OW$r_+a+H6J|m9Ct
driver.get("http://m.test.com");
/_gc!?e~^0 return driver;51Testing软件测试网VF*d DVZ VB1o
}51Testing软件测试网?HK Bjyx9h\7g
然后在需要这个webdriver的地方进行调用如下:
Eo(C7~be0@Test(dataprovider="driver")
_p7Pqzc(\8E0public openpage(WebDriver driver){
;a1w5G([Nq1Y0//省略}51Testing软件测试网O&L;J},Eb[T&u:?^
?9m up$V!MT{0语法上没有问题,但是总是会报错,大意如:dataprovider只能返回如Object[][]类型的对象。51Testing软件测试网)i#n}J t4w
调了两天还是不行(由于初学,实在是笨)51Testing软件测试网(wX0u-J t'U
最后的解决方法是,在另一个类中写一个方法,每次要用到这个driver时去调用这个方法,这个类中包含众多方法(如获取随机数,查找某个页面对象…………)最后的代码如下:
t;K0jvr \0 public static FirefoxDriver OpenWap(String url){51Testing软件测试网Xa*x:I']W)q
FirefoxDriver driver=new FirefoxDriver();51Testing软件测试网7h@6J4}%[X vj yx
driver.get(url);
T*{)? @9u\,q sAc0 return driver;51Testing软件测试网;|0U)_Xv)OG,WR
}51Testing软件测试网3A\M+v/qEM.ZI!^
1TRJB0y0在另一个类中的调用代码如下:51Testing软件测试网$MKv%zK`|%J`
private static WebDriver driver;51Testing软件测试网`"S Zc[
String url="http://m.vancl.com/user/switch/1";
HIW [TU.j!?cd0 driver=Utils.OpenWap(url);51Testing软件测试网q9q(QnL{9N9@V#e
于是整个调试顺利通过。整体的调用代码如下:
-F)r#XK mJ0import org.openqa.selenium.By;51Testing软件测试网qV"a%}n2zU\*\6r
import org.openqa.selenium.WebDriver;
eIz,UzZ/C"}A0import org.openqa.selenium.WebElement;51Testing软件测试网?!bn vm5Wf'I
import org.testng.annotations.Test;
问题:51Testing软件测试网#gHz\dU
在执行测试过程中要执行如下用例:
UW;m6{'dd*l|Q D01.打开网站首页(如:m.test.com)51Testing软件测试网&`h&X| o u4mo
2.在首页的搜索框中输入某个关键词,如:鞋子51Testing软件测试网 K8O.c.]Mv
3.检查打开的鞋子商品列表显示的数据列表是否为每页10条。
-ac,s2j&}-\)~0 以上这个步骤仅仅是众多用例中的一个,但是对于第一步的这个动作则是众多测试用例中都要首先进行操作的步骤。我想要把这个步骤独立出来,做成一个方法,让这个方法可以返回当前打开的网页的WebDriver于是我想到了testNG中的dataProvider但是经过N次的测试,此方法都不可以返回WebDriver这种对象类型。51Testing软件测试网So7R!DE"W]
为什么要将这个打开网站的方法参数化:51Testing软件测试网!ev-})M2nd5T~#oCj
1.因为多数用例都要使用这个步骤
*Gt-kdd;L0M)eK02.如果重新定义一个WebDriver,则会重新在新的WebDriver中找页面对象,但是页面对象却存在于第一次打开的页面中。
yUyY1m0原本的代码如下:
2["El"|L@1o^0@DataProvider(name="driver")
5nHYr!m']e0public static WebDriver BaseTest(){
`;g g~ALe l:D0 WebDriver driver=new FirefoxDriver();51Testing软件测试网0OW$r_+a+H6J|m9Ct
driver.get("http://m.test.com");
/_gc!?e~^0 return driver;51Testing软件测试网VF*d DVZ VB1o
}51Testing软件测试网?HK Bjyx9h\7g
然后在需要这个webdriver的地方进行调用如下:
Eo(C7~be0@Test(dataprovider="driver")
_p7Pqzc(\8E0public openpage(WebDriver driver){
;a1w5G([Nq1Y0//省略}51Testing软件测试网O&L;J},Eb[T&u:?^
?9m up$V!MT{0语法上没有问题,但是总是会报错,大意如:dataprovider只能返回如Object[][]类型的对象。51Testing软件测试网)i#n}J t4w
调了两天还是不行(由于初学,实在是笨)51Testing软件测试网(wX0u-J t'U
最后的解决方法是,在另一个类中写一个方法,每次要用到这个driver时去调用这个方法,这个类中包含众多方法(如获取随机数,查找某个页面对象…………)最后的代码如下:
t;K0jvr \0 public static FirefoxDriver OpenWap(String url){51Testing软件测试网Xa*x:I']W)q
FirefoxDriver driver=new FirefoxDriver();51Testing软件测试网7h@6J4}%[X vj yx
driver.get(url);
T*{)? @9u\,q sAc0 return driver;51Testing软件测试网;|0U)_Xv)OG,WR
}51Testing软件测试网3A\M+v/qEM.ZI!^
1TRJB0y0在另一个类中的调用代码如下:51Testing软件测试网$MKv%zK`|%J`
private static WebDriver driver;51Testing软件测试网`"S Zc[
String url="http://m.vancl.com/user/switch/1";
HIW [TU.j!?cd0 driver=Utils.OpenWap(url);51Testing软件测试网q9q(QnL{9N9@V#e
于是整个调试顺利通过。整体的调用代码如下:
-F)r#XK mJ0import org.openqa.selenium.By;51Testing软件测试网qV"a%}n2zU\*\6r
import org.openqa.selenium.WebDriver;
eIz,UzZ/C"}A0import org.openqa.selenium.WebElement;51Testing软件测试网?!bn vm5Wf'I
import org.testng.annotations.Test;