Best SeLion code snippet using com.paypal.selion.platform.dataprovider.pojos.excel.USER
Source:ExcelDataProviderTest.java
...37import com.paypal.selion.platform.dataprovider.impl.ExcelDataProviderImpl;38import com.paypal.selion.platform.dataprovider.impl.FileSystemResource;39import com.paypal.selion.platform.dataprovider.impl.InputStreamResource;40import com.paypal.selion.platform.dataprovider.pojos.excel.AREA_CODE;41import com.paypal.selion.platform.dataprovider.pojos.excel.USER;42import com.paypal.selion.platform.utilities.FileAssistant;43public class ExcelDataProviderTest {44 private static String fileName = "src/test/resources/User.xlsx";45 private static final String assertFailedMsg = "Assert condition failed.";46 private ExcelDataProvider dataSource;47 public static class MyCustomClass {48 private final String name;49 public MyCustomClass(String name) {50 this.name = name;51 }52 public String getName() {53 return name;54 }55 }56 @BeforeClass(alwaysRun = true)57 public void init() throws IOException {58 DataResource resource = new FileSystemResource(fileName, USER.class);59 dataSource = (ExcelDataProvider) DataProviderFactory.getDataProvider(resource);60 }61 public static class ColorsData {62 private String productName;63 private Colors whatColor;64 /**65 * @return the productName66 */67 public String getProductName() {68 return productName;69 }70 /**71 * @param productName72 * the productName to set73 */74 public void setProductName(String productName) {75 this.productName = productName;76 }77 /**78 * @return the whatColor79 */80 public Colors getWhatColor() {81 return whatColor;82 }83 /**84 * @param whatColor85 * the whatColor to set86 */87 public void setWhatColor(Colors whatColor) {88 this.whatColor = whatColor;89 }90 }91 public static class TweakedColorsData {92 private String productName;93 private List<String> whatColor;94 /**95 * @return the productName96 */97 public String getProductName() {98 return productName;99 }100 /**101 * @param productName102 * the productName to set103 */104 public void setProductName(String productName) {105 this.productName = productName;106 }107 /**108 * @return the whatColor109 */110 public List<String> getWhatColor() {111 return whatColor;112 }113 /**114 * @param whatColor115 * the whatColor to set116 */117 public void setWhatColor(List<String> whatColor) {118 this.whatColor = whatColor;119 }120 }121 @Test(groups = "unit")122 public void testInjectCustomData() throws IOException, NoSuchMethodException, SecurityException {123 DataResource resource = new InputStreamResource(new BufferedInputStream(124 FileAssistant.loadFile("src/test/resources/sampleData.xlsx")), ColorsData.class, "xlsx");125 ExcelDataProvider provider = (ExcelDataProvider) DataProviderFactory.getDataProvider(resource);126 DefaultCustomType type = new DefaultCustomType(Colors.class, Colors.class.getMethod("whatColor", String.class));127 provider.addCustomTypes(type);128 Object[][] data = provider.getAllData();129 List<Colors> expectedValues = Arrays.asList(Colors.values());130 assertTrue(data.length == 3);131 for (Object[] eachObjectRow : data) {132 ColorsData tData = (ColorsData) eachObjectRow[0];133 assertTrue(expectedValues.contains(tData.whatColor));134 }135 }136 @Test(groups = "unit", expectedExceptions = { IllegalArgumentException.class })137 public void testBehaviorWhenPojoClassHasInterfaces() throws IOException {138 DataResource resource = new InputStreamResource(new BufferedInputStream(139 FileAssistant.loadFile("src/test/resources/sampleData.xlsx")), TweakedColorsData.class, "xlsx");140 SeLionDataProvider provider = DataProviderFactory.getDataProvider(resource);141 provider.getAllData();142 }143 @Test(groups = "unit")144 public void testGetSingleExcelRowWithIndexFirstRowCondition() {145 Object[][] allUsers = new Object[][] { { dataSource.getSingleExcelRow(1) } };146 List<String> fetchedNames = transformExcelDataIntoList(allUsers);147 assertTrue(arrayComparer(new String[] { "Thomas" }, fetchedNames.toArray()), assertFailedMsg);148 }149 @Test(groups = "unit")150 public void testGetSingleExcelRowWithIndex() {151 Object[][] allUsers = new Object[][] { { dataSource.getSingleExcelRow(4) } };152 List<String> fetchedNames = transformExcelDataIntoList(allUsers);153 assertTrue(arrayComparer(new String[] { "suri" }, fetchedNames.toArray()), assertFailedMsg);154 }155 @Test(groups = "unit")156 public void testGetSingleExcelRowWithKeyFirstRowCondition() {157 Object[][] allUsers = new Object[][] { { dataSource.getSingleExcelRow("tom") } };158 List<String> fetchedNames = transformExcelDataIntoList(allUsers);159 assertTrue(arrayComparer(new String[] { "Thomas" }, fetchedNames.toArray()), assertFailedMsg);160 }161 @Test(groups = "unit")162 public void testGetSingleExcelRowWithKey() {163 Object[][] allUsers = new Object[][] { { dataSource.getSingleExcelRow("3") } };164 List<String> fetchedNames = transformExcelDataIntoList(allUsers);165 assertTrue(arrayComparer(new String[] { "suri" }, fetchedNames.toArray()), assertFailedMsg);166 }167 @Test(expectedExceptions = { DataProviderException.class }, groups = "unit")168 public void testGetSingleExcelRowWithInvalidKey() {169 dataSource.getSingleExcelRow("selion");170 }171 @Test(groups = "unit", expectedExceptions = { DataProviderException.class })172 public void testGetSingleExcelRowWithInvalidIndex() {173 assertNull(dataSource.getSingleExcelRow(100), "Returned data should have been null");174 }175 @Test(expectedExceptions = { DataProviderException.class }, groups = "unit")176 public void testGetExcelRowsNegativeConditions() throws IOException {177 dataSource.getDataByIndex("2~3");178 }179 @Test(groups = "unit")180 public void testGetExcelRowsWithKeys() {181 Object[][] allUsers = dataSource.getDataByKeys(new String[] { "tom", "binh" });182 List<String> fetchedNames = transformExcelDataIntoList(allUsers);183 assertTrue(arrayComparer(new String[] { "Thomas", "binh" }, fetchedNames.toArray()), assertFailedMsg);184 }185 @Test(expectedExceptions = { DataProviderException.class }, groups = "unit")186 public void testGetExcelRowsWithInvalidKeys() {187 dataSource.getDataByKeys(new String[] { "selion" });188 }189 @Test(groups = "unit")190 public void testGetExcelRowsWithIndividualIndexes() throws IOException {191 Object[][] allUsers = dataSource.getDataByIndex("2,3");192 List<String> fetchedNames = transformExcelDataIntoList(allUsers);193 assertTrue(arrayComparer(new String[] { "rama", "binh" }, fetchedNames.toArray()), assertFailedMsg);194 }195 @Test(groups = "unit")196 public void testGetExcelRowsWithIndividualIndexesArray() throws IOException {197 int[] index = { 2, 3 };198 Object[][] allUsers = dataSource.getDataByIndex(index);199 List<String> fetchedNames = transformExcelDataIntoList(allUsers);200 assertTrue(arrayComparer(new String[] { "rama", "binh" }, fetchedNames.toArray()), assertFailedMsg);201 }202 public List<String> transformExcelDataIntoList(Object[][] allUsers) {203 List<String> fetchedNames = new ArrayList<String>();204 for (Object[] object : allUsers) {205 USER user = (USER) object[0];206 fetchedNames.add(user.getName());207 }208 return fetchedNames;209 }210 public List<String> transformExcelDataIntoList(Iterator<Object[]> allUsers) {211 List<String> fetchedNames = new ArrayList<String>();212 while (allUsers.hasNext()) {213 USER user = (USER) allUsers.next()[0];214 fetchedNames.add(user.getName());215 }216 return fetchedNames;217 }218 @Test(groups = "unit")219 public void testGetExcelRowsWithRangeOfIndexes() throws IOException {220 Object[][] allUsers = dataSource.getDataByIndex("1-2");221 List<String> fetchedNames = transformExcelDataIntoList(allUsers);222 assertTrue(arrayComparer(new String[] { "Thomas", "rama" }, fetchedNames.toArray()), assertFailedMsg);223 }224 @Test(groups = "unit")225 public void testGetExcelRowsWithIndividualAndRangeOfIndexes() throws IOException {226 Object[][] allUsers = dataSource.getDataByIndex("1-2,4,6");227 List<String> fetchedNames = transformExcelDataIntoList(allUsers);228 assertTrue(arrayComparer(new String[] { "Thomas", "rama", "suri", "suri" }, fetchedNames.toArray()),229 assertFailedMsg);230 }231 @Test(groups = "unit", expectedExceptions = { DataProviderException.class })232 public void testGetExcelRowsWhereRowIsNull() throws IOException {233 Object[][] allUsers = dataSource.getDataByIndex("5");234 assertNull(allUsers[0][0], assertFailedMsg);235 }236 @Test(groups = "unit")237 public void testGetExcelRowsWithSimpleInclusionDataProviderFilterWithRangeOfIndexes() throws IOException {238 SimpleIndexInclusionFilter filter = new SimpleIndexInclusionFilter("1-2");239 Iterator<Object[]> allUsers = dataSource.getDataByFilter(filter);240 List<String> fetchedNames = transformExcelDataIntoList(allUsers);241 assertTrue(arrayComparer(new String[] { "Thomas", "rama" }, fetchedNames.toArray()), assertFailedMsg);242 }243 @Test(groups = "unit")244 public void testGetExcelRowsWithSimpleInclusionDataProviderFilterWithIndividualAndRangeOfIndexes()245 throws IOException {246 SimpleIndexInclusionFilter filter = new SimpleIndexInclusionFilter("1-2,4,5");247 Iterator<Object[]> allUsers = dataSource.getDataByFilter(filter);248 List<String> fetchedNames = transformExcelDataIntoList(allUsers);249 assertTrue(arrayComparer(new String[] { "Thomas", "rama", "suri", "suri" }, fetchedNames.toArray()),250 assertFailedMsg);251 }252 @Test(groups = "unit", expectedExceptions = { IllegalArgumentException.class }, expectedExceptionsMessageRegExp = "Please provide valid indexes for filtering")253 public void testGetExcelRowsWithSimpleInclusionDataProviderFilterWithNullIndexes() {254 // Passing just null will give compilation error.255 new SimpleIndexInclusionFilter((String) null);256 }257 @Test(groups = "unit")258 public void testGetExcelRowsWithSimpleInclusionDataProviderFilterWhereNoValuesReturns() throws IOException {259 SimpleIndexInclusionFilter filter = new SimpleIndexInclusionFilter("6");260 Iterator<Object[]> allUsers = dataSource.getDataByFilter(filter);261 assertFalse(allUsers.hasNext(), assertFailedMsg);262 }263 @Test(groups = "unit")264 public void testGetExcelRowsWithCustomKeyInclusionDataProviderFilterWithAccountNumber() throws IOException {265 CustomKeyFilter filter = new CustomKeyFilter("accountNumber", "78901,124567");266 Iterator<Object[]> allUsers = dataSource.getDataByFilter(filter);267 List<String> fetchedNames = transformExcelDataIntoList(allUsers);268 assertTrue(arrayComparer(new String[] { "Thomas", "binh" }, fetchedNames.toArray()), assertFailedMsg);269 }270 @Test(groups = "unit")271 public void testGetExcelRowsWithCustomKeyInclusionDataProviderFilterWithPhoneNumber() throws IOException {272 CustomKeyFilter filter = new CustomKeyFilter("phoneNumber", "1-408-666-5508,1-408-225-8040,1-714-666-0043");273 Iterator<Object[]> allUsers = dataSource.getDataByFilter(filter);274 List<String> fetchedNames = transformExcelDataIntoList(allUsers);275 assertTrue(arrayComparer(new String[] { "Thomas", "rama", "binh" }, fetchedNames.toArray()), assertFailedMsg);276 }277 @Test(groups = "unit", expectedExceptions = { IllegalArgumentException.class }, expectedExceptionsMessageRegExp = "Please specify values to use for filtering.")278 public void testGetExcelRowsWithCustomKeyInclusionDataProviderFilterWithNullFilterKeyValues() {279 @SuppressWarnings("unused")280 CustomKeyFilter filter = new CustomKeyFilter("phoneNumber", null);281 }282 @Test(groups = "unit", expectedExceptions = { IllegalArgumentException.class }, expectedExceptionsMessageRegExp = "Please specify a valid key.")283 public void testGetExcelRowsWithCustomKeyInclusionDataProviderFilterWithNullFilterKey() {284 new CustomKeyFilter(null, "1-408-666-5508,1-408-225-8040,1-714-666-0043");285 }286 private synchronized boolean arrayComparer(String[] expected, Object[] actual) {287 boolean isSame = false;288 for (int i = 0; i < expected.length; i++) {289 isSame = expected[i].matches((String) actual[i]);290 }291 return isSame;292 }293 @Test(groups = "unit")294 public void testGetAllExcelRows() throws IOException {295 Object[][] allUsers = dataSource.getAllData();296 assertNotNull(allUsers, "Data read from excel sheet failed");297 // Reduce 2 from the actual count, since the test excel sheet has 1 blank row298 // and 1 row for header299 assertEquals(allUsers.length, getRowCountFromSheet(USER.class.getSimpleName()) - 1,300 "Failed reading all rows from spreadsheet");301 }302 @Test(groups = "unit")303 public void testGetAllRowsAsHashTable() {304 Hashtable<String, Object> allValues = dataSource.getDataAsHashtable();305 assertNotNull(allValues, "Data read from excel sheet failed");306 assertEquals(allValues.size(), getRowCountFromSheet(USER.class.getSimpleName()) - 2,307 "Failed reading all rows from spreadsheet");308 }309 @Test(expectedExceptions = { IllegalArgumentException.class }, groups = "unit")310 public void testGetAllRowsAsHashTableInvalidSheetName() throws IOException {311 Student student = new ExcelDataProviderTest().new Student();312 DataResource resource = new FileSystemResource(fileName, student.getClass());313 SeLionDataProvider provider = DataProviderFactory.getDataProvider(resource);314 provider.getDataAsHashtable();315 }316 @Test(expectedExceptions = { IllegalArgumentException.class }, groups = "unit")317 public void testGetallExcelRowsInvalidSheetName() throws IOException {318 Student student = new ExcelDataProviderTest().new Student();319 DataResource resource = new FileSystemResource(fileName, student.getClass());320 SeLionDataProvider provider = DataProviderFactory.getDataProvider(resource);321 provider.getAllData();322 }323 @Test(expectedExceptions = { IllegalArgumentException.class }, groups = "unit")324 public void negativeTestsWithExcelDataProviderConstructor() throws IOException {325 new ExcelDataProviderImpl(null);326 }327 @Test(expectedExceptions = { IOException.class }, groups = "unit")328 public void negativeTestsInvalidFileName() throws IOException {329 DataResource resource = new FileSystemResource("IdontExist.xls", ColorsData.class);330 DataProviderFactory.getDataProvider(resource);331 }332 @Test(groups = "unit")333 public void getAllRowsAsHash() {334 assertNotNull(dataSource.getDataAsHashtable());335 }336 @Test(groups = "unit")337 public void getSheetAsHashByKeyTest1() {338 USER user = (USER) dataSource.getDataAsHashtable().get("binh");339 assertData(user);340 }341 @Test(groups = "unit")342 public void getSheetAsHashByKeyTest2() {343 USER user = (USER) dataSource.getDataAsHashtable().get("1");344 assertData(user);345 }346 @DataProvider(parallel = true)347 public Object[][] getExcelDataRowsByKeys() {348 return dataSource.getDataByKeys(new String[] { "1", "binh" });349 }350 @Test(dataProvider = "getExcelDataRowsByKeys", groups = "unit")351 public void getExcelDataRowsByKeys(USER myData) {352 assertData(myData);353 for (AREA_CODE eachArea : myData.getAreaCode()) {354 assertNotNull(eachArea.getAreaCode(), "Area code should not have been null");355 }356 }357 @DataProvider(parallel = true)358 public Object[][] getExcelDataRowsByIndexes() throws IOException {359 return dataSource.getDataByIndex("2, 3-4");360 }361 @Test(dataProvider = "getExcelDataRowsByIndexes", groups = "unit")362 public void getExcelDataRowsByIndexes(USER myData) {363 assertData(myData);364 for (AREA_CODE eachArea : myData.getAreaCode()) {365 assertNotNull(eachArea.getAreaCode(), "Area code should not have been null");366 }367 }368 @DataProvider(parallel = true)369 public Object[][] getAllExcelRows() throws IOException {370 return dataSource.getAllData();371 }372 @Test(dataProvider = "getAllExcelRows", groups = "unit")373 public void getAllExcelRows(USER myData) {374 assertData(myData);375 for (AREA_CODE eachArea : myData.getAreaCode()) {376 assertNotNull(eachArea.getAreaCode(), "Area code should not have been null");377 }378 }379 private void assertData(USER data) {380 assertNotNull(data);381 assertNotNull(data.getName());382 assertNotNull(data.getPassword());383 assertNotNull(data.getAreaCode()[0].getAreaCode());384 }385 private int getRowCountFromSheet(String sheetName) {386 int rowCount = 0;387 try {388 XSSFWorkbook workBook = new XSSFWorkbook(fileName);389 rowCount = workBook.getSheet(sheetName).getPhysicalNumberOfRows();390 } catch (IOException e) {391 // do nothing with the exception here392 }393 return rowCount;394 }395 public class Student {396 private String studentName;397 public void setStudentName(String name) {398 this.studentName = name;399 }400 public String getStudentName() {401 return this.studentName;402 }403 }404 @Test(groups = "unit")405 public void testGetRowContents() {406 Field[] fields = USER.class.getDeclaredFields();407 List<String> rowContents = dataSource.getRowContents("User", 3, fields.length);408 assertNotNull(rowContents);409 assertTrue("[rama, abc123, 123456, 100.00, ph1,ph2,ph3, bnk1, 1-408-225-8040, 12, true, 12.5, 167045, 12.5, 2]"410 .equals(rowContents.toString()));411 }412 @Test(groups = "unit")413 public void testGetAllRawExcelRows() {414 List<Row> rows = dataSource.getAllRawExcelRows("User", true);415 assertNotNull(rows);416 Row singleRow = rows.get(3);417 assertTrue(singleRow.getCell(1).getStringCellValue().equals("binh"));418 assertTrue(singleRow.getCell(2).getStringCellValue().equals("abc124"));419 }420 @Test(groups = "unit")421 public void testSheetOrderMismatchWithPOJO() throws IOException {422 Object[][] rowData = dataSource.getDataByIndex("1");423 USER user = (USER) rowData[0][0] ;424 assertEquals(user.getBank().getName(), "Well fargo");425 assertEquals(user.getBank().getAddress().getStreet(), "12 Pico st");426 assertEquals(user.getBank().getType(), "savings");427 }428}...
USER
Using AI Code Generation
1@Test(dataProvider = "excelDataProvider", dataProviderClass = ExcelDataProvider.class)2public void testExcelDataProviderWithPojo(USER user) {3 System.out.println(user);4}5@Test(dataProvider = "excelDataProvider", dataProviderClass = ExcelDataProvider.class)6public void testExcelDataProviderWithMap(Map<String, String> data) {7 System.out.println(data);8}9@Test(dataProvider = "excelDataProvider", dataProviderClass = ExcelDataProvider.class)10public void testExcelDataProviderWithStringArray(String[] data) {11 System.out.println(Arrays.toString(data));12}13@Test(dataProvider = "excelDataProvider", dataProviderClass = ExcelDataProvider.class)14public void testExcelDataProviderWithObjectArray(Object[] data) {15 System.out.println(Arrays.toString(data));16}17@Test(dataProvider = "excelDataProvider", dataProviderClass = ExcelDataProvider.class)18public void testExcelDataProviderWithString(String data) {19 System.out.println(data);20}21@Test(dataProvider = "excelDataProvider", dataProviderClass = ExcelDataProvider.class)22public void testExcelDataProviderWithObject(Object data) {23 System.out.println(data);24}25@Test(dataProvider = "excelDataProvider", dataProviderClass = ExcelDataProvider.class)26public void testExcelDataProviderWithListOfMap(List<Map<String, String>> data) {27 for (Map<String, String> map : data) {28 System.out.println(map);29 }30}31@Test(dataProvider = "excelDataProvider", dataProviderClass = ExcelDataProvider.class)32public void testExcelDataProviderWithListOfStringArray(List<String[]> data) {33 for (String[] array : data) {34 System.out.println(Arrays.toString(array));35 }36}37@Test(dataProvider = "excelDataProvider", dataProviderClass = ExcelDataProvider.class)38public void testExcelDataProviderWithListOfObjectArray(List<Object[]> data) {39 for (Object[] array : data) {40 System.out.println(Arrays.toString(array));41 }42}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!