Selenium页面对象+数据驱动测试框架
工程的目录结构:
pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?><br /><project xmlns="http://maven.apache.org/POM/4.0.0"<br /> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br /> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><br /> <modelVersion>4.0.0</modelVersion><br /><br /> <groupId>shanghai</groupId><br /> <artifactId>frame</artifactId><br /> <version>1.0-SNAPSHOT</version><br /><br /> <dependencies><br /> <dependency><br /> <groupId>org.testng</groupId><br /> <artifactId>testng</artifactId><br /> <version>6.11</version><br /> </dependency><br /><br /> <dependency><br /> <groupId>org.seleniumhq.selenium</groupId><br /> <artifactId>selenium-java</artifactId><br /> <version>3.8.1</version><br /> </dependency><br /><br /> <dependency><br /> <groupId>org.uncommons</groupId><br /> <artifactId>reportng</artifactId><br /> <version>1.1.4</version><br /> <scope>test</scope><br /> <exclusions><br /> <exclusion><br /> <groupId>org.testng</groupId><br /> <artifactId>testng</artifactId><br /> </exclusion><br /> </exclusions><br /> </dependency><br /><br /> <dependency><br /> <groupId>com.google.inject</groupId><br /> <artifactId>guice</artifactId><br /> <version>4.1.0</version><br /> <scope>test</scope><br /> </dependency><br /> </dependencies><br /><br /> <build><br /> <plugins><br /> <plugin><br /> <groupId>org.apache.maven.plugins</groupId><br /> <artifactId>maven-surefire-plugin</artifactId><br /> <version>2.20.1</version><br /> <configuration><br /> <testFailureIgnore>true</testFailureIgnore><br /> <suiteXmlFiles><br /> <suiteXmlFile>testng.xml</suiteXmlFile><br /> </suiteXmlFiles><br /> </configuration><br /> </plugin><br /> <plugin><br /> <groupId>org.apache.maven.plugins</groupId><br /> <artifactId>maven-compiler-plugin</artifactId><br /> <version>3.7.0</version><br /> <configuration><br /> <source>1.8</source><br /> <target>1.8</target><br /> </configuration><br /> </plugin><br /> </plugins><br /> </build><br /><br /></project><br /><br />textng.xml文件:
<?xml version="1.0" encoding="UTF-8"?><br /><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"><br /><br /><suite name="百度搜索的测试套件"><br /><br /> <test verbose="2" preserve-order="true" name="百度搜索"><br /> <classes><br /> <class name="BaiduSearchCase" /><br /> </classes><br /> </test><br /><br /> <listeners><br /> <listener class-name="org.uncommons.reportng.HTMLReporter" /><br /> <listener class-name="org.uncommons.reportng.JUnitXMLReporter" /><br /> </listeners><br /><br /></suite><br /><br />页面对象:
import org.openqa.selenium.WebDriver;<br />import org.openqa.selenium.WebElement;<br />import org.openqa.selenium.firefox.FirefoxDriver;<br />import org.openqa.selenium.support.FindBy;<br />import org.openqa.selenium.support.PageFactory;<br /><br />public class PageObject {<br /> //页面对象<br /><br /> @FindBy(xpath = ".//*[@id='kw']")<br /> private WebElement inputBox;<br /> //输入框<br /><br /> @FindBy(xpath = ".//*[@id='su']")<br /> private WebElement searchButton;<br /> //搜索按钮<br /><br /> @FindBy(xpath = ".//*[@id='1']/h3/a")<br /> private WebElement searchResult;<br /> //搜索结果第一行<br /><br /> private String baiduUrl = "https://www.baidu.com/";<br /> //百度首页<br /><br /> private WebDriver driver;<br /><br /> public PageObject(){<br /> //构造函数,生成浏览器对象,初始化PageFactory对象<br /> System.setProperty("webdriver.firefox.marionette",<br /> "src/main/resourcec/geckodriver.exe");<br /> driver = new FirefoxDriver();<br /> PageFactory.initElements(driver, this);<br /> driver.manage().window().maximize();<br /> }<br /><br /> public void open(){<br /> //打开百度<br /> String baiduUrl = "https://www.baidu.com/";<br /> driver.get(baiduUrl);<br /> }<br /><br /> public void refresh(){<br /> //刷新浏览器<br /> driver.navigate().refresh();<br /> }<br /><br /> public void quit(){<br /> //退出浏览器<br /> driver.close();<br /> driver.quit();<br /> }<br /><br /> public void search(String value){<br /> //输入并搜索<br /> inputBox.clear();<br /> inputBox.sendKeys(value);<br /> searchButton.click();<br /> }<br /><br /> public String text(){<br /> //获取搜索结果第一行的文本<br /> return searchResult.getText();<br /> }<br /><br />}<br /><br />读取Csv文件的工具类:
import java.io.BufferedReader;<br />import java.io.FileInputStream;<br />import java.io.IOException;<br />import java.io.InputStreamReader;<br />import java.util.ArrayList;<br />import java.util.List;<br /><br />public class ReadCSV {<br /><br /> public static Object [][] readCSV(String fileName) <br /> throws IOException {<br /> //读取CSV文件的方法<br /> List<Object[]> records = new ArrayList<Object[]>();<br /> String record;<br /> BufferedReader file = new BufferedReader(<br /> new InputStreamReader(<br /> new FileInputStream(fileName),<br /> "UTF-8"));<br /> file.readLine();<br /> while ((record=file.readLine())!=null){<br /> String fields[] = record.split(",");<br /> records.add(fields);<br /> }<br /> file.close();<br /><br /> Object[][] results = new Object[records.size()][];<br /> for (int i=0; i<records.size();i++){<br /> results[i] = records.get(i);<br /> }<br /> return results;<br /> }<br /><br />}<br /><br />Csv文件:
关键字 预期的搜索结果 测试用例的名称<br />中国,中国_百度百科,百度搜索中国的测试用例<br />美国,美国_百度百科,百度搜索美国的测试用例<br />英国,英国_百度百科,百度搜索英国的测试用例<br />法国,法国_百度百科,百度搜索法国的测试用例<br /><br />测试用例:
import org.testng.Assert;<br />import org.testng.Reporter;<br />import org.testng.annotations.AfterClass;<br />import org.testng.annotations.BeforeClass;<br />import org.testng.annotations.DataProvider;<br />import org.testng.annotations.Test;<br /><br />import java.io.IOException;<br /><br />public class BaiduSearchCase {<br /><br /> private PageObject pageObject = new PageObject();<br /><br /> @BeforeClass<br /> public void beforeClass() throws InterruptedException {<br /> pageObject.open();<br /> Thread.sleep(2000);<br /> }<br /><br /> @Test(dataProvider = "keyword")<br /> //百度搜索的测试用例<br /> public void baiduSearchCase(String word, String result, String case_1)<br /> throws InterruptedException {<br /> pageObject.search(word);<br /> Thread.sleep(2000);<br /> Assert.assertEquals(pageObject.text(), result);<br /> Reporter.log(case_1);<br /> pageObject.refresh();<br /> Thread.sleep(2000);<br /> }<br /><br /> @AfterClass<br /> public void afterClass(){<br /> pageObject.quit();<br /> }<br /><br /> @DataProvider(name = "keyword")<br /> public Object[][] dp() throws IOException {<br /> return ReadCSV.readCSV("src/main/resources/keyword.csv");<br /> }<br /><br />}<br /><br />测试报告:
相关推荐
Ronnyxie 2020-07-06
xiangxiaojun 2020-09-23
Reiki 2020-08-16
letheashura 2020-08-14
tiankele0 2020-07-29
curiousL 2020-07-18
王练 2020-07-18
tiankele0 2020-07-18
amei0 2020-07-08
Reiki 2020-07-06
xiangxiaojun 2020-07-05
zhanghaibing00 2020-06-28
xiongyouqiang 2020-06-28
Ronnyxie 2020-06-27
amei0 2020-06-26
letheashura 2020-06-26
tiankele0 2020-06-26
王练 2020-06-25