您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > TestNG
testNG中獨(dú)立出公共模塊的做法
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/12/25 11:41:38 ] 推薦標(biāo)簽:測試工具 軟件測試 單元測試

近在學(xué)習(xí)testNG+selenium+java自動(dòng)化測試工具,但過程中遇到了如下問題,現(xiàn)在已經(jīng)基本解決,主要是換了一下思路。寫此日志以便日后找出更好的解決方法。
問題:
 在執(zhí)行測試過程中要執(zhí)行如下用例:
1.打開網(wǎng)站首頁(如:m.test.com)
2.在首頁的搜索框中輸入某個(gè)關(guān)鍵詞,如:鞋子
3.檢查打開的鞋子商品列表顯示的數(shù)據(jù)列表是否為每頁10條。
  以上這個(gè)步驟僅僅是眾多用例中的一個(gè),但是對(duì)于第一步的這個(gè)動(dòng)作則是眾多測試用例中都要首先進(jìn)行操作的步驟。我想要把這個(gè)步驟獨(dú)立出來,做成一個(gè)方法,讓這個(gè)方法可以返回當(dāng)前打開的網(wǎng)頁的WebDriver于是我想到了testNG中的dataProvider但是經(jīng)過N次的測試,此方法都不可以返回WebDriver這種對(duì)象類型。
為什么要將這個(gè)打開網(wǎng)站的方法參數(shù)化:
1.因?yàn)槎鄶?shù)用例都要使用這個(gè)步驟
2.如果重新定義一個(gè)WebDriver,則會(huì)重新在新的WebDriver中找頁面對(duì)象,但是頁面對(duì)象卻存在于第一次打開的頁面中。
原本的代碼如下:
@DataProvider(name="driver")
public static WebDriver BaseTest(){
 WebDriver driver=new FirefoxDriver();
 driver.get("http://m.test.com");
 return driver;
}
然后在需要這個(gè)webdriver的地方進(jìn)行調(diào)用如下:
@Test(dataprovider="driver")
public openpage(WebDriver driver){
//省略}

語法上沒有問題,但是總是會(huì)報(bào)錯(cuò),大意如:dataprovider只能返回如Object[][]類型的對(duì)象。
調(diào)了兩天還是不行(由于初學(xué),實(shí)在是笨)
后的解決方法是,在另一個(gè)類中寫一個(gè)方法,每次要用到這個(gè)driver時(shí)去調(diào)用這個(gè)方法,這個(gè)類中包含眾多方法(如獲取隨機(jī)數(shù),查找某個(gè)頁面對(duì)象…………)后的代碼如下:
   public static  FirefoxDriver OpenWap(String url){
        FirefoxDriver driver=new FirefoxDriver();
        driver.get(url);
        return driver;
    }
  
在另一個(gè)類中的調(diào)用代碼如下:
  private static WebDriver driver;
   String url="http://m.vancl.com/user/switch/1";
   driver=Utils.OpenWap(url);
于是整個(gè)調(diào)試順利通過。整體的調(diào)用代碼如下:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
import org.testng.internal.thread.TestNGThread;
import web.Util.Utils;
public class ProductList {
    private static WebDriver driver;
    //打開wap站首頁
    @Test
    public void SearchWordClick() throws InterruptedException {
        String url="http://m.vancl.com/user/switch/1";
        driver=Utils.OpenWap(url);
        int i = Utils.getRandom(3);
        // 搜索關(guān)鍵詞的xpath string
        String KeyWordString = "//div[@class='nav']/a[" + i + "]";
        WebElement KeywordLink = Utils.getElement(KeyWordString, driver);
        KeywordLink.click();
        TestNGThread.sleep(10000);
  
    }
    @Test(dependsOnMethods="SearchWordClick")
    public void CheckSearchWordList() throws InterruptedException{
        /**
         * 檢查搜索列表頁是否正確加載
         **/
        try {
            WebElement cntchk = driver.findElement(By
                    .xpath("//div[@class='searchorder']/a[1]"));
            String chkstr = cntchk.getText().trim();
            if (chkstr.equals("屬性導(dǎo)航") || chkstr.equals("分類導(dǎo)航")) {
                System.out.println("找到了驗(yàn)證的標(biāo)記:" + chkstr);
                System.out.print("已成功加載商品列表頁");
            }
        } catch (Exception e) {
            System.out.println("商品列表頁加載失敗" + e.getMessage());
        }
        TestNGThread.sleep(3000);
        driver.close();
    }
}

初學(xué)testNG,一邊翻資料,一邊看API,一邊調(diào)試,一邊百度。真是頭大,但是慢慢來吧,點(diǎn)點(diǎn)積累誰讓咱起步晚呢~~~如果有高手有好的解決方法,還請(qǐng)告知我,非常感激。

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd