package test_classes; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.ArrayUtils; import org.openqa.selenium.By; import org.testng.SkipException; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Optional; import org.testng.annotations.Parameters; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import abstract_classes.BaseTest; import qsf_page_object_pattern.BackEnd.BackEnd_WideSlidesAddPage; import qsf_page_object_pattern.BackEnd.BackEnd_WideSlidesEditPage; import qsf_page_object_pattern.BackEnd.BackEnd_WideSlidesPage; import qsf_page_object_pattern.BackEnd.BackEnd_WideSlidesReorderPage; import tools.DynamicDataProvider; public class be_wideSlides_test extends BaseTest{ final String addedAlert = "Slide has been added"; final String updatedAlert = "Slide has been updated"; final String deletedAlert = "Slide has been deleted"; final String reorderedAlert = "Slides have been reordered"; final String modulePath = "site-content/slide"; DynamicDataProvider dp = new DynamicDataProvider("BE_WideSlides.json"); private String searchText; public be_wideSlides_test(){ } @DataProvider(name = "Incorrect_Data") public Object[][] createData2() { //dp.GenerateRequiredCheck(); return dp.GenerateFullCheck(); } @DataProvider(name = "Valid_Data") public Object[][] createData1() { return dp.GenerateTreeRecord("existingValue"); } @DataProvider(name = "Edit_Data") public Object[][] createData3() { return dp.GenerateTreeRecord("editValue"); } @DataProvider(name = "CleanUp_Data") public Object[][] createData4() { //return dp.GenerateTreeRecord("editValue"); return ArrayUtils.addAll(dp.GenerateTreeRecord("editValue"), dp.GenerateTreeRecord("existingValue")); } @Parameters({"browser"}) @BeforeClass public void SetUp(@Optional("Firefox") String browser){ SetDriver(browser); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); loginToAdmin(); } @Test(dataProvider = "Valid_Data", description = "") public void TestValidWideSlide(Map> fields){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_WideSlidesPage be_wideSlidesPage = new BackEnd_WideSlidesPage(driver); BackEnd_WideSlidesAddPage be_wideSlidesAddPage = be_wideSlidesPage.openAddNewForm(); be_wideSlidesPage = be_wideSlidesAddPage.addNew(fields); assertTrue(be_wideSlidesPage.getAlertInfo().contains(addedAlert), "Expected '"+addedAlert+"' to appear on page, but found: '"+be_wideSlidesPage.getAlertInfo()+"'"); searchText = fields.get("title").get("value"); be_wideSlidesPage.doSearch(searchText); Map FirstRecordData = be_wideSlidesPage.getFirstRecordDataFromTable(); assertTrue(fields.get("title").get("value").equals(FirstRecordData.get("title")), "Expected last title to be '"+fields.get("title").get("value")+"', but found '"+FirstRecordData.get("title")+"'"); String fileNameWithOutExt = FilenameUtils.removeExtension(fields.get("image").get("value")); assertTrue(FirstRecordData.get("image").contains(fileNameWithOutExt), "Expected Image '"+fields.get("image").get("value")+"' to be present in '"+FirstRecordData.get("image")+"'. But was not found."); BackEnd_WideSlidesEditPage be_wideSlidesEditPage = be_wideSlidesPage.editFirstRecordFromTable(); for(Entry> field : fields.entrySet()){ switch (field.getValue().get("type")) { case "fck-editor": { assertTrue(be_wideSlidesEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")).contains(field.getValue().get("value")), "Values in Edit Mode for "+field.getKey()+"do not match with input values"); } break; case "file": { fileNameWithOutExt = FilenameUtils.removeExtension(field.getValue().get("value")); assertTrue(be_wideSlidesEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")).contains(fileNameWithOutExt), "File for "+field.getKey()+" doesn't match"); } break; default: assertEquals(be_wideSlidesEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")), field.getValue().get("value"), "Values in Edit Mode for "+field.getKey()+"do not match with input values"); } } be_wideSlidesEditPage.clickCancel(); } @Test(dataProvider = "Incorrect_Data", dependsOnMethods="TestValidWideSlide", priority=2, enabled=true, description = "") public void TestIncorrectWideSlide(Map> fieldsData, Map> expectedErrors) throws InterruptedException{ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_WideSlidesPage be_wideSlidesPage = new BackEnd_WideSlidesPage(driver); BackEnd_WideSlidesAddPage be_wideSlidesAddPage = be_wideSlidesPage.openAddNewForm(); be_wideSlidesAddPage = be_wideSlidesAddPage.submitIncorrectData(fieldsData); Map>realFormErrors = be_wideSlidesAddPage.getFieldsAlerts(); compareFormErrors(realFormErrors, expectedErrors); } @Test(dataProvider = "Edit_Data", dependsOnMethods="TestValidWideSlide", priority=3, enabled=true, description = "") public void TestEditWideSlide(Map> fields){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_WideSlidesPage be_wideSlidesPage = new BackEnd_WideSlidesPage(driver); be_wideSlidesPage.doSearch(searchText); BackEnd_WideSlidesEditPage be_wideSlidesEditPage = be_wideSlidesPage.editFirstRecordFromTable(); be_wideSlidesPage = be_wideSlidesEditPage.editRecord(fields); assertTrue(be_wideSlidesPage.getAlertInfo().contains(updatedAlert), "Expected '"+updatedAlert+"' to appear on page, but found: '"+be_wideSlidesPage.getAlertInfo()+"'"); searchText = fields.get("title").get("value"); be_wideSlidesPage.doSearch(searchText); Map FirstRecordData = be_wideSlidesPage.getFirstRecordDataFromTable(); assertTrue(fields.get("title").get("value").equals(FirstRecordData.get("title")), "Expected last title to be '"+fields.get("title").get("value")+"', but found '"+FirstRecordData.get("title")+"'"); String fileNameWithOutExt = FilenameUtils.removeExtension(fields.get("image").get("value")); assertTrue(FirstRecordData.get("image").contains(fileNameWithOutExt), "Expected Image '"+fields.get("image").get("value")+"' to be present in '"+FirstRecordData.get("image")+"'. But was not found."); be_wideSlidesEditPage = be_wideSlidesPage.editFirstRecordFromTable(); for(Entry> field : fields.entrySet()){ switch (field.getValue().get("type")) { case "fck-editor": { assertTrue(be_wideSlidesEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")).contains(field.getValue().get("value")), "Values in Edit Mode for "+field.getKey()+"do not match with input values"); } break; case "file": { fileNameWithOutExt = FilenameUtils.removeExtension(field.getValue().get("value")); assertTrue(be_wideSlidesEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")).contains(fileNameWithOutExt), "File for "+field.getKey()+" doesn't match"); } break; default: assertEquals(be_wideSlidesEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")), field.getValue().get("value"), "Values in Edit Mode for "+field.getKey()+"do not match with input values"); } } } @Test(dataProvider = "Incorrect_Data", dependsOnMethods="TestEditWideSlide", priority=5, enabled=true, description = "") public void TestIncorrectEditWideSlide(Map> fieldsData, Map> expectedErrors) throws InterruptedException{ if(fieldsData.get("image").get("value").isEmpty()){ throw new SkipException("It is unable to test Image Field for 'Required' validaton in Edit Mode"); } driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_WideSlidesPage be_wideSlidesPage = new BackEnd_WideSlidesPage(driver); be_wideSlidesPage.doSearch(searchText); BackEnd_WideSlidesEditPage be_wideSlidesEditPage = be_wideSlidesPage.editFirstRecordFromTable(); Map> clearData = dp.GenerateClearForm(); clearData.remove("linkType"); clearData.remove("type"); be_wideSlidesEditPage.clearForm(clearData); be_wideSlidesEditPage = be_wideSlidesEditPage.submitIncorrectData(fieldsData); Map>realFormErrors = be_wideSlidesEditPage.getFieldsAlerts(); compareFormErrors(realFormErrors, expectedErrors); } @Test(dataProvider = "Valid_Data", dependsOnMethods = "TestEditWideSlide", priority=4, description = "") public void TestAnotherValidWideSlide(Map> fields){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_WideSlidesPage be_wideSlidesPage = new BackEnd_WideSlidesPage(driver); BackEnd_WideSlidesAddPage be_wideSlidesAddPage = be_wideSlidesPage.openAddNewForm(); be_wideSlidesPage = be_wideSlidesAddPage.addNew(fields); assertTrue(be_wideSlidesPage.getAlertInfo().contains(addedAlert), "Expected '"+addedAlert+"' to appear on page, but found: '"+be_wideSlidesPage.getAlertInfo()+"'"); searchText = fields.get("title").get("value"); be_wideSlidesPage.doSearch(searchText); Map FirstRecordData = be_wideSlidesPage.getFirstRecordDataFromTable(); assertTrue(fields.get("title").get("value").equals(FirstRecordData.get("title")), "Expected last title to be '"+fields.get("title").get("value")+"', but found '"+FirstRecordData.get("title")+"'"); String fileNameWithOutExt = FilenameUtils.removeExtension(fields.get("image").get("value")); assertTrue(FirstRecordData.get("image").contains(fileNameWithOutExt), "Expected Image '"+fields.get("image").get("value")+"' to be present in '"+FirstRecordData.get("image")+"'. But was not found."); BackEnd_WideSlidesEditPage be_wideSlidesEditPage = be_wideSlidesPage.editFirstRecordFromTable(); for(Entry> field : fields.entrySet()){ switch (field.getValue().get("type")) { case "fck-editor": { assertTrue(be_wideSlidesEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")).contains(field.getValue().get("value")), "Values in Edit Mode for "+field.getKey()+"do not match with input values"); } break; case "file": { fileNameWithOutExt = FilenameUtils.removeExtension(field.getValue().get("value")); assertTrue(be_wideSlidesEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")).contains(fileNameWithOutExt), "File for "+field.getKey()+" doesn't match"); } break; default: assertEquals(be_wideSlidesEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")), field.getValue().get("value"), "Values in Edit Mode for "+field.getKey()+"do not match with input values"); } } be_wideSlidesEditPage.clickCancel(); } @Test(dependsOnMethods="TestAnotherValidWideSlide", priority=5) public void TestReorderWideSlides(){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_WideSlidesPage be_wideSlidesPage = new BackEnd_WideSlidesPage(driver); ListtitleOrder = be_wideSlidesPage.getTableTitlesByOrder(); BackEnd_WideSlidesReorderPage be_wideSlidesReorderPage = be_wideSlidesPage.openReorderPage(); String firstTitle = titleOrder.get(0); be_wideSlidesPage = be_wideSlidesReorderPage.moveElementToBottom(firstTitle); assertTrue(be_wideSlidesPage.getAlertInfo().contains(reorderedAlert), "Expected '"+reorderedAlert+"' to appear on page, but found: '"+be_wideSlidesPage.getAlertInfo()+"'"); titleOrder = be_wideSlidesPage.getTableTitlesByOrder(); assertEquals(titleOrder.get(titleOrder.size() - 1), firstTitle); driver.get(settings.http + settings.adminUrl + modulePath); be_wideSlidesPage = new BackEnd_WideSlidesPage(driver); titleOrder = be_wideSlidesPage.getTableTitlesByOrder(); be_wideSlidesReorderPage = be_wideSlidesPage.openReorderPage(); String lastTitle = titleOrder.get(titleOrder.size() - 1); be_wideSlidesPage = be_wideSlidesReorderPage.moveElementToTop(lastTitle); assertTrue(be_wideSlidesPage.getAlertInfo().contains(reorderedAlert), "Expected '"+reorderedAlert+"' to appear on page, but found: '"+be_wideSlidesPage.getAlertInfo()+"'"); titleOrder = be_wideSlidesPage.getTableTitlesByOrder(); assertEquals(titleOrder.get(0), lastTitle); } @Test(dataProvider = "CleanUp_Data", dependsOnMethods="TestAnotherValidWideSlide", priority=6, description = "") public void cleanUpWideSlides(Map> fields){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_WideSlidesPage be_wideSlidesPage = new BackEnd_WideSlidesPage(driver); searchText = fields.get("title").get("value"); be_wideSlidesPage.doSearch(searchText); try { be_wideSlidesPage.deleteFirstRecordFromTable(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } assertTrue(be_wideSlidesPage.getAlertInfo().contains(deletedAlert), "Expected '"+deletedAlert+"' to appear on page, but found: '"+be_wideSlidesPage.getAlertInfo()+"'"); } @Test(priority=7, description = "") public void testCancelButton(){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_WideSlidesPage be_wideSlidesPage = new BackEnd_WideSlidesPage(driver); BackEnd_WideSlidesAddPage be_wideSlidesAddPage = be_wideSlidesPage.openAddNewForm(); be_wideSlidesPage = be_wideSlidesAddPage.clickCancel(); be_wideSlidesPage.doSearch(searchText); driver.findElement(By.id("btnCancel")).click(); be_wideSlidesPage = new BackEnd_WideSlidesPage(driver); BackEnd_WideSlidesReorderPage be_wideSlidesReorderPage = be_wideSlidesPage.openReorderPage(); be_wideSlidesReorderPage.clickCancel(); } @AfterClass public void tearDown(){ driver.quit(); //driver.close(); } }