1. 程式人生 > >通過junit/TestNG+java 實現自動化測試

通過junit/TestNG+java 實現自動化測試

第一步 安裝JDK

  JDk1.7.

下載地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

一路猛擊‘下一步’,OK。安裝完成後配置環境變數:

  JAVA_HOME = E:\Java\Java\jdk1.7.0_15

  PATH = %JAVA_HOME%\bin

  CLASSPATH = .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar

配置完環境變數後,CMD命令列輸入:java -version,返回如下結果,則表示安裝成功:

 

 

第二步 下載Eclipse

下載地址:http://www.eclipse.org/download/ 

最新的Eclipse Standard 4.3, 198 MB,下載的都是不用安裝的,解壓出來後直接用。

 

第三步 下載Selenium IDE、SeleniumRC、IEDriverServer

下載地址:http://www.seleniumhq.org/download/

  1、  Selenium IDE:selenium-ide-2.5.0.xpi 用來在Firefox上錄製指令碼。 

  2、  Selenium RC:selenium-server-standalone-2.40.0.jar 模擬伺服器端,selenium 1.0執行指令碼時需要單獨啟動該jar包, selenium webdriver無需單獨啟動。

  3、  IEDriverServer:IEDriverServer_Win32_2.40.0.zip IE驅動

  

 

這裡,我將下載得到的所有檔案,全存放在E:\eclipse\selenium下面,方便管理:

 

第四步 下載Firefox

 

下載地址:http://www.firefox.com.cn/download/

 

下載得到檔案:Firefox-latest.exe,最好是下載Firefox 25簡體中文版,後續版本有人說通過Selenium會啟動不了Firefox。

 

 

第五步 安裝IDE、Firebug、Xpath checker、Xpath finder

 

安裝完Firefox後,開啟Firefox,把前面下載的selenium-ide-2.5.0xpi拖放到Firefox,彈出下圖後,安裝即可。

Firebug、Xpath checker、Xpath finder,開啟firefox瀏覽器,選擇工具――附加元件,開啟附加元件管理器頁面,搜尋firebug、Xpath。

將查詢到的firebug、xpath checker、xpath finder都裝上,重啟瀏覽器後生效: 

SeleniumIDE、Firebug和xpath的用法,可以百度Selenium私房菜(新手入門教程).pdf,裡面有很好的說明。

 

第六步 啟動SeleniumRC

注意:selenium 1.0需要啟動單獨rc,webdriver則不需要啟動。

啟動seleniumRC的方法:
cmd命令列進入selenium-server-standalone-2.40.0.jar存放目錄,輸入如下命令
java -jar selenium-server-standalone-2.40.0.jar

 

為了方便,可以將啟動命令寫一個bat來執行,Run_selenium.bat,內容如下:

@echo off
cd E:\eclipse\selenium
E:
java -jar selenium-server-standalone-2.40.0.jar

 

第七步 Eclipse執行Selenium的Java例項

-----7.1

開啟Eclipse,新建一個工程File—new—Java Project

 

-----7.2

輸入工程名:Selenum,next

-----7.3

接下來,視窗進入Java Settings,選擇Libraries,點選Addlibrary。

引用Junit4的Jar包(E:\eclipse\plugins\org.junit_4.11.0.v2XXXX)。

然後點選Add External Jars..,

引用Selenium相關的包(E:\eclipse\selenium),最終Libraries如下:

 

 

完成後,Java檢視如下:

 

 

-----7.4

右擊src,new->package新建一個包Selenium_Test,

再右擊包Selenium_Test,new->class,新建一個Class類Case1.java,最終效果如下:

 

 

-----7.5

下面我們來用IE瀏覽器執行一個例項,修改Case1.java,這裡我們用selenium webdriver來寫程式碼,程式碼如下:

複製程式碼
package Selenium_Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.DesiredCapabilities; public class Case1 { public static void main(String[] args) { System.setProperty("webdriver.ie.driver", "E:\\eclipse\\selenium\\IEDriverServer.exe");//注意這裡IEDriverServer.exe的檔案存放路徑 DesiredCapabilities ieCapabilities = DesiredCapabilities .internetExplorer(); ieCapabilities .setCapability( InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); //new一個webdriver物件 WebDriver driver = new InternetExplorerDriver(ieCapabilities); //上面這一段是用來解決IE安全設定提示的 //通過webdriver的get方法呼叫瀏覽器,開啟網頁:http://www.google.com.hk driver.get("http://www.google.com.hk"); //通過頁面元素的name=q定位到查詢輸入框  WebElement element = driver.findElement(By.name("q")); //在輸入框輸入‘hello Selenium!’ element.sendKeys("hello Selenium!"); //提交查詢  element.submit(); //等待,超時則丟擲錯誤 try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } //輸出當前頁面的title System.out.println("Page title is: " + driver.getTitle()); //關閉所有webdriver程序,退出  driver.quit(); } }
複製程式碼

 

 

-----7.6

右擊Case1.Java,Run As—>Java Application,執行成功結果如下:

 -----7.7

接著,我們換成用selenium 1.0來寫程式碼,Case1_1.java程式碼如下:

複製程式碼
package Selenium_Test;

import com.thoughtworks.selenium.*;

 

public class Case1_1 { public static void main(String[] args) { DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.baidu.com/"); selenium.start(); selenium.open("/"); selenium.type("id=kw1", "selenium"); selenium.click("id=su1"); System.out.println("Page title is: " + selenium.getTitle()); selenium.stop(); } }
複製程式碼

 

 -----7.8

右擊Case1_1.Java,Run As—>Java Application,執行成功結果如下:

 

-----7.9

上面提示不能連線伺服器,下面我們先執行前面的Run_selenium.bat,啟動selenium rc

 

-----7.10

再次右擊Case1_1.Java,Run As—>Java Application,執行成功結果如下:

 

以上例子,展示了使用selenium webdriver和selenium 1.0寫程式碼的執行區別。

兩者開啟瀏覽器用的方法不同,當然,還有其他的方法也不同。且1.0還得另外啟動selenium rc

下面我們通過Junit來執行指令碼,指令碼需要修改一下,因為Junit的Java檔案有它自己的格式。

第八步 Eclipse通過Junit執行Selenium的Java例項

-----8.1

右擊Selenium_Test,new->Junit test case 新建一個Case2.java。

完成後如下:

 

-----8.2

修改Case2.java程式碼如下:

複製程式碼
 1 package Selenium_Test;
 2 
 3 import org.junit.*;  4 import org.openqa.selenium.*;  5 import org.openqa.selenium.firefox.FirefoxDriver;  6  7 public class Case2 {  8  WebDriver driver;  9 10  @Before 11 public void setUp() throws Exception { 12 driver = new FirefoxDriver(); 13  } 14 15  @Test 16 public void test_case2() throws Exception { 17 driver.get("http://www.google.com.hk"); 18 WebElement element = driver.findElement(By.name("q")); 19 element.sendKeys("hello Selenium!"); 20  element.submit(); 21  } 22 23  @After 24 public void tearDown() throws Exception { 25 System.out.println("Page title is: " + driver.getTitle()); 26  driver.quit(); 27  } 28 }
複製程式碼

 

-----8.3 

右擊Case2.java,Run As—>Junit Test,執行成功結果如下:

 

第九步 Eclipse通過TestNG執行Selenium的Java例項

-----9.1

安裝 TestNG

 

  在 Eclipse 中,點選 Help ->  Install new software ,在 add 欄中輸入http://beust.com/eclipse,在下面就會看到 TestNG.選中點選安裝,按下一步直到安裝完,線上安裝會有點很慢。

安裝完重啟Eclipse後,在 window->Show View->other 裡面選中Java->TestNG,就會出現TestNG選項了。

 

-----9.2

右擊包Selenium_Test,new->other->TestNG新建一個 TestNG 的測試類Case3.java。

完成後如下:

修改Case3.java指令碼內容如下:

複製程式碼
 1 package Selenium_Test;
 2 
 3 import org.testng.annotations.Test;  4 import org.openqa.selenium.By;  5 import org.openqa.selenium.WebDriver;  6 import org.openqa.selenium.WebElement;  7 import org.testng.annotations.BeforeMethod;  8 import org.testng.annotations.AfterMethod;  9 import org.openqa.selenium.firefox.FirefoxDriver; 10 11 public class Case3 { 12  WebDriver driver; 13 14  @BeforeMethod 15 public void beforeMethod() { 16 17  } 18 19  @AfterMethod 20 public void afterMethod() { 21 System.out.println("Page title is: " + driver.getTitle()); 22  driver.quit(); 23  } 24 25  @Test 26 public void test_case3() { 27 driver = new FirefoxDriver(); 28 driver.get("http://www.google.com.hk"); 29 WebElement element = driver.findElement(By.name("q")); 30 element.sendKeys("hello Selenium!"); 31  element.submit(); 32  } 33 }
複製程式碼

 

-----9.3

右擊Case3.java,Run as->TestNG Test,執行成功結果如下:

 

 執行完,會生成一個test-output資料夾,資料夾下面的index.html就是測試報告,如下:

 以上是在Eclipse下如何搭建Selenium的測試環境,包括直接執行.java,通過Junit執行.java,通過TestNG執行.java。