码迷,mamicode.com
首页 > Web开发 > 详细

selenium WebDriver:使用TestNG和CSV文件进行数据驱动

时间:2015-12-07 20:36:14      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

1.如何解决CSV文件乱码问题

讲csv文件用UltraEdit-32软件打开,在底部状态栏有标识中将编码类型变为utf-8

2.具体结合csv数据驱动代码

csv文件

技术分享

package cn.gloryroad;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.testng.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

@Test
public class TestDataDrivenByCSVFile {

public WebDriver driver;
String baseUrl = "http://www.baidu.com/";

@DataProvider(name = "testData")
public static Object[][] words() throws IOException {

return getTestData("D:\\testData.csv");
}
@Test(dataProvider="testData")
public void testSearch(String searchWord1, String searchWord2,
String SearchResult) {


driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
String baseUrl = "http://www.baidu.com/";
Navigation navigation = driver.navigate();
navigation.to("http://www.baidu.com");
driver.findElement(By.id("kw"))
.sendKeys(searchWord1 + "" + searchWord2);
driver.findElement(By.id("su")).click();

(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.findElement(By.id("foot")).getText()
.contains("?????????");
}
});


Assert.assertTrue(driver.getPageSource().contains(SearchResult));

}

@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}

@AfterMethod
public void afterMethod() {
driver.quit();
}



public static Object [][] getTestData (String fileName) throws IOException{
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);
System.out.println(results[i]);
}
return results;

}

}

selenium WebDriver:使用TestNG和CSV文件进行数据驱动

标签:

原文地址:http://www.cnblogs.com/Treasa/p/5027206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!