zoukankan      html  css  js  c++  java
  • selenium webdriver testng自动化测试数据驱动

    selenium webdriver testng自动化测试数据驱动

    selenium webdriver testng自动化测试数据驱动

    一、数据驱动测试概念

            数据驱动测试是相同的测试脚本使用不同的测试数据执行,测试数据和测试行为完全分离。

    二、实施数据驱动测试的步骤:

    1、编写测试脚本,脚本需要支持程序对象、文件或者数据库读入测试数据。

    2、将测试脚本使用的测试数据存入程序对象、文件或者数据库等外部介质中。

    3、运行脚本,循环调用存储在外部介质的测试数据。

    4、验证所有的测试结果是否符合期望的结果。

    三、使用TestNG进行数据驱动

    使用@DataProvider注解定义当前方法中的返回对象作为测试脚本的测试数据集。

    用法参考代码:

    import java.util.concurrent.TimeUnit;  
    import org.openqa.selenium.By;  
    import org.openqa.selenium.WebDriver;  
    import org.openqa.selenium.WebElement;  
    import org.openqa.selenium.firefox.FirefoxDriver;  
    import org.testng.Assert;  
    import org.testng.annotations.AfterMethod;  
    import org.testng.annotations.BeforeMethod;  
    import org.testng.annotations.DataProvider;  
    import org.testng.annotations.Test;  
    //搜索2个关键词,验证搜索结果页面是否包含期望的关键词  
    public class DataProviderDemo {  
        private static WebDriver driver;  
        @DataProvider(name="searchData")  
        public static Object[][] data()  
        {  
            return new Object[][] {{"老九门","演员","赵丽颖"},{"X站警天启","导演","布莱恩·辛格"},{"诛仙青云志","编剧","张戬"}};  
        }  
      @Test(dataProvider="searchData")  
      public void testSearch(String searchdata1,String searchdata2,String searchResult) {  
          //打开sogou首页  
          driver.get("http://www.sogou.com/");  
          //输入搜索条件  
          driver.findElement(By.id("query")).sendKeys(searchdata1+" "+searchdata2);  
          //单击搜索按钮  
          driver.findElement(By.id("stb")).click();  
          //单击搜索按钮后,等待3秒显示搜索结果  
          try{  
              Thread.sleep(3000);  
          }catch(InterruptedException e){  
              e.printStackTrace();  
          }  
          //判断搜索的结果是否包含期望的关键字  
          Assert.assertTrue(driver.getPageSource().contains(searchResult));  
      }  
      @BeforeMethod  
      public void beforeMethod() {  
          //若无法打开Firefox浏览器,可设定Firefox浏览器的安装路径  
          System.setProperty("webdriver.firefox.bin", "D:\Program Files (x86)\Mozilla Firefox\firefox.exe");  
          //打开Firefox浏览器  
          driver=new FirefoxDriver();  
          //设定等待时间为5秒  
          driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);  
      }  
      
      @AfterMethod  
      public void afterMethod() {  
          //关闭打开的浏览器  
          driver.quit();  
      }  
    }  

    运行结果:

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

    上述代码表示测试方法中3个参数分别使用searchData测试数据集中每个一维数组中的数据进行赋值。

    此方法会被调用3次,分别使用测试数据集中的三组数据。

  • 相关阅读:
    [hdu6271]Master of Connected Component
    [hdu5468]Puzzled Elena
    [hdu4582]DFS spanning tree
    [poj2054]Color a Tree
    [luogu4107]兔子和樱花
    整除的尾数[HDU2099]
    胜利大逃亡[HDU1253]
    Bitset[HDU2051]
    折线分割平面[HDU2050]
    不容易系列之(4)——考新郎[HDU2049]
  • 原文地址:https://www.cnblogs.com/111testing/p/6209127.html
Copyright © 2011-2022 走看看