TestNG+selenium高级应用—数据驱动

什么是数据驱动

相同的测试脚本使用不同的测试数据来执行,测试数据和测试行为完全进行了分离。这样的测试脚本设计模式称为数据驱动。

数据驱动的方式

  • 使用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("测试完成啦");
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值