欢迎所有的测试博友对我的文章以及见解给出建议,意见。枞榕之测试所见所得,将从现在开始与大家一同分享。。。

testNG中独立出公共模块的做法

上一篇 / 下一篇  2013-01-24 18:40:29 / 个人分类:testNG

最近在学习testNG+selenium+java自动化测试工具,但过程中遇到了如下问题,现在已经基本解决,主要是换了一下思路。写此日志以便日后找出更好的解决方法。51Testing软件测试网#r ?8s,G~6Z#]4N
问题:51Testing软件测试网#gHz\dU
 在执行测试过程中要执行如下用例:
UW;m6{'dd*l|QD01.打开网站首页(如: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中找页面对象,但是页面对象却存在于第一次打开的页面中。
y UyY1m0原本的代码如下:
2["El"|L@1o^0@DataProvider(name="driver")
5n HYr!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 DVZV B1o
}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:?^

?9mup$V!MT{0语法上没有问题,但是总是会报错,大意如:dataprovider只能返回如Object[][]类型的对象。51Testing软件测试网)i#n}Jt4w
调了两天还是不行(由于初学,实在是笨)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";
H IW [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软件测试网?!bnvm5Wf'I
import org.testng.annotations.Test;
zp?DL6]l2o0import org.testng.internal.thread.TestNGThread;51Testing软件测试网6O[#J eC*Vk'I _(?
import web.Util.Utils;51Testing软件测试网-@(U5MN-CP
public class ProductList {
!X%q)o P$_B0    private static WebDriver driver;
W+a[&F+H WT%R0    //打开wap站首页51Testing软件测试网)m;d4~7Z{ g)b8K$p
    @Test
K$a ?"F]0    public void SearchWordClick() throws InterruptedException {51Testing软件测试网JjT3a?3U9E
        String url="http://m.vancl.com/user/switch/1";
(u0^(k gJ{0        driver=Utils.OpenWap(url);51Testing软件测试网Ia[H*uN~
        int i = Utils.getRandom(3);51Testing软件测试网x)oYex*e$~~ Q Xv
        // 搜索关键词的xpath string51Testing软件测试网zbJp}d;Gl
        String KeyWordString = "//div[@class='nav']/a[" + i + "]";
7Q/T @v:Ae3\]0        WebElement KeywordLink = Utils.getElement(KeyWordString, driver);51Testing软件测试网y"CX7PXl0|Y+Nd
        KeywordLink.click();51Testing软件测试网*W8S t2^m2K7jnS~
        TestNGThread.sleep(10000);
gX*Qu6e0   
+H*i sH.jXV8l0    }
.|DT+Z`0    @Test(dependsOnMethods="SearchWordClick")51Testing软件测试网(^6e:Ua;K|`IL
    public void CheckSearchWordList() throws InterruptedException{
2Z|Fe7G#O0        /**
7}y1t|_~0         * 检查搜索列表页是否正确加载
#nGfJpj0         **/
gp]Q&]3UY9T0        try {51Testing软件测试网Er8|"P FuL Op
            WebElement cntchk = driver.findElement(By
3c tzv%sRh0                    .xpath("//div[@class='searchorder']/a[1]"));
}$S0?G(h#L0            String chkstr = cntchk.getText().trim();
,f%`9UI)L\^N4pQ0            if (chkstr.equals("属性导航") || chkstr.equals("分类导航")) {
W Ao*X:JR$D0                System.out.println("找到了验证的标记:" + chkstr);
*k1HXO+]B0                System.out.print("已成功加载商品列表页");51Testing软件测试网sW3@.r BL
            }51Testing软件测试网t9MQ&@0ol/Kh
        } catch (Exception e) {51Testing软件测试网9e/u9o H m~MJ
            System.out.println("商品列表页加载失败" + e.getMessage());51Testing软件测试网 x1~N I`hA*w(_
        }
a\ v%mY0        TestNGThread.sleep(3000);
ot#M{Z.t-V0        driver.close();51Testing软件测试网hvqbV,t
    }
~.M'N"~~-| ?z6V3{0}
51Testing软件测试网V0w \ j)Pu cR9ZF
51Testing软件测试网a3ENf^1B3[
初学testNG,一边翻资料,一边看API,一边调试,一边百度。真是头大,但是慢慢来吧,点点积累谁让咱起步晚呢~~~如果有高手有好的解决方法,还请告知我,非常感激。51Testing软件测试网h8r_Rz*L wc

TAG: TestNG testNG testng

 

评分:0

我来说两句

Open Toolbar