selenium+testNG自動化測試框架搭建


自動化測試框架搭建

1 Java環境的搭建

1.1訪問oracle的官網下載最新版本的jdk

http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html

進去后選擇電腦配置對應版本的JDK版本。

下載成功以后直接下一步,安裝默認的路徑。這里注意:安裝的過程中會提示一個JRE的安裝路徑,需要注意一下,一個是運行環境JRE,一個是編譯的環境JDK中默認會有JRE

1.2配置環境變量 

打開電腦中的系統屬性中的高級系統配置中的環境變量。系統變量中新建一個變量名稱為Java_Home,存放的路徑為jdk的安裝目錄的路徑:C:\Program Files\Java\jdk-version

新建變量Path%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;

新建變量Classpath.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;

驗證是否安裝成功,windows cmd:輸入java -versions,回車,出現jdk版本信息,則證明配置成功

2安裝Java編輯工具eclipse

2.1訪問eclipse官網下載Java編輯工具eclipse

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

下載后解壓到指定目錄,點擊啟動程序文件即可打開eclipse如下圖

 

 

 

 

 

 

 

 

 

 

2.2 eclipse中安裝testNG插件

在線安裝:

打開eclipse-->Help-->Install New Software-->Add-->NameLocation中輸入MyTestNGhttp://beust.com/eclipse-->出現TestNG勾選-->Next-->安裝成功后重啟eclipse

 

 

 

 

離線安裝:

 1.下載附件(eclipse-testng離線包.zip),並解壓;
      2.將解壓后的文件..\eclipse-testng離線包\features\目錄下的文件夾org.testng.eclipse_6.8.6.20130607_0745放到eclipse--features目錄下;
      3.將解壓后的文件..\eclipse-testng離線包\org.testng.eclipse_6.8.6.20130607_0745文件夾放到eclipse--plugins目錄下;
      4.重啟eclipse.

驗證成功安裝testNG的方法:file-->new-->other-->TestNg

 

 

 

3 Eclipse執行SeleniumJava實例

3.1新建java工程

File-->new-->other-->Java Project-->輸入工程名,選擇Java運行環境的版本,點擊Finish

 

 

 

3.2導入Selenium相關的包

tests上右鍵,Properties-->Java Build Path-->Libraries-->Add External Jars-->選擇下載好的Selenium相關包全部導入-->點擊OK

 

 

 

 

以上方法導入若新建一個工程就要重新導入所有的jar包,現我們用另一種方法解決這種重復導入jar包的麻煩。同樣tests上右鍵,Properties-->Java Build Path-->Libraries-->Add Library -->User Library-->Next-->User Librarys-->New-->輸入Library名(selenium Library 方便記)-->OK-->Add External Jars-->選擇下載好的Selenium相關包全部導入-->點擊OK-->選擇剛創建的User Library-->Finish-->OK

 

 

 

 

 

 

 

 

 

 

 

 

 

3.3導入testNG

同樣tests上右鍵,Properties-->Java Build Path-->Libraries-->Add Library -->TestNG-->Next-->OK

 

 

 

 

3.4 瀏覽器驅動

下載ChromeDriver.exe,並拷貝到Chrome安裝目錄中

 

 

 

 

同樣方法下載IEDriverServer.exe,並拷貝到IE瀏覽器的安裝目錄中,

由於selenium支持火狐瀏覽器,所以我們可以不用下載其驅動,但為了以防萬一,我們還是要安裝一下滴!(下載geckodriver.exe,拷貝到火狐的安裝目錄中)。

有時候我們要把代碼壓縮,發送給其他人用的時候,那些驅動就不能使用了,因為沒有一起打包壓縮,那么我們就可以將這些驅動全部加到項目的文件夾中。

首先,我們在tests項目下的src目錄下,新建一個driver目錄,將三個瀏覽器驅動拷貝到driver目錄下,這樣我們就可以將驅動一起打包壓縮交給別人了。

 

 

 

 

3.5 編寫代碼

首先我們在SRC目錄下新建幾個Package

 

 

現在我們來說說這些package作用吧。

Pageobject存放一些頁面對象,一個頁面一個類,類中存放和這個頁面相關的所有方法(該頁面的所有相關操作)。

Test存放要測試的類,一個類測試一個頁面,類中存放多個測試案例,調用方法,使用數據進行測試的類。

Util 存放公共類代碼,如UseBrowser.java,存放chromeFirefoxIE等瀏覽器的相關代碼。BaseTest.java,存放執行TestSuit前要執行的方法和執行TestSuit后要執行的方法的代碼。

首先我們在Util包中編寫要使用的瀏覽器的啟動代碼,一般有三種瀏覽器是比較常用的。分別是谷歌瀏覽器、火狐瀏覽器以及IE瀏覽器。下面是啟動瀏覽器的相關代碼:

UseBrowser

package com.zzx.util;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.ie.InternetExplorerDriver;

 

public class UseBrowser {

public static WebDriver driver;

//啟動谷歌瀏覽器

public WebDriver startChrome(String url) throws Exception {

try {

System.setProperty("webdriver.chrome.driver", "D:\\workspace\\tests\\src\\driver\\chromedriver.exe");

driver = new ChromeDriver();

driver.get(url);

System.out.println("成功打開谷歌瀏覽器!");

// driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

} catch (Exception e) {

System.out.println("打開谷歌瀏覽器時出錯了"+e);

}

return driver;

}

 

//啟動火狐瀏覽器

public WebDriver startFirefox(String url) throws Exception {

try {

// 默認支持火狐瀏覽器,能夠正常打開,若不能打開火狐,則把下面的火狐的驅動放開

// System.setProperty("webdriver.firefox.marionette","D:\\workspace\\tests\\src\\driver\\geckodriver.exe");

driver = new FirefoxDriver();

driver.get(url);

System.out.println("成功打開火狐瀏覽器!");

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Thread.sleep(2000);

} catch (Exception e) {

System.out.println("打開火狐瀏覽器時出錯了"+e);

}

return driver;

}

 

//啟動IE瀏覽器

public WebDriver startIE(String url) throws Exception {

try {

System.setProperty("webdriver.ie.driver", "D:\\workspace\\tests\\src\\driver\\IEDriverServer.exe");

driver = new InternetExplorerDriver();

driver.get(url);

System.out.println("成功打開IE瀏覽器!");

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

} catch (Exception e) {

System.out.println("打開IE瀏覽器時出錯了"+e);

}

return driver;

}

 

//關閉瀏覽器

public void tearDownBrowser() throws Exception {

try {

Thread.sleep(2000);

driver.close();

System.out.println("成功關閉瀏覽器!");

} catch (Exception e) {

System.out.println("關閉瀏覽器時出錯了"+e);

}

 

}

}

在編寫完啟動瀏覽器的相關代碼后,我們再編寫基礎類,當有測試類要執行時就要繼承這個基礎類,繼承它的兩個方法。這個類的主要功能就是在執行TestSuite之前要先打開相關的瀏覽器,然后進行相關測試,執行完TestSuite的測試用例之后,我們要關閉瀏覽器。具體代碼如下:

BaseTest

package com.zzx.util;

import org.openqa.selenium.WebDriver;

import org.testng.annotations.AfterSuite;

import org.testng.annotations.BeforeSuite;

 

public class BaseTest {

public String LoginURL = "http://*************";

private static String URL = "http://****************/";

UseBrowser useBrowser = new UseBrowser();

public static WebDriver driver;

@BeforeSuite

public void start() throws Exception {

try {

 

driver = useBrowser.startChrome(URL);

// driver = useBrowser.startIE(URL);

// driver = useBrowser.startFirefox(URL);

} catch (Exception e) {

System.out.println(e);

}

}

 

@AfterSuite

public void end() throws Exception {

useBrowser.tearDownBrowser();

}

 

}

 

前期的准備工作算是已經完成了,現在我們要開始做最主要的事情,在pageobject包中新建一個LoginPage類,對頁面元素的封裝,這里我並沒有二次封裝對元素的操作。直接調用原生API方法進行元素的操作。要對元素的操作進行二次封裝其實很簡單,只要自己定義方法,重新封裝,然后調用自己定義的方法就可以對元素進行相關的操作了。該頁面對象中存放着該頁面上要進行測試功能點的方法以及和測試功能點相關的其他輔助方法。具體代碼如下:

登錄頁面相關操作及方法

package com.zzx.pageObject;

 

import org.openqa.selenium.By;

import org.openqa.selenium.NoAlertPresentException;

import org.openqa.selenium.WebDriver;

import org.testng.Reporter;

 

public class LoginPage {

private String name = "name";

private String pwd = "pwd";

private String forLogin = "inputbutton";

 

public void login(WebDriver driver, String username, String password) throws Exception {

driver.findElement(By.name(name)).sendKeys(username);

driver.findElement(By.name(pwd)).sendKeys(password);

// 點擊登錄

driver.findElement(By.id(forLogin)).click();

Thread.sleep(2000);

// System.out.println(driver.getCurrentUrl());

 

}

 

public boolean isLoginSuccess(WebDriver driver) throws Exception{

boolean flag = false;

try {

if(driver.findElement(By.id("asset")).isDisplayed()){

flag=true;

}

} catch (Exception e) {

// e.printStackTrace();

// System.out.println(e);

}

return flag;

}

public boolean loginStatus(WebDriver driver) throws Exception {

if (isAlertPresent(driver)) {

Reporter.log(driver.switchTo().alert().getText());

System.out.println(driver.switchTo().alert().getText());

driver.switchTo().alert().accept();

driver.navigate().refresh();

return false;

}

else if (!(isLoginSuccess(driver))) {

Reporter.log("用戶名錯誤!");

System.out.println("用戶名錯誤!");

driver.navigate().refresh();

Thread.sleep(2000);

return false;

}

else {

Reporter.log("登錄成功!");

System.out.println("登錄成功!");

return true;

}

}

 

public boolean isAlertPresent(WebDriver driver) throws Exception {

try {

driver.switchTo().alert();

return true;

} catch (NoAlertPresentException e) {

// e.printStackTrace();

return false;

}

}

 

public  boolean isLoginPage(WebDriver driver) throws Exception {

boolean flag = false;

try {

if (driver.findElement(By.id(forLogin)).getAttribute("value").equals("登錄")) {

flag = true;

return flag;

}

} catch (Exception e) {

//     System.out.println(e);

return flag;

}

return flag;

}

}

 

寫好了測試功能點的代碼后,我們就要在test包中編寫相關的測試用例的執行方法了,這里我只用了登錄的相關測試用例作為例子。

 

登錄頁面相關測試用例

package com.zzx.test;

 

import static org.testng.Assert.assertEquals;

import org.testng.annotations.Test;

import com.zzx.pageObject.LoginPage;

import com.zzx.util.BaseTest;

 

public class LoginTest extends BaseTest{

 

LoginPage loginPage = new LoginPage();

String LoginURL = "http://oneadmin.peersafe.cn/logout";

 

/**

 * 方法名稱:loginTest1   

 * 方法描述: This method is testing the empty username and the right password

 *         The end is Loginfailed ,I will print some error information  on the console and

 *         the page still stay on the login page

 * 創建人:zzx

 * 創建時間:2017912日 下午5:33:27   

 * 修改人:zzx 

 * 修改時間:2017912日 下午5:33:27   

 * 修改備注:   

 * @version  1.0

 * @throws Exception maybe some exception will happen

 */

//空的用戶名和正確的密碼,登錄失敗,控制台輸出用戶名錯誤!

@Test(priority=1)

public void loginTest1() throws Exception{

if(!loginPage.isLoginPage(driver)){

driver.get(LoginURL);

}

try {

loginPage.login(driver, "","$z58dSHE");

} catch (Exception e) {

System.out.println(e);

}

finally {

assertEquals(false, loginPage.loginStatus(driver));

}

}

 

/**

 * 方法名稱:loginTest2   

 * 方法描述: This method is testing the empty username and the error password

 *         The end is Loginfailed ,I will print some error information  on the console and

 *         the page still stay on the login page

 * 創建人:zzx

 * 創建時間:2017912日 下午5:33:27   

 * 修改人:zzx

 * 修改時間:2017912日 下午5:33:27   

 * 修改備注:   

 * @version  1.0

 * @throws Exception maybe some exception will happen

 */

//空的用戶名和錯誤的密碼,登錄失敗,控制台輸出用戶名錯誤!

@Test(priority=2)

public void loginTest2() throws Exception{

if(!loginPage.isLoginPage(driver)){

driver.get(LoginURL);

}

try {

loginPage.login(driver, "","$z58dSH");

} catch (Exception e) {

System.out.println(e);

}

finally {

assertEquals(false, loginPage.loginStatus(driver));

}

}

 

/**

 * 方法名稱:loginTest3  

 * 方法描述: This method is testing the right username and the empty password

 *         The end is Loginfailed ,I will print some error information  on the console and

 *         the page still stay on the login page

 * 創建人:zzx

 * 創建時間:2017912日 下午5:33:27   

 * 修改人:zzx 

 * 修改時間:2017912日 下午5:33:27   

 * 修改備注:   

 * @version  1.0

 * @throws Exception maybe some exception will happen

 */

//正確用戶名和空的密碼,登錄失敗,控制台輸出密碼不正確

@Test(priority=3)

public void loginTest3() throws Exception{

if(!loginPage.isLoginPage(driver)){

driver.get(LoginURL);

}

try {

loginPage.login(driver, "admin","");

} catch (Exception e) {

System.out.println(e);

}

finally {

assertEquals(false, loginPage.loginStatus(driver));

}

}

 

/**

 * 方法名稱:loginTest4   

 * 方法描述: This method is testing the error username and the empty password

 *         The end is Loginfailed ,I will print some error information  on the console and

 *         the page still stay on the login page

 * 創建人:zzx 

 * 創建時間:2017912日 下午5:33:27   

 * 修改人:zzx

 * 修改時間:2017912日 下午5:33:27   

 * 修改備注:   

 * @version  1.0

 * @throws Exception maybe some exception will happen

 */

//錯誤用戶名和空的密碼,登錄失敗,控制台輸出用戶名錯誤!

@Test(priority=4)

public void loginTest4() throws Exception{

if(!loginPage.isLoginPage(driver)){

driver.get(LoginURL);

}

try {

loginPage.login(driver, "admim","");

} catch (Exception e) {

System.out.println(e);

}

finally {

assertEquals(false, loginPage.loginStatus(driver));

}

}

 

/**

 * 方法名稱:loginTest5  

 * 方法描述: This method is testing the empty username and the empty password

 *         The end is Loginfailed ,I will print some error information  on the console and

 *         the page still stay on the login page

 * 創建人:zzx 

 * 創建時間:2017912日 下午5:33:27   

 * 修改人:zzx 

 * 修改時間:2017912日 下午5:33:27   

 * 修改備注:   

 * @version  1.0

 * @throws Exception maybe some exception will happen

 */

//空的用戶名和空的密碼,登錄失敗,控制台輸出用戶名錯誤!

@Test(priority=5)

public void loginTest5() throws Exception{

if(!loginPage.isLoginPage(driver)){

driver.get(LoginURL);

}

try {

loginPage.login(driver, "","");

} catch (Exception e) {

System.out.println(e);

}

finally {

assertEquals(false, loginPage.loginStatus(driver));

}

}

 

/**

 * 方法名稱:loginTest6   

 * 方法描述: This method is testing the error username and the error password

 *         The end is Loginfailed ,I will print some error information  on the console and

 *         the page still stay on the login page

 * 創建人:zzx 

 * 創建時間:2017912日 下午5:33:27   

 * 修改人:zzx 

 * 修改時間:2017912日 下午5:33:27   

 * 修改備注:   

 * @version  1.0

 * @throws Exception maybe some exception will happen

 */

//錯誤用戶名和錯誤的密碼,登錄失敗,控制台輸出用戶名錯誤!

@Test(priority=6)

public void loginTest6() throws Exception{

if(!loginPage.isLoginPage(driver)){

driver.get(LoginURL);

}

try {

loginPage.login(driver, "admim","$z58dSH");

} catch (Exception e) {

System.out.println(e);

}

finally {

assertEquals(false, loginPage.loginStatus(driver));

}

}

 

/**

 * 方法名稱:loginTest7   

 * 方法描述: This method is testing the right username and the error password

 *         The end is Loginfailed ,I will print some error information  on the console and

 *         the page still stay on the login page

 * 創建人:zzx 

 * 創建時間:2017912日 下午5:33:27   

 * 修改人:zzx

 * 修改時間:2017912日 下午5:33:27   

 * 修改備注:   

 * @version  1.0

 * @throws Exception maybe some exception will happen

 */

//正確用戶名和錯誤的密碼,登錄失敗,控制台輸出密碼不正確

@Test(priority=7)

public void loginTest7() throws Exception{

if(!loginPage.isLoginPage(driver)){

driver.get(LoginURL);

}

try {

loginPage.login(driver, "admin","$z58dSH");

} catch (Exception e) {

System.out.println(e);

}

finally {

assertEquals(false, loginPage.loginStatus(driver));

}

}

 

/**

 * 方法名稱:loginTest8  

 * 方法描述: This method is testing the error username and the right password

 *         The end is Loginfailed ,I will print some error information  on the console and

 *         the page still stay on the login page

 * 創建人:zzx 

 * 創建時間:2017912日 下午5:33:27   

 * 修改人:zzx 

 * 修改時間:2017912日 下午5:33:27   

 * 修改備注:   

 * @version  1.0

 * @throws Exception maybe some exception will happen

 */

//錯誤用戶名和正確密碼,登錄失敗,控制台輸出用戶名錯誤!

@Test(priority=8)

public void loginTest8() throws Exception{

if(!loginPage.isLoginPage(driver)){

driver.get(LoginURL);

}

try {

loginPage.login(driver, "admim","$z58dSHE");

} catch (Exception e) {

System.out.println(e);

}

finally {

assertEquals(false, loginPage.loginStatus(driver));

}

}

 

/**

 * 方法名稱:loginTest9   

 * 方法描述: This method is testing the right username and the right password

 *         The end is successed ,I will print some successed information  on the console and

 *         the page will into the home page

 * 創建人:zzx 

 * 創建時間:2017912日 下午5:33:27   

 * 修改人:zzx 

 * 修改時間:2017912日 下午5:33:27   

 * 修改備注:   

 * @version  1.0

 * @throws Exception maybe some exception will happen

 */

//正確用戶名和正確密碼,登錄成功,控制台輸出登錄成功!

@Test(priority=9)

public void loginTest9() throws Exception{

if(!loginPage.isLoginPage(driver)){

driver.get(LoginURL);

}

try {

loginPage.login(driver, "admin","$z58dSHE");

} catch (Exception e) {

System.out.println(e);

}

finally {

assertEquals(true, loginPage.loginStatus(driver));

}

}

}

 

 

3.6 代碼執行

在所有的代碼都已經編寫完成后,我們就要開始去執行測試用例了,有兩種執行方法。

第一種方法:在test包下的某個測試類頁面下或在包下的某個類,點擊右鍵出現Run As-->TestNG Test執行該測試類中的所有方法。

第二種方法:執行某個測試類則在test包下的某個測試類右鍵---TestNG -----> Convert to TestNG--->Finish,出現testng.xml,右鍵testng.xml--->Run As--->TestNG Suite執行測試方法,若要執行整個項目的所有測試方法,則項目右鍵--->TestNG--->Convert to TestNG--->Finish,出現testng.xml,右鍵testng.xml--->Run As--->TestNG Suite執行項目的所有測試方法

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM