数据驱动(2)

时间:2019-03-19
本文章向大家介绍数据驱动(2),主要包括数据驱动(2)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

2.1使用TestNG和CSV文件进行数据驱动

测试逻辑:

(1)打开百度首页

(2)从CSV文件中读取每行中前两个逗号分隔的中文词作为搜索框中输入的搜索关键词,两个关键词中间带有一个空格

(3)单击搜索按钮

(4)断言搜索结果页面是否包含CSV文件中每行第三个词汇,包含则认为测试用例执行成功,否则认定为测试执行失败

使用记事本编写CSV文件,在保存时要将文件存储为UTF-8编码格式,内容为:

搜索值1,搜索值2,搜索结果要验证的内容
功夫,主演,周星驰
蝙蝠侠,主演,迈克尔
生化危机,编剧,安德森
超人,导演,扎克·施奈德

测试代码;

package cn.datadriven;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
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 org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;

public class TestDataDrivenByCSVFile {
    WebDriver driver;
    String url = "http://www.baidu.com";
    //使用注解Dataprovider将数据集集合命名为testData
    @DataProvider(name = "testData")
    public static Object[][] words() throws IOException{
        //调用静态方法getTestData,获取csv测试文件数据
        return getTestData("E:\\材料\\selenium\\Data.csv");
    }
    //读取csv文件的静态方法,使用csv文件的绝对文件路径做完函数参数
 public static Object[][] getTestData(String fileName)throws IOException {
        List<Object[]> records = new ArrayList<Object[]>();
        String record;
        //设定UTF-8字符集,使用带缓冲区的字符输入流BufferedReader读取文件内容
        BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
        //忽略读取文件标题行(第一行)
        file.readLine();
        /*遍历读取文件中除第一行外的其他所有行内容
         *并存储在名为records的ArrayList
         *每一个recods中存储的对象为一个String数组 
         * */
        while((record=file.readLine())!=null){
            String fileds[] = record.split(",");
            records.add(fileds);
        }
        //关闭文件对象
        file.close();
        //定义函数返回值object[][]
        //将存储测试数据的list转换为一个Object的二维数组
        Object[][] results = new Object[records.size()][];
        for(int i=0; i<records.size();i++){
            results[i] = records.get(i);
        }
        return results;
    }
  @Test(dataProvider="testData")
  public void testSearch(String searchWord1,String searchWord2, String searchResult) {
      driver.findElement(By.id("kw")).sendKeys(searchWord1+""+searchWord2);
      driver.findElement(By.id("su")).click();
      //使用显示等待,确认页面已加载完成,页面底部帮助已显示在页面上
      (new WebDriverWait(driver,10)).until(new ExpectedCondition<Boolean>() {
          @Override
          public Boolean apply(WebDriver d){
              return d.findElement(By.xpath("//*[@id='help']/a[1]")).getText().contains("帮助");
          }
    });
      //断言判断是否搜索出的结果页面上是否包含csv文件预期的关键字,预期值为csv每行最后一个关键字
      Assert.assertTrue(driver.getPageSource().contains(searchResult));
  }
  @BeforeMethod
  public void beforeMethod() {
      System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
      driver = new ChromeDriver();
      driver.manage().window().maximize();
      driver.get(url);
  }

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

}

测试结果: