First, to configure the environment selenium+testng and POI package, selenium+testng environment is not elaborate, here is the sharing of POI package Https://pan.baidu.com/s/1BJEIWR57_4vwrCDy6WuBWA
After downloading, add the Lib file to the project and put the desired POI package into it, and import its Java build path into the project.
Second, create a new Excel data table.
Three, the code is as follows:
Import Org.testng.annotations.Test;
Import Org.testng.annotations.BeforeClass;
Import Org.testng.annotations.DataProvider;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Org.apache.poi.hssf.usermodel.HSSFCell;
Import Org.apache.poi.hssf.usermodel.HSSFRow;
Import Org.apache.poi.hssf.usermodel.HSSFSheet;
Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;
Import Org.apache.poi.ss.usermodel.Cell;
Import Org.openqa.selenium.By;
Import Org.openqa.selenium.WebDriver;
Import Org.openqa.selenium.chrome.ChromeDriver;
Import Org.testng.annotations.AfterClass;
public class Excelparameter {
Using the Dataprovider keyword to drive parameterization
@DataProvider (name = "Logindata")
Public object[][] Logindata () throws IOException {
Return GetData ("F:\\hell.xls");
}
@Test (Dataprovider = "Logindata")
public void f (String uername, string passwd) {
Output information in the input box
System.out.println (USERNAME+PASSWD);
}
Getting data from Excel
@SuppressWarnings ("deprecation")
Public object[][] GetData (String path) throws IOException {
int i;
Int J;
Defining Rows and Columns
int rownum;
int cellnum;
String Path = "F:\\hell.xls";
Define the files to read
FileInputStream readFile = new FileInputStream (path);
First create an empty workbook, read from the specified file
@SuppressWarnings ("resource")
Hssfworkbook wb = new Hssfworkbook (readFile);
Then select the sheet to read
Hssfsheet st = wb.getsheet ("test page");
Gets the total number of rows (or St.getphysicalnumberofrows ())
RowNum = St.getlastrownum () +1;
Get the total number of columns
Cellnum = St.getrow (0). Getphysicalnumberofcells ();
Create a two-dimensional array that stores Excel data
string[][] data = new String[rownum][cellnum];
Iterate through the data in Excel and deposit it
for (i = 0; i < rownum; i++) {
for (j = 0; J < Cellnum; J + +) {
I represents each line in Excel
Hssfrow row = St.getrow (i);
J represents each column in Excel
Hssfcell cell = Row.getcell (j);
First, the values in all rows and columns are set to string format (data cannot be read if cell Gechun is a number)
Cell.setcelltype (cell.cell_type_string);
Store values for each column as well as the defined array
DATA[I][J] = Cell.getstringcellvalue ();
}
}
return data;
}
}
Third, the results of the operation
Selenium+testng+java+poi Data parameterization for Excel