Using MySQL for data-driven TESNG

Source: Internet
Author: User
Tags assert testng

First download the MySQL Jar package website: http://dev.mysql.com/downloads/connector/j/

Execute SQL statement to build the database to add test data, here do not do too much introduction, are basic knowledge; don't forget to change the code to UTF-8/GBK;

Here is my library name: Mysqldata

Show: TestData

Directly on the code

 PackageChina ;Importorg.testng.annotations.Test;ImportOrg.testng.annotations.BeforeMethod;Importjava.io.IOException;Importjava.sql.Connection;ImportJava.sql.*;ImportJava.sql.DriverManager;Importjava.sql.Statement;Importjava.util.ArrayList;Importjava.util.List;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;ImportOrg.openqa.selenium.chrome.ChromeDriver;Importorg.openqa.selenium.support.ui.ExpectedCondition;Importorg.openqa.selenium.support.ui.WebDriverWait;ImportOrg.testng.Assert;ImportOrg.testng.annotations.AfterMethod;ImportOrg.testng.annotations.DataProvider; Public classTestdatadriverbymysqldatabase { PublicWebdriver Driver; String BASEURL= "Http://www.sogou.com"; @Test (Dataprovider= "TestData")   Public voidF (String searchword1,string searchword2,string searchresult) {driver.get (BASEURL); Driver.findelement (By.id ("Query"). SendKeys (searchword1+ "" +searchWord2); Driver.findelement (By.id ("STB") . Click (); (NewWebdriverwait (driver,10)). Until (NewExpectedcondition<boolean>() {@Override PublicBoolean Apply (webdriver d) {returnD.findelement (By.id ("S_footer")). GetText (). Contains ("Search Help");      }    });  Assert.asserttrue (Driver.getpagesource (). Contains (SearchResult)); } @BeforeMethod Public voidBeforemethod () {System.setproperty ("Webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe"); Driver=NewChromedriver (); } @AfterMethod Public voidAftermethod () {driver.quit (); } @DataProvider (Name= "TestData")   Public StaticObject[][] Words ()throwsioexception{returnGettestdata ("TestData"); }   Public StaticObject[][] Gettestdata (String tablename)throwsioexception{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 =NewArraylist<object[]>(); Try{class.forname (driver); Connection Conn=drivermanager.getconnection (Url,user,password); if(!conn.isclosed ()) System.out.println ("Linked database succeeded"); Statement Statement=conn.createstatement (); String SQL= "SELECT * from TestData;"; ResultSet RS=statement.executequery (SQL); ResultSetMetaData Rsmetadata=Rs.getmetadata (); intcols=Rsmetadata.getcolumncount ();  while(Rs.next ()) {String fields[]=NewString[cols]; intCol=0;  for(intcolidx=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 Driver not found");          E.printstacktrace (); }Catch(SQLException e) {e.printstacktrace (); }Catch(Exception e) {e.printstacktrace (); } object[][] Results=Newobject[records.size () [];  for(intI=0;i<records.size (); i++) {Results[i]=Records.get (i); }      returnresults; }      }

Using MySQL for data-driven TESNG

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.