数据驱动测试二:使用TestNG和CSV文件进行数据驱动

转载:https://blog.csdn.net/heart_1014/article/details/52013173

使用@DataProvider注解定义当前方法中的返回对象CSV文件(存放测试数据)作为测试脚本的测试数据集进行数据驱动。

用法参考代码:

代码在搜索完成后使用显式等待方式,确认页面已经加载完成,页面底部的关键字"搜索帮助"已经显示在页面上

 

  1.  
    //从CSV文件中读取每行中前2个逗号分割的中文词作为搜索框中输入的搜索关键词
  2.  
    //断言搜索结果页面是否包含CSV文件中每行的最后一个词汇的关键字
  3.  
    import java.io.BufferedReader;
  4.  
    import java.io.FileInputStream;
  5.  
    import java.io.IOException;
  6.  
    import java.io.InputStreamReader;
  7.  
    import java.util.ArrayList;
  8.  
    import java.util.List;
  9.  
    import java.util.concurrent.TimeUnit;
  10.  
     
  11.  
    import org.openqa.selenium.By;
  12.  
    import org.openqa.selenium.WebDriver;
  13.  
    import org.openqa.selenium.firefox.FirefoxDriver;
  14.  
    import org.openqa.selenium.support.ui.ExpectedCondition;
  15.  
    import org.openqa.selenium.support.ui.WebDriverWait;
  16.  
    import org.testng.Assert;
  17.  
    import org.testng.annotations.AfterMethod;
  18.  
    import org.testng.annotations.BeforeMethod;
  19.  
    import org.testng.annotations.DataProvider;
  20.  
    import org.testng.annotations.Test;
  21.  
     
  22.  
    public class TestDataByCSVFile {
  23.  
    private static WebDriver driver;
  24.  
    @DataProvider(name="searchData")
  25.  
    public static Object[][] data() throws IOException
  26.  
    {
  27.  
    return getSearchData("E:\\AutoData\\testData.csv");//获取CSV文件的测试数据
  28.  
    }
  29.  
    @Test(dataProvider="searchData")
  30.  
    public void testSearch(String searchdata1,String searchdata2,String searchResult) {
  31.  
    //打开sogou首页
  32.  
    driver.get("http://www.sogou.com/");
  33.  
    //输入搜索条件
  34.  
    //从CSV文件中读取每行中前2个逗号分割的中文词作为搜索框中输入的搜索关键词,在两个搜索词中间增加一个空格
  35.  
    driver.findElement(By.id("query")).sendKeys(searchdata1+" "+searchdata2);
  36.  
    //单击搜索按钮
  37.  
    driver.findElement(By.id("stb")).click();
  38.  
     
  39.  
    //使用显式等待方式,确认页面已经加载完成,页面底部的关键字"搜索帮助"已经显示在页面上
  40.  
    (new WebDriverWait(driver,3)).until(new ExpectedCondition<Boolean>(){
  41.  
     
  42.  
    @Override
  43.  
    public Boolean apply(WebDriver d) {
  44.  
    return d.findElement(By.id("sogou_webhelp")).getText().contains("搜索帮助");
  45.  
    }});
  46.  
     
  47.  
    //断言搜索结果页面是否包含CSV文件中每行的最后一个词汇的关键字
  48.  
    Assert.assertTrue(driver.getPageSource().contains(searchResult));
  49.  
    }
  50.  
    @BeforeMethod
  51.  
    public void beforeMethod() {
  52.  
    //若无法打开Firefox浏览器,可设定Firefox浏览器的安装路径
  53.  
    System.setProperty("webdriver.firefox.bin", "D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
  54.  
    //打开Firefox浏览器
  55.  
    driver=new FirefoxDriver();
  56.  
    //设定等待时间为5秒
  57.  
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  58.  
    }
  59.  
     
  60.  
    @AfterMethod
  61.  
    public void afterMethod() {
  62.  
    //关闭打开的浏览器
  63.  
    driver.quit();
  64.  
    }
  65.  
    //读取CSV文件的静态方法,使用CSV文件的绝对文件路径作为函数参数
  66.  
    public static Object[][] getSearchData(String FileNameroot) throws IOException{
  67.  
    List<Object[]> records=new ArrayList<Object[]>();
  68.  
    String record;
  69.  
    //设定UTF-8字符集,使用带缓冲区的字符输入流BufferedReader读取文件内容
  70.  
    BufferedReader file=new BufferedReader(new InputStreamReader(new FileInputStream(FileNameroot),"UTF-8"));
  71.  
    //忽略读取CSV文件的标题行(第一行)
  72.  
    file.readLine();
  73.  
    //遍历读取文件中除第一行外的其他所有内容并存储在名为records的ArrayList中,每一行records中存储的对象为一个String数组
  74.  
    while((record=file.readLine())!=null){
  75.  
    String fields[]=record.split(",");
  76.  
    records.add(fields);
  77.  
    }
  78.  
    //关闭文件对象
  79.  
    file.close();
  80.  
    //将存储测试数据的List转换为一个Object的二维数组
  81.  
    Object[][] results=new Object[records.size()][];
  82.  
    //设置二位数组每行的值,每行是一个Object对象
  83.  
    for(int i=0;i<records.size();i++){
  84.  
    results[i]=records.get(i);
  85.  
    }
  86.  
    return results;
  87.  
    }
  88.  
    }

运行结果:

 

  1.  
    PASSED: testSearch("老九门", "演员", "赵丽颖")
  2.  
    PASSED: testSearch("X站警天启", "导演", "布莱恩·辛格")
  3.  
    PASSED: testSearch("诛仙青云志", "编剧", "张戬")
  4.  
     
  5.  
    ===============================================
  6.  
    Default test
  7.  
    Tests run: 3, Failures: 0, Skips: 0
  8.  
    ===============================================

测试数据的CSV文件内容:

 

搜索关键词1,搜索关键词2,搜索结果
老九门,演员,赵丽颖
X站警天启,导演,布莱恩·辛格
诛仙青云志,编剧,张戬

注意:使用写字板程序编辑CSV文件内容,在保存文件时要将文件存储为UTF-8编码格式。

posted @ 2018-08-23 14:13  小学生II  阅读(329)  评论(0编辑  收藏  举报