public class ExcelDataProviderImpl extends Object implements ExcelDataProvider
Modifier and Type | Field and Description |
---|---|
protected com.paypal.selion.platform.dataprovider.impl.ExcelReader |
excelReader |
protected static SimpleLogger |
logger |
protected DataResource |
resource |
Constructor and Description |
---|
ExcelDataProviderImpl(DataResource resource)
The constructor will use the path name and the file name of the Excel workbook to initialize the input stream
before the stream is being used by several methods to get the test data from the Excel workbook.
|
Modifier and Type | Method and Description |
---|---|
void |
addCustomTypes(DefaultCustomType type) |
Object[][] |
getAllData()
This function will read the whole excel sheet and map the data into two-dimensional array of object which is
compatible with TestNG DataProvider to provide real test data driven development.
|
List<org.apache.poi.ss.usermodel.Row> |
getAllRawExcelRows(String sheetName,
boolean heading)
Get all excel rows from a specified sheet.
|
Hashtable<String,Object> |
getDataAsHashtable()
This function will read all rows of a specified excel sheet and store the data to a hash table.
|
Iterator<Object[]> |
getDataByFilter(DataProviderFilter dataFilter)
Gets data from Excel sheet by applying the given filter.
|
Object[][] |
getDataByIndex(int[] indexes)
This function will use the input string representing the indexes to collect and return the correct excel sheet
data rows as two dimensional object to be used as TestNG DataProvider.
|
Object[][] |
getDataByIndex(String indexes)
This function will use the input string representing the indexes to collect and return the correct excel sheet
data rows as two dimensional object to be used as TestNG DataProvider.
|
Object[][] |
getDataByKeys(String[] keys)
This function will use the input string representing the keys to collect and return the correct excel sheet data
rows as two dimensional object to be used as TestNG DataProvider.
|
List<String> |
getHeaderRowContents(String sheetName,
int size)
Utility to get the header row contents of the excel sheet
|
List<String> |
getRowContents(String sheetName,
int rowIndex,
int size)
Using the specified rowIndex to search for the row from the specified Excel sheet, then return the row contents
in a list of string format.
|
Object |
getSingleExcelRow(int index)
This method can be used to fetch a particular row from an excel sheet.
|
protected Object |
getSingleExcelRow(Object userObj,
int index,
boolean isExternalCall) |
protected Object |
getSingleExcelRow(Object userObj,
String key,
boolean isExternalCall)
This method fetches a specific row from an excel sheet which can be identified using a key and returns the data
as an Object which can be cast back into the user's actual data type.
|
Object |
getSingleExcelRow(String key)
This method fetches a specific row from an excel sheet which can be identified using a key and returns the data
as an Object which can be cast back into the user's actual data type.
|
protected Object |
prepareObject(Object userObj,
Field[] fields,
List<String> excelRowData,
Map<String,String> headerRowDataMap)
Currently this function will handle these data types:
1.
|
protected DataResource resource
protected com.paypal.selion.platform.dataprovider.impl.ExcelReader excelReader
protected static final SimpleLogger logger
public ExcelDataProviderImpl(DataResource resource) throws IOException
String pathName = "src/test/java"; String fileName = "DataReaderTest.xls" LOCAL_DATA myData = new LOCAL_DATA(); Object [][] myObj; // To get a single row of excel sheet using a key associated with the data DataResource resource = new FileSystemResource(pathName, fileName, myData.class); myData = (LOCAL_DATA) SimpleExcelDataProvider dataProvider = new SimpleExcelDataProvider( resource).getSingleExcelRow("4"); // To get a whole sheet of excel data. This will not need key. myObj = new SimpleExcelDataProvider(resource).getAllData(); myData = (LOCAL_DATA)myObj[1][0]; System.out.println(myObj.seller.bank[0].name);
resource
- A DataResource
object that represents an excel spreadsheet.IOException
public Hashtable<String,Object> getDataAsHashtable()
getAllData()
this method will skip ALL blank rows
that may occur in between data in the spreadsheet. getAllData()
NEED NOT be the same.
... MyDataStructure myObj = new MyDataStructure(); HashTable<String, Object> myExcelTableData; ... myExceltableData = SimpleExcelDataProvider.getDataAsHashtable();
getDataAsHashtable
in interface SeLionDataProvider
Hashtable
that represents the excel sheet data in form of hashTable.public Object getSingleExcelRow(String key)
getSingleExcelRow
in interface ExcelDataProvider
key
- - A string that represents a key to search for in the excel sheetpublic Object getSingleExcelRow(int index)
getSingleExcelRow
in interface ExcelDataProvider
index
- - The row number from the excel sheet that is to be read. For e.g., if you wanted to read the 2nd row
(which is where your data exists) in your excel sheet, the value for index would be 1. This method
assumes that your excel sheet would have a header which it would EXCLUDE. When specifying index
value always remember to ignore the header, since this method will look for a particular row ignoring
the header row.public Object[][] getDataByIndex(String indexes)
getDataByIndex
in interface SeLionDataProvider
indexes
- the string represent the keys for the search and return the wanted rows. It is in the format of: public Object[][] getDataByIndex(int[] indexes)
getDataByIndex
in interface SeLionDataProvider
indexes
- the string represent the keys for the search and return the wanted rows. It is in the format of: public Object[][] getDataByKeys(String[] keys)
getDataByKeys
in interface SeLionDataProvider
keys
- the string represents the list of key for the search and return the wanted row. It is in the format of
{"row1", "row3", "row5"}public Object[][] getAllData()
'@DataProvider(name = "dataProvider1")' public Object[][] createData1() throws Exception { // Declare your objects String pathName = "src/test/java/com/paypal/test/datareader"; String fileName = "DataReader.xls"; // Declare your data block LOCAL_DATA myData = new LOCAL_DATA(); // Pass your data block to "getAllExcelRows" FileSystemResource resource = new FileSystemResource(pathName, fileName, myData.class); Object[][] object = new SimpleExcelDataProvider(resource).getAllData(); // return the two-dimensional array object return object; } // Specify our TestNG DataProvider '@Test(dataProvider = "dataProvider1")' public void verifyLocalData1(LOCAL_DATA data) { // Your data will be distribute to your test case // one row per instance, and all can be run at the same time. System.out.println("Name: " + data.name); System.out.println("Password: " + data.password); System.out.println("the bank: " + data.bank.bankName); System.out.println("Ph1: " + data.phone.areaCode); System.out.println("Ph2: " + data.cell.areaCode); System.out.println("Bank Address: " + data.bank.address.street); System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); }
getAllData
in interface SeLionDataProvider
public Iterator<Object[]> getDataByFilter(DataProviderFilter dataFilter)
getDataByFilter
in interface SeLionDataProvider
dataFilter
- an implementation class of DataProviderFilter
public final void addCustomTypes(DefaultCustomType type)
addCustomTypes
in interface ExcelDataProvider
type
- - A DefaultCustomType
that represents custom types that need to be taken into consideration
when generating an Object that represents every row of data from the excel sheet.protected Object getSingleExcelRow(Object userObj, String key, boolean isExternalCall)
userObj
- An Object into which data is to be packed intokey
- A string that represents a key to search for in the excel sheetisExternalCall
- A boolean that helps distinguish internally if the call is being made internally or by the user. For
external calls the index of the row would need to be bumped up,because the first row is to be ignored
always.protected Object getSingleExcelRow(Object userObj, int index, boolean isExternalCall)
userObj
- The User defined object into which the data is to be packed into.index
- The row number from the excel sheet that is to be read. For e.g., if you wanted to read the 2nd row
(which is where your data exists) in your excel sheet, the value for index would be 1. This method
assumes that your excel sheet would have a header which it would EXCLUDE. When specifying index
value always remember to ignore the header, since this method will look for a particular row ignoring
the header row.isExternalCall
- A boolean that helps distinguish internally if the call is being made internally or by the user.protected Object prepareObject(Object userObj, Field[] fields, List<String> excelRowData, Map<String,String> headerRowDataMap) throws IllegalAccessException
userObj
- this object is used by the function to extract the object info, such as class name, objects
declarations, object data structure...fields
- the array contains the list of name in the specify data structureexcelRowData
- the raw data read from the excel sheet to be extracted and filled up the object before return the full
object to the caller.headerRowDataMap
- this map has the excel header row as key and the current row that is used to prepare Object as valueIllegalAccessException
public List<String> getRowContents(String sheetName, int rowIndex, int size)
getRowContents
in interface ExcelDataProvider
rowIndex
- The row number from the excel sheet that is to be read. For e.g., if you wanted to read the 2nd row
(which is where your data exists) in your excel sheet, the value for index would be 1. This method
assumes that your excel sheet would have a header which it would EXCLUDE. When specifying index
value always remember to ignore the header, since this method will look for a particular row ignoring
the header row.size
- The number of columns to read, including empty and blank column.public List<String> getHeaderRowContents(String sheetName, int size)
sheetName
- The excel sheet name where the data is to be fetchedsize
- The number of columns to read, including empty and blank column.public List<org.apache.poi.ss.usermodel.Row> getAllRawExcelRows(String sheetName, boolean heading)
getAllRawExcelRows
in interface ExcelDataProvider
sheetName
- A String that represents the Sheet name from which data is to be readheading
- If true, will return all rows along with the heading row. If false, will return all rows except the
heading row.Row
that are read.Copyright © 2016 PayPal Open Source. All rights reserved.