Selenium Webdriver: Data driven using testng, poi, and Excel files

Source: Internet
Author: User
Tags rowcount testng

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

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.