首页 > 代码库 > tesNG下使用Mysql进行数据驱动

tesNG下使用Mysql进行数据驱动

首先需要下载mysql jar包 官网:http://dev.mysql.com/downloads/connector/j/

执行sql语句建库建表添加测试数据,这里不做过多介绍,都是基础知识;别忘记编码改为UTF-8/gbk;

这里我的库名:mysqldata

表明:testdata    

直接上代码

package china;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import java.io.IOException;
import java.sql.Connection;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.Statement;
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;
import org.testng.annotations.DataProvider;

public class TestDataDriverByMysqlDatabase {
    public WebDriver driver;
    String baseUrl="http://www.sogou.com";
  @Test(dataProvider = "testData")
  public void f(String searchWord1,String searchWord2,String searchresult){
      driver.get(baseUrl);
      driver.findElement(By.id("query")).sendKeys(searchWord1+""+searchWord2);
      driver.findElement(By.id("stb")).click();
      (new WebDriverWait(driver,10)).until(new ExpectedCondition<Boolean>() {
          @Override
          public Boolean apply(WebDriver d){
             return d.findElement(By.id("s_footer")).getText().contains("搜索帮助"); 
          }
    });
      Assert.assertTrue(driver.getPageSource().contains(searchresult));
  }
  @BeforeMethod
  public void beforeMethod(){
      System.setProperty("webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe");
      driver=new ChromeDriver();
  }

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


  @DataProvider(name="testData")
  public static Object[][] words() throws IOException{
    return getTestData("testdata");
      
  }
  public static Object[][] getTestData(String tablename)throws IOException{
      String driver="com.mysql.jdbc.Driver";
      String url="jdbc:mysql://127.0.0.1:3306/mysqldata";
      String user="root";
      String password="123456";
      List<Object[]>records = new ArrayList<Object[]>();
      try{
          Class.forName(driver);
          Connection conn =DriverManager.getConnection(url,user,password);
          if(!conn.isClosed())
              System.out.println("链接数据库成功");
              Statement statement=conn.createStatement();
              String sql="select * from testdata;";
              ResultSet rs=statement.executeQuery(sql);
              ResultSetMetaData rsMetaData=rs.getMetaData();
              int cols=rsMetaData.getColumnCount();
              while(rs.next()){
                  String fields[]=new String[cols];
                  int col=0;
                  for(int colIdx=0;colIdx<cols;colIdx++){
                      fields[col]=rs.getString(colIdx+1);
                      col++;
                  }
                  records.add(fields);
                  System.out.println(rs.getString(1)+"  "+rs.getString(2)+"  "+rs.getString(3));
                  
              }
              rs.close();
              conn.close();
          }catch(ClassNotFoundException e){
              System.out.println("未找到Mysql的驱动");
              e.printStackTrace();
          }catch(SQLException e){
              e.printStackTrace();
          }catch(Exception e){
              e.printStackTrace();
              
              }
      Object[][] results=new Object[records.size()][];
      for(int i=0;i<records.size();i++){
          results[i]=records.get(i);
             
          
      }
      return results;
  }      
}

 

tesNG下使用Mysql进行数据驱动