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

自動化測試框架搭建html

1 Java環境的搭建

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

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

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

下載成功之後直接下一步,安裝默認的路徑。這裏注意:安裝的過程當中會提示一個JRE的安裝路徑,須要注意一下,一個是運行環境JRE,一個是編譯的環境JDK中默認會有JREchrome

1.2配置環境變量 

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

新建變量Path%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;瀏覽器

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

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

2安裝Java編輯工具eclipse

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

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

下載後解壓到指定目錄,點擊啓動程序文件便可打開eclipse以下圖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執行項目的全部測試方法

 

 

 

 

 

相關文章
相關標籤/搜索