Selenium2(webdriver)入門之TestNG的安裝與簡單使用

賀滿發表於2013-12-20

 上一篇已經搭建好了Eclipse+selenium2的環境,這一篇主要記錄下TestNG的使用。

一、在Eclipse中安裝TestNG

1、開啟eclipse-->help-->Install New Software-->Add,輸入Name和Location後,點選OK。

TestNG官方下載地址:http://testng.org/doc/download.html

2、然後選中TestNG,單擊Next安裝

3、安裝好TestNG後重啟eclipse檢視是否安裝好,Help-->About Eclipse-->Installation Details,如圖:

二、使用TestNG來執行單個測試案例:

1、新建TestHelloWorldTestNG.java類,目錄結構如下:

2、測試程式碼: 

 1 package com.selenium;
 2 
 3 import org.openqa.selenium.By;
 4 import org.openqa.selenium.WebDriver;
 5 import org.openqa.selenium.WebElement;
 6 import org.openqa.selenium.firefox.*;
 7 import org.testng.annotations.*;
 8 import org.testng.Assert;
 9 
10 
11 public class TestHelloWorldTestNG {
12 
13     WebDriver driver;
14     @Test
15     public void helloWorld() throws Exception {        
16         //如果火狐瀏覽器沒有預設安裝在C盤,需要制定其路徑
17         //System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla firefox/firefox.exe"); 
18         driver = new FirefoxDriver();
19         driver.get("http://www.baidu.com/");
20         
21         driver.manage().window().maximize();
22         
23         WebElement txtbox = driver.findElement(By.name("wd"));
24         txtbox.sendKeys("Glen");
25         
26         WebElement btn = driver.findElement(By.id("su"));
27         btn.click();
28                 
29         String expectedTitle = "Glen_百度搜尋";
30         String actualTitle = driver.getTitle();
31         
32         Assert.assertEquals(actualTitle,expectedTitle);            
33     }
34     
35     @AfterTest
36     public void tearDown(){
37         driver.quit();
38     }
39 
40 }

3、然後右鍵Run As-->TestNG Test,執行結果如下:  

[TestNG] Running:
  C:\Users\Administrator\AppData\Local\Temp\testng-eclipse-332204777\testng-customsuite.xml

PASSED: helloWorld

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 1 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@15d56d5: 34 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@19106c7: 11 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@1632c2d: 4 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@cdedfd: 11 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@13caecd: 22 ms

 

、使用TestNG來執行多個測試案例:

1、增加一個失敗的測試類TestHelloWorldTestNG_Fail.java:

 1 package com.selenium;
 2 
 3 import org.openqa.selenium.By;
 4 import org.openqa.selenium.WebDriver;
 5 import org.openqa.selenium.WebElement;
 6 import org.openqa.selenium.firefox.*;
 7 import org.testng.annotations.*;
 8 import org.testng.Assert;
 9 
10 
11 public class TestHelloWorldTestNG_Fail {
12 
13     WebDriver driver;
14     @Test
15     public void helloWorld() throws Exception {        
16         //如果火狐瀏覽器沒有預設安裝在C盤,需要制定其路徑
17         //System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla firefox/firefox.exe"); 
18         driver = new FirefoxDriver();
19         driver.get("http://www.baidu.com/");
20         
21         driver.manage().window().maximize();
22         
23         WebElement txtbox = driver.findElement(By.name("wd"));
24         txtbox.sendKeys("Glen");
25         
26         WebElement btn = driver.findElement(By.id("su"));
27         btn.click();
28                 
29         String expectedTitle = "Glen_百度";
30         String actualTitle = driver.getTitle();
31         
32         Assert.assertEquals(actualTitle,expectedTitle);
33     }
34     
35     @AfterTest
36     public void tearDown(){
37         driver.quit();
38     }
39 
40 }

 2、在專案下新建一個Suite.xml檔案:  

<suite name="seleniumcn.cn.demo">    
    <test name="test_seleniumcn" >
        <classes>
            <class name="com.selenium.TestHelloWorldTestNG"/>
            <class name="com.selenium.TestHelloWorldTestNG_Fail"/>                
        </classes>
    </test>  
</suite>

3、目錄結構:

4、右鍵Suite.xml檔案,Run As->TestNG Suite,如此就會執行suite.xml檔案中所有的案例。  

[TestNG] Running:
  F:\workspace\WebDriverDemo\Suite.xml

===============================================
seleniumcn.cn.demo
Total tests run: 2, Failures: 1, Skips: 0
===============================================

5、右鍵WebDriverDemo重新整理專案,目錄中會新增加一個test.output資料夾,開啟 index.html可以看一個簡單的報告。

目錄:

報告:

 

相關文章