Selenium+testng+java+poi Data parameterization for Excel

Source: Internet
Author: User
Tags readfile string format testng

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

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.