Excel Data driven during testing (Java implementation)

Source: Internet
Author: User
Tags assert

Import Java.io.FileInputStream;
Import Java.io.InputStream;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map;
Import Org.testng.Assert;
Import jxl.*;

/**
* Excel is named under the Data folder: Test the class name. xls Excel's sheet naming method: Test method name Excel first behavior map key
*
* @ClassName: Exceldataprovider
* @Description: TODO (read Excel data)
* Design idea: An Excel text represents a test class, a sheet represents a test method, and an Excel text has multiple sheet (
* i.e. multiple test methods in a Class)
*/
public class Exceldata implements iterator<object[]> {

Private Workbook book = null;
Private Sheet Sheet = null;
private int rowNum = 0;
private int currentrowno = 0;
private int columnnum = 0;
Private string[] Columnnname;

/* Construction Method: Initialize an Excel text object and get the key value */
Public Exceldata (String classname, String methodname) {

try {

int dotnum = Classname.indexof (".");

if (Dotnum > 0) {
classname = classname.substring (Classname.lastindexof (".") + 1, classname.length ());
}
Read the Excel file named after the class name from the/data folder
String Path = "c:/users/administrator/desktop/" + classname + ". xls";
InputStream InputStream = new FileInputStream (path);

Book = Workbook.getworkbook (InputStream);
Get a single sheet
Sheet = Book.getsheet (methodname);
Get all the rows in sheet
RowNum = Sheet.getrows ();
Get all cells for the first row (that is, key)
cell[] cell = sheet.getrow (0);
The length of all cells in the first row
Columnnum = Cell.length;
Creates an array of type string with length of all cell lengths in the first row: Columnnname
Columnnname = new String[cell.length];
Store the contents of all cells in the first row (that is, key) in the array: columnnname
for (int i = 0; i < cell.length; i++) {
Columnnname[i] = cell[i].getcontents (). toString ();
}
After each row is stored, it automatically points to the next line
this.currentrowno++;

} catch (Exception e) {
E.printstacktrace ();
Assert.fail ("Unable to read Excel data");
}
}

/* Override the Hasnext () method to determine if the next line exists */
public Boolean hasnext () {
If there is no content in the current sheet, or the current line number points to a row that exceeds the largest row in the current sheet, the read data is ended
if (This.rownum = = 0 | | This.currentrowno >= this.rownum) {

try {
Book.close ();
} catch (Exception e) {
E.printstacktrace ();
}
return false;
} else {
Sheet The next line is empty judgment end
if ((Sheet.getrow (Currentrowno)) [0].getcontents (). Equals (""))
return false;
return true;
}
}

/* Override the next () method to get the contents of all cells in the current row */
Public object[] Next () {
Gets all cell objects for the current row
Cell[] C = Sheet.getrow (This.currentrowno);
map<string, string> data = new hashmap<string, string> ();
Store all the contents of a cell in a map and specify the corresponding relationship between key and value
for (int i = 0; i < This.columnnum; i++) {

String temp = "";

try {
temp = C[i].getcontents (). toString ();
} catch (ArrayIndexOutOfBoundsException ex) {
temp = "";
}
Data.put (This.columnnname[i], temp);
}
Creates an array of type object of only one element for storing the key and value values for a single fetch
Object object[] = new OBJECT[1];
OBJECT[0] = data;
this.currentrowno++;
return object;
}

public void Remove () {
throw new Unsupportedoperationexception ("Remove unsupported.");
}
}

Excel Data driven during testing (Java implementation)

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.