一直在用的webUI自動化測試框架,之前寫得太粗糙了,如今細化一下。css
weiUI自動化測試須要掌握如下幾點:一是獲取元素,java獲取元素對象與python差很少,用的是findElement方法,不過我在搭建框架過程當中爲了實現PO模式,從萬能的百度中獲知還有個一更好的類FindBy,FindBy+PageFactory能夠完美實現PO模式。二是測試框架,junit和testNG都是java方面的主流測試框架,這兩個框架我都沒有用過,不能比較兩者優劣,不過看如今各公司的招聘要求基本都是寫着要會testNG,因此選擇testNG做爲測試框架應該不會錯。三是測試報告的展現,嘗試事後,發現測試報告仍是reportNG比testNG的原生測試報告好看,因此決定用reportNG代替testNG生成測試報告。最後就是項目的構建了,好久之前我也是用過maven的,以爲這個東西仍是滿好用的,因此框架中也加上吧。下面就一個個說一下個人代碼結構。java
如下是配置文件POM.xml,各依賴關係下面已經注析清楚。python
<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,使用方式以下:web
new WebDriverWait(driver,10).until( apache ExpectedConditions.presenceOfElementLocated(By.cssSelector("css locator")));框架 |
也就是說要在定位元素的時候加入這兩個類做爲等待條件,直到目標元素出現爲止。可是若是每個元素都這麼寫的話就有不少冗餘代碼了,因此我寫了個公共類BasePage.java,重寫了click事件和sendkeys事件,代碼以下:maven
/** * @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,代碼以下:ui
/** * @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文件,內容以下:this
<?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了,這個之後再寫下來吧。