1. 程式人生 > >Selenium頁面對象+數據驅動測試框架

Selenium頁面對象+數據驅動測試框架

text 結果 split 試用 record sha keys gate name

工程的目錄結構:

技術分享圖片

pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<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>shanghai</groupId>
<artifactId>frame</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
</dependency>

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.8.1</version>
</dependency>

<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>

<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>

textng.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="百度搜索的測試套件">

<test verbose="2" preserve-order="true" name="百度搜索">
<classes>
<class name="BaiduSearchCase" />
</classes>
</test>

<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter" />
<listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
</listeners>

</suite>

頁面對象:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class PageObject {
//頁面對象

@FindBy(xpath = ".//*[@id=‘kw‘]")
private WebElement inputBox;
//輸入框

@FindBy(xpath = ".//*[@id=‘su‘]")
private WebElement searchButton;
//搜索按鈕

@FindBy(xpath = ".//*[@id=‘1‘]/h3/a")
private WebElement searchResult;
//搜索結果第一行

private String baiduUrl = "https://www.baidu.com/";
//百度首頁

private WebDriver driver;

public PageObject(){
//構造函數,生成瀏覽器對象,初始化PageFactory對象
System.setProperty("webdriver.firefox.marionette",
"src/main/resourcec/geckodriver.exe");
driver = new FirefoxDriver();
PageFactory.initElements(driver, this);
driver.manage().window().maximize();
}

public void open(){
//打開百度
String baiduUrl = "https://www.baidu.com/";
driver.get(baiduUrl);
}

public void refresh(){
//刷新瀏覽器
driver.navigate().refresh();
}

public void quit(){
//退出瀏覽器
driver.close();
driver.quit();
}

public void search(String value){
//輸入並搜索
inputBox.clear();
inputBox.sendKeys(value);
searchButton.click();
}

public String text(){
//獲取搜索結果第一行的文本
return searchResult.getText();
}

}

讀取Csv文件的工具類:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ReadCSV {

public static Object [][] readCSV(String fileName)
throws IOException {
//讀取CSV文件的方法
List<Object[]> records = new ArrayList<Object[]>();
String record;
BufferedReader file = new BufferedReader(
new InputStreamReader(
new FileInputStream(fileName),
"UTF-8"));
file.readLine();
while ((record=file.readLine())!=null){
String fields[] = record.split(",");
records.add(fields);
}
file.close();

Object[][] results = new Object[records.size()][];
for (int i=0; i<records.size();i++){
results[i] = records.get(i);
}
return results;
}

}

Csv文件:

技術分享圖片


關鍵字 預期的搜索結果 測試用例的名稱
中國,中國_百度百科,百度搜索中國的測試用例
美國,美國_百度百科,百度搜索美國的測試用例
英國,英國_百度百科,百度搜索英國的測試用例
法國,法國_百度百科,百度搜索法國的測試用例

測試用例:
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.IOException;

public class BaiduSearchCase {

private PageObject pageObject = new PageObject();

@BeforeClass
public void beforeClass() throws InterruptedException {
pageObject.open();
Thread.sleep(2000);
}

@Test(dataProvider = "keyword")
//百度搜索的測試用例
public void baiduSearchCase(String word, String result, String case_1)
throws InterruptedException {
pageObject.search(word);
Thread.sleep(2000);
Assert.assertEquals(pageObject.text(), result);
Reporter.log(case_1);
pageObject.refresh();
Thread.sleep(2000);
}

@AfterClass
public void afterClass(){
pageObject.quit();
}

@DataProvider(name = "keyword")
public Object[][] dp() throws IOException {
return ReadCSV.readCSV("src/main/resources/keyword.csv");
}

}

測試報告:

技術分享圖片

Selenium頁面對象+數據驅動測試框架