Selenium code Exercise (APACHEPOI)

Source: Internet
Author: User

Can read data from any row in Excel


Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;

Import Org.apache.poi.ss.usermodel.Cell;
Import Org.apache.poi.xssf.usermodel.XSSFSheet;
Import Org.apache.poi.xssf.usermodel.XSSFWorkbook;
Import Org.testng.Assert;
Import Org.testng.annotations.DataProvider;
Import Org.testng.annotations.Test;

public class Apachepoidemo {

@Test (dataprovider= "Datadriven")
public void Testadd (double a,double b,double c) {
Assert.assertequals (A+b, C);
//
//}
//
@DataProvider (name= "Datadriven")
Public object[][] Generatedata () throws IOException
{
Return Gettestdata ("data/data3.xlsx");

}
public static object[][] Gettestdata (String filelocation) throws ioexception{
Read file
File File=new file (filelocation);
FileInputStream fis=new fileinputstream (file);

Reading workbooks
Xssfworkbook workbook=new Xssfworkbook (FIS);
Visit Sheet Page
Xssfsheet sheet=workbook.getsheetat (0);
Get the number of columns in a table
int Colcount=sheet.getrow (0). Getphysicalnumberofcells ();
Get the number of rows in a table
int rowcount=sheet.getphysicalnumberofrows ();
Creating a two-dimensional array
Object[][] Ret=new Object[rowcount-1][colcount];
Reading tabular data and assigning values to two-dimensional arrays
for (int i=0;i<rowcount-1;i++) {
for (int j=0;j<colcount;j++) {
Get cell data
Cell Cell=sheet.getrow (i+1). Getcell (j);

Switch (Cell.getcelltype ()) {
Case Cell.cell_type_numeric:
Ret[i][j]=cell.getnumericcellvalue (); break;
Case cell.cell_type_string:
Ret[i][j]=cell.getstringcellvalue (); break;
Default
System.out.println ("Wrong cell type");
}
}

}
Close file stream
Fis.close ();
return ret;


}

}

Selenium code Exercise (APACHEPOI)

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.