什么是数据驱动
相同的测试脚本使用不同的测试数据来执行,测试数据和测试行为完全进行了分离。这样的测试脚本设计模式称为数据驱动。
数据驱动的方式
- 使用testNG进行数据驱动
- 使用testNG+csv数据驱动
- 使用testNG+apachePOI+excel文件数据驱动
- 使用mysql数据驱动
TestNG数据驱动
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
import java.io.IOException;
import org.testng.annotations.DataProvider;
public class TestData {
WebDriver driver;
WebElement element;
@DataProvider(name = "testData")
public static Object[][] words() throws IOException{
return new Object[][]{{"红楼梦","作者","曹雪芹"},{"西游记","作者","吴承恩"},{"三国演义","作者","罗贯中"}};
}
@Test(dataProvider = "testData")
public void selectsh(String input1, String input2, String result1) throws InterruptedException {
System.setProperty("webdriver.firefox.bin", "D:\\wylsoft\\Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", "D:\\wylsoft\\selenium3.5\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[@id=\"kw\"]")).sendKeys(input1+" "+input2);
element=driver.findElement(By.xpath("//*[@id=\"su\"]"));
Assert.assertTrue(element.isDisplayed());//判断百度一下是否存在
element.click();
(new WebDriverWait(driver,10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d){
return d.findElement(By.xpath("//*[@id=\"rs\"]/div")).getText().contains("相关搜索");
}
});
Assert.assertTrue(driver.getPageSource().contains(result1));
}
@AfterTest
public void afterTest() {
// driver.close();//关闭浏览器
System.out.println("测试完成啦");
}
}
TestNG+CSV数据驱动
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import org.testng.annotations.DataProvider;
public class TestDataByCSV {
WebDriver driver;
WebElement element;
//TestNG定义一个DataProvider
@DataProvider(name = "testData")
public static Object[][] words() throws IOException{
return getTestData("C:\\Users\\ZIYUN001\\Desktop\\test.csv");
}
//读取csv文件的静态方法,使用csv文件的路径作为函数参数
public static Object[][] getTestData(String filepath ) throws IOException{
List<Object[]> records=new ArrayList<Object[]>();
String record;
BufferedReader file=new BufferedReader(new InputStreamReader(new FileInputStream(filepath),"utf-8"));
// file.readLine();//忽略读取文件的标题行
while((record=file.readLine())!=null){
String[] fileds = record.split(","); //获取每行的数据
records.add(fileds);
}
file.close();
//定义object[][]二维数组,用list.size()定义二维数组行的限度
Object[][] result = new Object[records.size()][];
for (int i = 0; i < records.size(); i++) {
result[i] = records.get(i); //将CSV每行中的数据存放到二维数组中
}
return result;
}
//利用TestNG的特性,可以直接在方法中传入参数,这些参数从DataProvider中数据化传进来
@Test(dataProvider = "testData")
public void selectsh(String input1, String input2, String result1) throws InterruptedException {
System.setProperty("webdriver.firefox.bin", "D:\\wylsoft\\Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", "D:\\wylsoft\\selenium3.5\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[@id=\"kw\"]")).sendKeys(input1+" "+input2);
element=driver.findElement(By.xpath("//*[@id=\"su\"]"));
Assert.assertTrue(element.isDisplayed());//判断百度一下是否存在
element.click();
(new WebDriverWait(driver,10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d){
return d.findElement(By.xpath("//*[@id=\"rs\"]/div")).getText().contains("相关搜索");
}
});
Assert.assertTrue(driver.getPageSource().contains(result1));
}
@AfterTest
public void afterTest() {
// driver.close();//关闭浏览器
System.out.println("测试完成啦");
}
}