工程的目录结构:
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");
}
}
测试报告: