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_PartnerAddPage; import qsf_page_object_pattern.BackEnd.BackEnd_PartnerEditPage; import qsf_page_object_pattern.BackEnd.BackEnd_PartnersPage; import qsf_page_object_pattern.BackEnd.BackEnd_PartnersReorderPage; import qsf_page_object_pattern.FrontEnd.FrontEnd_PartnersPage; import tools.DynamicDataProvider; public class be_partners_test extends BaseTest{ final String addedAlert = "Partner has been added"; final String updatedAlert = "Partner has been updated"; final String deletedAlert = "Partner has been deleted"; final String reorderedAlert = "Partners have been reordered"; final String moduleBackEndPath = "site-content/partner"; final String moduleFrontEndPath = "company/partners/"; DynamicDataProvider dp = new DynamicDataProvider("BE_Partners.json"); private String searchText; public be_partners_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("existingValue"), dp.GenerateTreeRecord("editValue")); } @Parameters({"browser"}) @BeforeClass public void SetUp(@Optional("Firefox") String browser){ SetDriver(browser); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); loginToAdmin(); } @Test(dataProvider = "Valid_Data") public void TestValidPartner(Map> fields){ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_PartnersPage be_partnersPage = new BackEnd_PartnersPage(driver); BackEnd_PartnerAddPage be_partnerAddPage = be_partnersPage.openAddNewForm(); be_partnersPage = be_partnerAddPage.addNew(fields); assertTrue(be_partnersPage.getAlertInfo().contains(addedAlert), "Expected '"+addedAlert+"' to appear on page, but found: '"+be_partnersPage.getAlertInfo()+"'"); searchText = fields.get("title").get("value"); be_partnersPage.doSearch(searchText); Map FirstRecordData = be_partnersPage.getFirstRecordDataFromTable(); assertTrue(fields.get("title").get("value").equals(FirstRecordData.get("title")), "Expected Title to be '"+fields.get("title").get("value")+"', but found '"+FirstRecordData.get("title")+"'"); assertTrue(fields.get("url").get("value").equals(FirstRecordData.get("url")), "Expected Url to be '"+fields.get("url").get("value")+"', but found '"+FirstRecordData.get("url")+"'"); 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_PartnerEditPage be_partnerEditPage = be_partnersPage.editFirstRecordFromTable(); for(Entry> field : fields.entrySet()){ switch (field.getValue().get("type")) { case "fck-editor": { assertTrue(be_partnerEditPage.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_partnerEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")).contains(fileNameWithOutExt), "File for "+field.getKey()+" doesn't match"); } break; default: assertEquals(be_partnerEditPage.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="TestValidPartner", priority=2, enabled=true) public void TestIncorrectPartnerItem(Map> fieldsData, Map> expectedErrors) throws InterruptedException{ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_PartnersPage be_partnersPage = new BackEnd_PartnersPage(driver); BackEnd_PartnerAddPage be_partnerAddPage = be_partnersPage.openAddNewForm(); be_partnerAddPage = be_partnerAddPage.submitIncorrectData(fieldsData); Map>realFormErrors = be_partnerAddPage.getFieldsAlerts(); compareFormErrors(realFormErrors, expectedErrors); } @Test(dataProvider = "Valid_Data", dependsOnMethods="TestValidPartner", priority=1) public void TestFrontEnd(Map> fields){ driver.get(settings.http + settings.siteUrl + moduleFrontEndPath); FrontEnd_PartnersPage fe_partnersPage = new FrontEnd_PartnersPage(driver); for(Entry> field : fields.entrySet()){ if(fe_partnersPage.getFrontEndValue(field.getKey())!=null){ switch (field.getKey()) { case "image":{ String fileNameWithOutExt = FilenameUtils.removeExtension(field.getValue().get("value")); assertTrue(fe_partnersPage.getFrontEndValue(field.getKey()).contains(fileNameWithOutExt), "Value for '"+field.getKey()+"' did not match with Front End"); } break; default:{ assertEquals(fe_partnersPage.getFrontEndValue(field.getKey()), field.getValue().get("value"), "Value for '"+field.getKey()+"' did not match with Front End"); } } } } } @Test(dataProvider = "Edit_Data", dependsOnMethods="TestValidPartner", priority=3, enabled=true) public void TestEditPartner(Map> fields){ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_PartnersPage be_partnersPage = new BackEnd_PartnersPage(driver); be_partnersPage.doSearch(searchText); BackEnd_PartnerEditPage be_partnerEditPage = be_partnersPage.editFirstRecordFromTable(); be_partnersPage = be_partnerEditPage.editRecord(fields); assertTrue(be_partnersPage.getAlertInfo().contains(updatedAlert), "Expected '"+updatedAlert+"' to appear on page, but found: '"+be_partnersPage.getAlertInfo()+"'"); searchText = fields.get("title").get("value"); be_partnersPage.doSearch(searchText); Map FirstRecordData = be_partnersPage.getFirstRecordDataFromTable(); assertTrue(fields.get("title").get("value").equals(FirstRecordData.get("title")), "Expected Title to be '"+fields.get("title").get("value")+"', but found '"+FirstRecordData.get("title")+"'"); assertTrue(fields.get("url").get("value").equals(FirstRecordData.get("url")), "Expected Url to be '"+fields.get("url").get("value")+"', but found '"+FirstRecordData.get("url")+"'"); 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_partnerEditPage = be_partnersPage.editFirstRecordFromTable(); for(Entry> field : fields.entrySet()){ switch (field.getValue().get("type")) { case "fck-editor": { assertTrue(be_partnerEditPage.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_partnerEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")).contains(fileNameWithOutExt), "File for "+field.getKey()+" doesn't match"); } break; default: assertEquals(be_partnerEditPage.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 = "Valid_Data", dependsOnMethods="TestEditPartner", priority=4, enabled=true) public void TestDisabledPartnerFrontEnd(Map> fields){ fields.get("show").put("value", "false"); driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_PartnersPage be_partnersPage = new BackEnd_PartnersPage(driver); BackEnd_PartnerAddPage be_partnerAddPage = be_partnersPage.openAddNewForm(); be_partnersPage = be_partnerAddPage.addNew(fields); assertTrue(be_partnersPage.getAlertInfo().contains(addedAlert), "Expected '"+addedAlert+"' to appear on page, but found: '"+be_partnersPage.getAlertInfo()+"'"); driver.get(settings.http + settings.siteUrl + moduleFrontEndPath); FrontEnd_PartnersPage fe_partnersPage = new FrontEnd_PartnersPage(driver); for(Entry> field : fields.entrySet()){ if(field.getKey()!="url" && fe_partnersPage.getFrontEndValue(field.getKey())!=null){ switch (field.getKey()) { case "image":{ String fileNameWithOutExt = FilenameUtils.removeExtension(field.getValue().get("value")); assertFalse(fe_partnersPage.getFrontEndValue(field.getKey()).contains(fileNameWithOutExt)); } break; default:{ assertNotEquals(fe_partnersPage.getFrontEndValue(field.getKey()), field.getValue().get("value")); } } } } fields.get("show").put("value", "true"); driver.get(settings.http + settings.adminUrl + moduleBackEndPath); be_partnersPage = new BackEnd_PartnersPage(driver); be_partnersPage.doSearch(fields.get("title").get("value")); BackEnd_PartnerEditPage be_partnerEditPage = be_partnersPage.editFirstRecordFromTable(); be_partnersPage = be_partnerEditPage.editRecord(fields); assertTrue(be_partnersPage.getAlertInfo().contains(updatedAlert), "Expected '"+updatedAlert+"' to appear on page, but found: '"+be_partnersPage.getAlertInfo()+"'"); } @Test(dependsOnMethods="TestEditPartner", priority=5) public void TestReorderPartners(){ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_PartnersPage be_partnersPage = new BackEnd_PartnersPage(driver); ListtitleOrder = be_partnersPage.getTableTitlesByOrder(); BackEnd_PartnersReorderPage be_partnersReorderPage = be_partnersPage.openReorderPage(); String firstTitle = titleOrder.get(0); be_partnersPage = be_partnersReorderPage.moveElementToBottom(firstTitle); assertTrue(be_partnersPage.getAlertInfo().contains(reorderedAlert), "Expected '"+reorderedAlert+"' to appear on page, but found: '"+be_partnersPage.getAlertInfo()+"'"); titleOrder = be_partnersPage.getTableTitlesByOrder(); assertEquals(titleOrder.get(titleOrder.size() - 1), firstTitle); driver.get(settings.http + settings.adminUrl + moduleBackEndPath); be_partnersPage = new BackEnd_PartnersPage(driver); titleOrder = be_partnersPage.getTableTitlesByOrder(); be_partnersReorderPage = be_partnersPage.openReorderPage(); String lastTitle = titleOrder.get(titleOrder.size() - 1); be_partnersPage = be_partnersReorderPage.moveElementToTop(lastTitle); assertTrue(be_partnersPage.getAlertInfo().contains(reorderedAlert), "Expected '"+reorderedAlert+"' to appear on page, but found: '"+be_partnersPage.getAlertInfo()+"'"); titleOrder = be_partnersPage.getTableTitlesByOrder(); assertEquals(titleOrder.get(0), lastTitle); } @Test(dataProvider = "Incorrect_Data", dependsOnMethods="TestEditPartner", priority=5, enabled=true) public void TestIncorrectEditPartner(Map> fieldsData, Map> expectedErrors) throws InterruptedException{ if(fieldsData.get("image").get("value")==""){ throw new SkipException("It is unable to test empty Image field during editing"); } driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_PartnersPage be_partnersPage = new BackEnd_PartnersPage(driver); be_partnersPage.doSearch(searchText); BackEnd_PartnerEditPage be_partnerEditPage = be_partnersPage.editFirstRecordFromTable(); Map> clearData = dp.GenerateClearForm(); be_partnerEditPage.clearForm(clearData); be_partnerEditPage = be_partnerEditPage.submitIncorrectData(fieldsData); Map>realFormErrors = be_partnerEditPage.getFieldsAlerts(); compareFormErrors(realFormErrors, expectedErrors); } @Test(dataProvider = "CleanUp_Data", dependsOnMethods="TestEditPartner", priority=6) public void cleanUpPartners(Map> fields){ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_PartnersPage be_partnersPage = new BackEnd_PartnersPage(driver); searchText = fields.get("title").get("value"); be_partnersPage.doSearch(searchText); try { be_partnersPage.deleteFirstRecordFromTable(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } assertTrue(be_partnersPage.getAlertInfo().contains(deletedAlert), "Expected '"+deletedAlert+"' to appear on page, but found: '"+be_partnersPage.getAlertInfo()+"'"); } @Test(priority=7) public void testCancelButton(){ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_PartnersPage be_partnersPage = new BackEnd_PartnersPage(driver); BackEnd_PartnerAddPage be_partnerAddPage = be_partnersPage.openAddNewForm(); be_partnersPage = be_partnerAddPage.clickCancel(); be_partnersPage.doSearch(searchText); driver.findElement(By.id("btnCancel")).click(); be_partnersPage = new BackEnd_PartnersPage(driver); BackEnd_PartnersReorderPage be_partnersReorderPage = be_partnersPage.openReorderPage(); be_partnersPage = be_partnersReorderPage.clickCancel(); } @AfterClass public void tearDown(){ driver.quit(); //driver.close(); } }