Package cn.gloryroad;
Import Org.testng.annotations.Test;
Import Org.testng.annotations.BeforeMethod;
Import Org.testng.annotations.AfterMethod;
Import Org.testng.annotations.DataProvider;
Import Java.io.File;
Import Java.io.BufferedReader;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.concurrent.TimeUnit;
Import Org.testng.Assert;
Import Org.openqa.selenium.By;
Import Org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebDriver.Navigation;
Import Org.openqa.selenium.ie.InternetExplorerDriver;
Import org.openqa.selenium.support.ui.ExpectedCondition;
Import org.openqa.selenium.support.ui.WebDriverWait;
Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;
Import Org.apache.poi.ss.usermodel.Row;
Import Org.apache.poi.ss.usermodel.Sheet;
Import Org.apache.poi.ss.usermodel.Workbook;
import Org.apache.poi.xssf.usermodel.XSSFWorkbook;;
public class Testdatadrivenbyexcelfile {
public Webdriver driver;
String baseUrl = "http://www.baidu.com/";
@DataProvider (name = "TestData")
public static object[][] Words () throws IOException {
Return Gettestdata ("d:\\", "Testdata.xls", "Sheet1");
}
@Test (dataprovider= "TestData")
public void Testsearch (string searchWord1, String searchWord2,
String SearchResult) {
Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds);
Navigation Navigation = Driver.navigate ();
Navigation.to ("http://www.baidu.com");
Driver.findelement (By.id ("kw"))
. SendKeys (SearchWord1 + "" + SearchWord2);
Driver.findelement (By.id ("su")). Click ();
(New webdriverwait (Driver)). Until (new expectedcondition<boolean> () {
Public Boolean apply (Webdriver D) {
Return D.findelement (By.id ("foot")). GetText ()
. Contains ("Search Help");
}
});
Assert.asserttrue (Driver.getpagesource (). Contains (SearchResult));
}
@BeforeMethod
public void Beforemethod () {
System.setproperty ("Webdriver.ie.driver", "D:\\iedriverserver.exe");
Driver = new Internetexplorerdriver ();
}
@AfterMethod
public void Aftermethod () {
Driver.quit ();
}
public static Object [[] Gettestdata (String filepath,string filename,string sheetname) throws ioexception{
File File = new file (FilePath + "\" + fileName);
FileInputStream InputStream = new FileInputStream (file);
Workbook Workbook = null;
String fileextensionname = filename.substring (Filename.indexof ("."));
if (Fileextensionname.equals (". xlsx")) {
Workbook = new Xssfworkbook (InputStream);
} else if (fileextensionname.equals (". xls")) {
Workbook = new Hssfworkbook (InputStream);
}
Sheet Sheet = Workbook.getsheet (sheetname);
int rowCount = Sheet.getlastrownum ()-Sheet.getfirstrownum ();
list<object[]> records = new ArrayList <Object[]> ();
for (int i=1;i<rowcount+1;i++) {
Row row = Sheet.getrow (i);
String fields[] = new String[row.getlastcellnum ()];
for (int j = 0;j<row.getlastcellnum (); j + +) {
Fields[j]=row.getcell (j). Getstringcellvalue ();
}
Records.add (fields);
}
Object[][] results = new object[records.size () [];
for (int i=0;i<records.size (); i++) {
Results[i]=records.get (i);
System.out.println (Results[i]);
}
return results;
}
}
Selenium Webdriver: Data driven using testng, poi, and Excel files