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_StandartSlidesAddPage; import qsf_page_object_pattern.BackEnd.BackEnd_StandartSlidesEditPage; import qsf_page_object_pattern.BackEnd.BackEnd_StandartSlidesPage; import qsf_page_object_pattern.BackEnd.BackEnd_StandartSlidesReorderPage; import tools.DynamicDataProvider; public class be_standartSlides_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 = "slide/standard"; DynamicDataProvider dp = new DynamicDataProvider("BE_StandartSlides.json"); private String searchText; public be_standartSlides_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("Iexplorer") String browser){ SetDriver(browser); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); loginToAdmin(); } @Test(dataProvider = "Valid_Data", description = "") public void TestValidStandartSlide(Map> fields){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_StandartSlidesPage be_standartSlidesPage = new BackEnd_StandartSlidesPage(driver); BackEnd_StandartSlidesAddPage be_standartSlidesAddPage = be_standartSlidesPage.openAddNewForm(); be_standartSlidesPage = be_standartSlidesAddPage.addNew(fields); assertTrue(be_standartSlidesPage.getAlertInfo().contains(addedAlert), "Expected '"+addedAlert+"' to appear on page, but found: '"+be_standartSlidesPage.getAlertInfo()+"'"); searchText = fields.get("title").get("value"); be_standartSlidesPage.doSearch(searchText); Map FirstRecordData = be_standartSlidesPage.getFirstRecordDataFromTable(); assertTrue(fields.get("type").get("value").equals(FirstRecordData.get("type")), "Expected first date to be '"+fields.get("type").get("value")+"', but found '"+FirstRecordData.get("type")+"'"); 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_StandartSlidesEditPage be_standartSlidesEditPage = be_standartSlidesPage.editFirstRecordFromTable(); for(Entry> field : fields.entrySet()){ switch (field.getValue().get("type")) { case "fck-editor": { assertTrue(be_standartSlidesEditPage.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_standartSlidesEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")).contains(fileNameWithOutExt), "File for "+field.getKey()+" doesn't match"); } break; default: assertEquals(be_standartSlidesEditPage.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_standartSlidesEditPage.clickCancel(); } @Test(dataProvider = "Incorrect_Data", dependsOnMethods="TestValidStandartSlide", priority=2, description = "") public void TestIncorrectStandartSlide(Map> fieldsData, Map> expectedErrors) throws InterruptedException{ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_StandartSlidesPage be_standartSlidesPage = new BackEnd_StandartSlidesPage(driver); BackEnd_StandartSlidesAddPage be_standartSlidesAddPage = be_standartSlidesPage.openAddNewForm(); be_standartSlidesAddPage = be_standartSlidesAddPage.submitIncorrectData(fieldsData); Map>realFormErrors = be_standartSlidesAddPage.getFieldsAlerts(); compareFormErrors(realFormErrors, expectedErrors); } @Test(dataProvider = "Edit_Data", dependsOnMethods="TestValidStandartSlide", priority=3, enabled=true, description = "") public void TestEditStandartSlide(Map> fields){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_StandartSlidesPage be_standartSlidesPage = new BackEnd_StandartSlidesPage(driver); be_standartSlidesPage.doSearch(searchText); BackEnd_StandartSlidesEditPage be_standartSlidesEditPage = be_standartSlidesPage.editFirstRecordFromTable(); be_standartSlidesPage = be_standartSlidesEditPage.editRecord(fields); assertTrue(be_standartSlidesPage.getAlertInfo().contains(updatedAlert), "Expected '"+updatedAlert+"' to appear on page, but found: '"+be_standartSlidesPage.getAlertInfo()+"'"); searchText = fields.get("title").get("value"); be_standartSlidesPage.doSearch(searchText); Map FirstRecordData = be_standartSlidesPage.getFirstRecordDataFromTable(); assertTrue(fields.get("type").get("value").equals(FirstRecordData.get("type")), "Expected first date to be '"+fields.get("type").get("value")+"', but found '"+FirstRecordData.get("type")+"'"); 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("backgroundImage").get("value")); assertTrue(FirstRecordData.get("image").contains(fileNameWithOutExt), "Expected Image '"+fields.get("backgroundImage").get("value")+"' to be present in '"+FirstRecordData.get("image")+"'. But was not found."); be_standartSlidesEditPage = be_standartSlidesPage.editFirstRecordFromTable(); for(Entry> field : fields.entrySet()){ switch (field.getValue().get("type")) { case "fck-editor": { assertTrue(be_standartSlidesEditPage.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_standartSlidesEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")).contains(fileNameWithOutExt), "File for "+field.getKey()+" doesn't match"); } break; default: assertEquals(be_standartSlidesEditPage.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="TestEditStandartSlide", priority=5, enabled=true, description = "") public void TestIncorrectEditStandartSlide(Map> fieldsData, Map> expectedErrors) throws InterruptedException{ if(fieldsData.containsKey("image")&&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_StandartSlidesPage be_standartSlidesPage = new BackEnd_StandartSlidesPage(driver); be_standartSlidesPage.doSearch(searchText); BackEnd_StandartSlidesEditPage be_standartSlidesEditPage = be_standartSlidesPage.editFirstRecordFromTable(); Map> clearData = dp.GenerateClearForm(); clearData.remove("linkType"); clearData.remove("type"); be_standartSlidesEditPage.clearForm(clearData); be_standartSlidesEditPage = be_standartSlidesEditPage.submitIncorrectData(fieldsData); Map>realFormErrors = be_standartSlidesEditPage.getFieldsAlerts(); compareFormErrors(realFormErrors, expectedErrors); } @Test(dataProvider = "Valid_Data", dependsOnMethods = "TestEditStandartSlide", priority=4, description = "") public void TestAnotherValidStandartSlide(Map> fields){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_StandartSlidesPage be_standartSlidesPage = new BackEnd_StandartSlidesPage(driver); BackEnd_StandartSlidesAddPage be_standartSlidesAddPage = be_standartSlidesPage.openAddNewForm(); be_standartSlidesPage = be_standartSlidesAddPage.addNew(fields); assertTrue(be_standartSlidesPage.getAlertInfo().contains(addedAlert), "Expected '"+addedAlert+"' to appear on page, but found: '"+be_standartSlidesPage.getAlertInfo()+"'"); searchText = fields.get("title").get("value"); be_standartSlidesPage.doSearch(searchText); Map FirstRecordData = be_standartSlidesPage.getFirstRecordDataFromTable(); assertTrue(fields.get("type").get("value").equals(FirstRecordData.get("type")), "Expected first date to be '"+fields.get("type").get("value")+"', but found '"+FirstRecordData.get("type")+"'"); 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_StandartSlidesEditPage be_standartSlidesEditPage = be_standartSlidesPage.editFirstRecordFromTable(); for(Entry> field : fields.entrySet()){ switch (field.getValue().get("type")) { case "fck-editor": { assertTrue(be_standartSlidesEditPage.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_standartSlidesEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")).contains(fileNameWithOutExt), "File for "+field.getKey()+" doesn't match"); } break; default: assertEquals(be_standartSlidesEditPage.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_standartSlidesEditPage.clickCancel(); } @Test(dependsOnMethods="TestAnotherValidStandartSlide", priority=5) public void TestReorderStandartSlides(){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_StandartSlidesPage be_standartSlidesPage = new BackEnd_StandartSlidesPage(driver); ListtitleOrder = be_standartSlidesPage.getTableTitlesByOrder(); BackEnd_StandartSlidesReorderPage be_standartSlidesReorderPage = be_standartSlidesPage.openReorderPage(); String firstTitle = titleOrder.get(0); be_standartSlidesPage = be_standartSlidesReorderPage.moveElementToBottom(firstTitle); assertTrue(be_standartSlidesPage.getAlertInfo().contains(reorderedAlert), "Expected '"+reorderedAlert+"' to appear on page, but found: '"+be_standartSlidesPage.getAlertInfo()+"'"); titleOrder = be_standartSlidesPage.getTableTitlesByOrder(); assertEquals(titleOrder.get(titleOrder.size() - 1), firstTitle); driver.get(settings.http + settings.adminUrl + modulePath); be_standartSlidesPage = new BackEnd_StandartSlidesPage(driver); titleOrder = be_standartSlidesPage.getTableTitlesByOrder(); be_standartSlidesReorderPage = be_standartSlidesPage.openReorderPage(); String lastTitle = titleOrder.get(titleOrder.size() - 1); be_standartSlidesPage = be_standartSlidesReorderPage.moveElementToTop(lastTitle); assertTrue(be_standartSlidesPage.getAlertInfo().contains(reorderedAlert), "Expected '"+reorderedAlert+"' to appear on page, but found: '"+be_standartSlidesPage.getAlertInfo()+"'"); titleOrder = be_standartSlidesPage.getTableTitlesByOrder(); assertEquals(titleOrder.get(0), lastTitle); } @Test(dataProvider = "CleanUp_Data", dependsOnMethods="TestAnotherValidStandartSlide", priority=6, description = "") public void cleanUpStandartSlides(Map> fields){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_StandartSlidesPage be_standartSlidesPage = new BackEnd_StandartSlidesPage(driver); searchText = fields.get("title").get("value"); be_standartSlidesPage.doSearch(searchText); try { be_standartSlidesPage.deleteFirstRecordFromTable(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } assertTrue(be_standartSlidesPage.getAlertInfo().contains(deletedAlert), "Expected '"+deletedAlert+"' to appear on page, but found: '"+be_standartSlidesPage.getAlertInfo()+"'"); } @Test(priority=7, description = "") public void testCancelButton(){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_StandartSlidesPage be_standartSlidesPage = new BackEnd_StandartSlidesPage(driver); BackEnd_StandartSlidesAddPage be_standartSlidesAddPage = be_standartSlidesPage.openAddNewForm(); be_standartSlidesPage = be_standartSlidesAddPage.clickCancel(); be_standartSlidesPage.doSearch(searchText); driver.findElement(By.id("btnCancel")).click(); be_standartSlidesPage = new BackEnd_StandartSlidesPage(driver); BackEnd_StandartSlidesReorderPage be_standartSlidesReorderPage = be_standartSlidesPage.openReorderPage(); be_standartSlidesReorderPage.clickCancel(); } @AfterClass public void tearDown(){ driver.quit(); //driver.close(); } }