tesNG下使用Mysql進行資料驅動

來源:互聯網
上載者:User

標籤:lda   代碼   tco   next   package   cut   meta   use   exp   

首先需要下載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進行資料驅動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.