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.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_TestimonialAddPage; import qsf_page_object_pattern.BackEnd.BackEnd_TestimonialEditPage; import qsf_page_object_pattern.BackEnd.BackEnd_TestimonialsPage; import qsf_page_object_pattern.BackEnd.BackEnd_TestimonialsReorderPage; import qsf_page_object_pattern.FrontEnd.FrontEnd_TestimonialsPage; import tools.DynamicDataProvider; public class be_testimonials_test extends BaseTest{ final String addedAlert = "Testimonial has been added"; final String updatedAlert = "Testimonial has been updated"; final String deletedAlert = "Testimonial has been deleted"; final String reorderedAlert = "Testimonials have been reordered"; final String moduleBackEndPath = "site-content/testimonial"; final String moduleFrontEndPath = "company/testimonials/"; DynamicDataProvider dp = new DynamicDataProvider("BE_Testimonials.json"); private String searchText; public be_testimonials_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 TestValidTestimonial(Map> fields){ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_TestimonialsPage be_testimonialsPage = new BackEnd_TestimonialsPage(driver); BackEnd_TestimonialAddPage be_testimonialAddPage = be_testimonialsPage.openAddNewForm(); be_testimonialsPage = be_testimonialAddPage.addNew(fields); assertTrue(be_testimonialsPage.getAlertInfo().contains(addedAlert), "Expected '"+addedAlert+"' to appear on page, but found: '"+be_testimonialsPage.getAlertInfo()+"'"); searchText = fields.get("name").get("value"); be_testimonialsPage.doSearch(searchText); Map FirstRecordData = be_testimonialsPage.getFirstRecordDataFromTable(); assertTrue(fields.get("name").get("value").equals(FirstRecordData.get("name")), "Expected Name to be '"+fields.get("name").get("value")+"', but found '"+FirstRecordData.get("name")+"'"); assertTrue(fields.get("testimonial").get("value").equals(FirstRecordData.get("testimonial")), "Expected Url to be '"+fields.get("testimonial").get("value")+"', but found '"+FirstRecordData.get("testimonial")+"'"); BackEnd_TestimonialEditPage be_testimonialEditPage = be_testimonialsPage.editFirstRecordFromTable(); for(Entry> field : fields.entrySet()){ switch (field.getValue().get("type")) { case "fck-editor": { assertTrue(be_testimonialEditPage.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; default: assertEquals(be_testimonialEditPage.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="TestValidTestimonial", priority=2, enabled=true) public void TestIncorrectTestimonial(Map> fieldsData, Map> expectedErrors) throws InterruptedException{ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_TestimonialsPage be_testimonialsPage = new BackEnd_TestimonialsPage(driver); BackEnd_TestimonialAddPage be_testimonialAddPage = be_testimonialsPage.openAddNewForm(); be_testimonialAddPage = be_testimonialAddPage.submitIncorrectData(fieldsData); Map>realFormErrors = be_testimonialAddPage.getFieldsAlerts(); compareFormErrors(realFormErrors, expectedErrors); } @Test(dataProvider = "Valid_Data", dependsOnMethods="TestValidTestimonial", priority=1) public void TestFrontEnd(Map> fields){ driver.get(settings.http + settings.siteUrl + moduleFrontEndPath); FrontEnd_TestimonialsPage fe_testimonialsPage = new FrontEnd_TestimonialsPage(driver); for(Entry> field : fields.entrySet()){ if(fe_testimonialsPage.getFrontEndValue(field.getKey())!=null){ switch (field.getKey()) { case "testimonial":{ assertEquals(fe_testimonialsPage.getFrontEndValue(field.getKey()).replace("\"", ""), field.getValue().get("value"), "Value for '"+field.getKey()+"' did not match with Front End"); } break; case "jobTitle":{ assertTrue(fe_testimonialsPage.getFrontEndValue(field.getKey()).contains(field.getValue().get("value")), "Value for '"+field.getKey()+"' did not match with Front End"); } break; case "company":{ assertTrue(fe_testimonialsPage.getFrontEndValue(field.getKey()).contains(field.getValue().get("value")), "Value for '"+field.getKey()+"' did not match with Front End"); } break; default:{ assertEquals(fe_testimonialsPage.getFrontEndValue(field.getKey()), field.getValue().get("value"), "Value for '"+field.getKey()+"' did not match with Front End"); } } } } } @Test(dataProvider = "Edit_Data", dependsOnMethods="TestValidTestimonial", priority=3, enabled=true) public void TestEditTestimonial(Map> fields){ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_TestimonialsPage be_testimonialssPage = new BackEnd_TestimonialsPage(driver); be_testimonialssPage.doSearch(searchText); BackEnd_TestimonialEditPage be_testimonialsEditPage = be_testimonialssPage.editFirstRecordFromTable(); be_testimonialssPage = be_testimonialsEditPage.editRecord(fields); assertTrue(be_testimonialssPage.getAlertInfo().contains(updatedAlert), "Expected '"+updatedAlert+"' to appear on page, but found: '"+be_testimonialssPage.getAlertInfo()+"'"); searchText = fields.get("name").get("value"); be_testimonialssPage.doSearch(searchText); Map FirstRecordData = be_testimonialssPage.getFirstRecordDataFromTable(); assertTrue(fields.get("name").get("value").equals(FirstRecordData.get("name")), "Expected Name to be '"+fields.get("name").get("value")+"', but found '"+FirstRecordData.get("name")+"'"); assertTrue(fields.get("testimonial").get("value").equals(FirstRecordData.get("testimonial")), "Expected Url to be '"+fields.get("testimonial").get("value")+"', but found '"+FirstRecordData.get("testimonial")+"'"); be_testimonialsEditPage = be_testimonialssPage.editFirstRecordFromTable(); for(Entry> field : fields.entrySet()){ switch (field.getValue().get("type")) { case "fck-editor": { assertTrue(be_testimonialsEditPage.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; default: assertEquals(be_testimonialsEditPage.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="TestEditTestimonial", priority=4, enabled=true) public void TestDisabledTestimonialFrontEnd(Map> fields){ fields.get("show").put("value", "false"); driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_TestimonialsPage be_testimonialsPage = new BackEnd_TestimonialsPage(driver); BackEnd_TestimonialAddPage be_TestimonialAddPage = be_testimonialsPage.openAddNewForm(); be_testimonialsPage = be_TestimonialAddPage.addNew(fields); assertTrue(be_testimonialsPage.getAlertInfo().contains(addedAlert), "Expected '"+addedAlert+"' to appear on page, but found: '"+be_testimonialsPage.getAlertInfo()+"'"); driver.get(settings.http + settings.siteUrl + moduleFrontEndPath); FrontEnd_TestimonialsPage fe_testimonialsPage = new FrontEnd_TestimonialsPage(driver); for(Entry> field : fields.entrySet()){ if(field.getKey()!="url" && fe_testimonialsPage.getFrontEndValue(field.getKey())!=null){ switch (field.getKey()) { case "image":{ String fileNameWithOutExt = FilenameUtils.removeExtension(field.getValue().get("value")); assertFalse(fe_testimonialsPage.getFrontEndValue(field.getKey()).contains(fileNameWithOutExt)); } break; default:{ assertNotEquals(fe_testimonialsPage.getFrontEndValue(field.getKey()), field.getValue().get("value")); } } } } fields.get("show").put("value", "true"); driver.get(settings.http + settings.adminUrl + moduleBackEndPath); be_testimonialsPage = new BackEnd_TestimonialsPage(driver); be_testimonialsPage.doSearch(fields.get("name").get("value")); BackEnd_TestimonialEditPage be_testimonialEditPage = be_testimonialsPage.editFirstRecordFromTable(); be_testimonialsPage = be_testimonialEditPage.editRecord(fields); assertTrue(be_testimonialsPage.getAlertInfo().contains(updatedAlert), "Expected '"+updatedAlert+"' to appear on page, but found: '"+be_testimonialsPage.getAlertInfo()+"'"); } @Test(dependsOnMethods="TestEditTestimonial", priority=5) public void TestReorderTestimonials(){ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_TestimonialsPage be_testimonialsPage = new BackEnd_TestimonialsPage(driver); be_testimonialsPage.doSearch("selenium"); ListtitleOrder = be_testimonialsPage.getTableTitlesByOrder(); BackEnd_TestimonialsReorderPage be_testimonialsReorderPage = be_testimonialsPage.openReorderPage(); String firstTitle = titleOrder.get(0); be_testimonialsPage = be_testimonialsReorderPage.moveElementToBottom(firstTitle); assertTrue(be_testimonialsPage.getAlertInfo().contains(reorderedAlert), "Expected '"+reorderedAlert+"' to appear on page, but found: '"+be_testimonialsPage.getAlertInfo()+"'"); titleOrder = be_testimonialsPage.getTableTitlesByOrder(); assertEquals(titleOrder.get(titleOrder.size() - 1), firstTitle); driver.get(settings.http + settings.adminUrl + moduleBackEndPath); be_testimonialsPage = new BackEnd_TestimonialsPage(driver); be_testimonialsPage.doSearch("selenium"); titleOrder = be_testimonialsPage.getTableTitlesByOrder(); be_testimonialsReorderPage = be_testimonialsPage.openReorderPage(); String lastTitle = titleOrder.get(titleOrder.size() - 1); be_testimonialsPage = be_testimonialsReorderPage.moveElementToTop(lastTitle); assertTrue(be_testimonialsPage.getAlertInfo().contains(reorderedAlert), "Expected '"+reorderedAlert+"' to appear on page, but found: '"+be_testimonialsPage.getAlertInfo()+"'"); titleOrder = be_testimonialsPage.getTableTitlesByOrder(); assertEquals(titleOrder.get(0), lastTitle); } @Test(dataProvider = "Incorrect_Data", dependsOnMethods="TestEditTestimonial", priority=5, enabled=true) public void TestIncorrectEditTestimonial(Map> fieldsData, Map> expectedErrors) throws InterruptedException{ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_TestimonialsPage be_testimonialsPage = new BackEnd_TestimonialsPage(driver); be_testimonialsPage.doSearch(searchText); BackEnd_TestimonialEditPage be_testimonialsEditPage = be_testimonialsPage.editFirstRecordFromTable(); Map> clearData = dp.GenerateClearForm(); be_testimonialsEditPage.clearForm(clearData); be_testimonialsEditPage = be_testimonialsEditPage.submitIncorrectData(fieldsData); Map>realFormErrors = be_testimonialsEditPage.getFieldsAlerts(); compareFormErrors(realFormErrors, expectedErrors); } @Test(dataProvider = "CleanUp_Data", dependsOnMethods="TestEditTestimonial", priority=6) public void cleanUpTestimonials(Map> fields){ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_TestimonialsPage be_testimonialsPage = new BackEnd_TestimonialsPage(driver); searchText = fields.get("name").get("value"); be_testimonialsPage.doSearch(searchText); try { be_testimonialsPage.deleteFirstRecordFromTable(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } assertTrue(be_testimonialsPage.getAlertInfo().contains(deletedAlert), "Expected '"+deletedAlert+"' to appear on page, but found: '"+be_testimonialsPage.getAlertInfo()+"'"); } @Test(priority=7) public void testCancelButton(){ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_TestimonialsPage be_testimonialsPage = new BackEnd_TestimonialsPage(driver); BackEnd_TestimonialAddPage be_testimonialAddPage = be_testimonialsPage.openAddNewForm(); be_testimonialsPage = be_testimonialAddPage.clickCancel(); be_testimonialsPage.doSearch(searchText); driver.findElement(By.id("btnCancel")).click(); be_testimonialsPage = new BackEnd_TestimonialsPage(driver); BackEnd_TestimonialsReorderPage be_testimonialsReorderPage = be_testimonialsPage.openReorderPage(); be_testimonialsPage = be_testimonialsReorderPage.clickCancel(); } @AfterClass public void tearDown(){ driver.quit(); //driver.close(); } }