1. 程式人生 > >Java+Maven+selenium+testing+reportNG自動化測試框架

Java+Maven+selenium+testing+reportNG自動化測試框架

都是 tlist image pub tro snapshot htm sea cells

最近公司新出了一個產品,需要搭建自動化測試框架,這是一個學以至用的好機會,跟上級申請後,決定搭建一個java自動化測試框架。

Java自動化測試對我來講可以說不難不易,因為java是我大學在校四年學的主要開發語言,但是畢業這麽多年沒寫了難免生疏。

weiUI自動化測試需要掌握以下幾點:一是獲取元素,java獲取元素對象與python差不多,用的是findElement方法,不過我在搭建框架過程中為了實現PO模式,從萬能的百度中獲知還有個一更好的類FindBy,FindBy+PageFactory可以完美實現PO模式。二是測試框架,junit和testNG都是java方面的主流測試框架,這兩個框架我都沒有用過,不能比較二者優劣,不過看現在各公司的招聘要求基本都是寫著要會testNG,所以選擇testNG作為測試框架應該不會錯。三是測試報告的展示,嘗試過後,發現測試報告還是reportNG比testNG的原生測試報告好看,所以決定用reportNG代替testNG生成測試報告。最後就是項目的構建了,很久以前我也是用過maven的,覺得這個東西還是滿好用的,所以框架中也加上吧。下面就一個個說一下我的代碼結構。

以下是配置文件POM.xml,各依賴關系下面已經註析清楚。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>myTest</
groupId> <artifactId>mytest</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>mytest</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>
UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.14.2</version> <scope>test</scope> </dependency> <!-- 加入reportNG依賴,代替testNG測試報告 --> <dependency> <groupId>org.uncommons</groupId> <artifactId>reportng</artifactId> <version>1.1.4</version> <scope>test</scope> <exclusions> <exclusion> <groupId>org.testng</groupId> <artifactId>testng</artifactId> </exclusion> </exclusions> </dependency> <!-- 加入selenium做webUI測試,選用selenium2 --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.53.1</version> </dependency> <!-- 依賴Guice --> <dependency> <groupId>com.google.inject</groupId> <artifactId>guice</artifactId> <version>3.0</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <!-- 加入maven-surefire-plugin插件用來使用maven執行用例,其中suiteXmlFile配置的就是testNG用例執行文件的地址 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.21.0</version> <configuration> <suiteXmlFiles> <suiteXmlFile>src/test/java/myTest/mytest/testNG.xml</suiteXmlFile> </suiteXmlFiles> <!-- 加入編碼設置,否則生成的報告會中文亂碼 --> <argLine>-Dfile.encoding=UTF-8</argLine> </configuration> </plugin> <!-- 添加插件,添加ReportNg的監聽器,修改最後的TestNg的報告 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.5</version> <configuration> <properties> <property> <name>usedefaultlisteners</name> <value>false</value> </property> <property> <name>listener</name> <value>org.uncommons.reportng.HTMLReporter</value> </property> </properties> <workingDirectory>target/</workingDirectory> <!-- <forkMode>always</forkMode> --> </configuration> </plugin> </plugins> </build> </project>

接下來是代碼實現,根據以往經驗,UI自動化測試很容易出現找到不對象的情況,所以在操作之前必須判斷對象是否存在,java和python一樣有隱式等待和顯式等待,我選擇使用更加穩妥的顯式等待。Java顯式等待使用的是WebDriverWait+ExpectedConditions,使用方式如下:

new WebDriverWait(driver,10).until(

ExpectedConditions.presenceOfElementLocated(By.cssSelector("css locator")));

也就是說要在定位元素的時候加入這兩個類作為等待條件,直到目標元素出現為止。但是如果每一個元素都這麽寫的話就有很多冗余代碼了,所以我寫了個公共類BasePage.java,重寫了click事件和sendkeys事件,代碼如下:

/**
 * @author:Helen
 * @date:2018年4月7日
 * @Description: 處理頁面元素公共類,重寫頁面操作事件,為每個元素加入顯式等待
 */
package myTest.mytest;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class BasePage {
    WebDriver driver;
    private final int timeOut = 10;//等待時間

    public BasePage(WebDriver driver) {
        // TODO Auto-generated constructor stub
        this.driver = driver;
    }

    /* 重寫senkeys方法 */
    void sendkeys(WebElement element, String s) {
        new WebDriverWait(driver, timeOut).until(ExpectedConditions.visibilityOf(element));// 加入顯式等待
        element.clear();// 先清空輸入框
        element.sendKeys(s);// 輸入數據
    }

    /* 重寫click方法 */
    void click(WebElement element) {
        new WebDriverWait(driver, timeOut).until(ExpectedConditions.visibilityOf(element));// 加入顯式等待
        element.click();
    }
}

接下來就是實現頁面對象獲取了,下面以百度頁面為示例

/**
 * @author:Helen
 * @date:2018年4月7日
 * @Description: 百度頁面,對象定位和操作,繼承BasePage
 */
package myTest.mytest;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class MyPage extends BasePage{
    public MyPage(WebDriver driver) {
        super(driver);
        // TODO Auto-generated constructor stub
    }

    //關鍵詞輸入框
    @FindBy(id="kw")
    private WebElement kw_Element;
    
    //“搜索”按鈕
    @FindBy(id="su")
    private WebElement su_Element;
    
    //輸入關鍵詞
    public void kw_sendkes(String s){
        this.sendkeys(kw_Element, s);
    }
    
    //點擊“搜索”按鈕
    public void su_click() {
        this.click(su_Element);
    }
    
}

接下來是寫測試業務內容,加載頁面的時候加入PageFactory,代碼如下:

/**
 * @author:Helen
 * @date:2018年4月7日
 * @Description: 百度搜索測試
 */
package myTest.mytest;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.testng.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;


public class myTestNg {
    private WebDriver driver = new FirefoxDriver();

    @Test
    public void baidu_search() {
        MyPage myPage = PageFactory.initElements(driver, MyPage.class);
        driver.get("https://www.baidu.com");
        driver.manage().window().maximize();//窗口最大化
        myPage.kw_sendkes("helenMemery");
        myPage.su_click();
    }

    @Test
    public void f2() {
        Assert.assertEquals("b", "b");
    }
    
    @AfterMethod
    public void close(){
        //driver.close();
    }

}

最後是配置testNG.xml文件,內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="百度搜索">
    <test name="搜索業務">
        <classes>
            <class name="myTest.mytest.NewTest"></class>
            <class name="myTest.mytest.myTestNg"></class>
        </classes>
    </test>
</suite>

接下來就是執行測試了,選中pom.xml右鍵-Run As-maven test

技術分享圖片

最後生成HTML 測試報告,如下圖所示:

技術分享圖片

最後剩下來的就是配置jenkins了,這個以後再寫下來吧。

Java+Maven+selenium+testing+reportNG自動化測試框架