package test_classes; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.NoSuchElementException; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FilenameUtils; 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_NewsletterAddPage; import qsf_page_object_pattern.BackEnd.BackEnd_NewsletterEditPage; import qsf_page_object_pattern.BackEnd.BackEnd_NewslettersPage; import qsf_page_object_pattern.FrontEnd.FrontEnd_NewslettersPage; import tools.DynamicDataProvider; import tools.Strtotime; public class be_newsletter_test extends BaseTest{ final String addedAlert = "Newsletter has been added"; final String updatedAlert = "Newsletter has been updated"; final String deletedAlert = "Newsletter has been deleted"; final String modulePath = "news/newsletter"; final String moduleFrontEndPath = "news-1/newsletters/"; DynamicDataProvider dp = new DynamicDataProvider("BE_Newsletters.json"); private String searchText; public be_newsletter_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.GenerateFullCheck(), 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", description = "") public void TestValidNewsletter(Map> fields){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_NewslettersPage be_newslettersPage = new BackEnd_NewslettersPage(driver); BackEnd_NewsletterAddPage be_newsletterAddPage = be_newslettersPage.openAddNewForm(); be_newslettersPage = be_newsletterAddPage.addNew(fields); assertTrue(be_newslettersPage.getAlertInfo().contains(addedAlert), "Expected '"+addedAlert+"' to appear on page, but found: '"+be_newslettersPage.getAlertInfo()+"'"); searchText = fields.get("title").get("value"); be_newslettersPage.doSearch(searchText); Map FirstRecordData = be_newslettersPage.getFirstRecordDataFromTable(); DateFormat df = new SimpleDateFormat("MMM d, yyyy"); String reportDate = df.format(Strtotime.strtotime(fields.get("_date").get("value"))); assertTrue(reportDate.equals(FirstRecordData.get("date")), "Expected first date to be '"+reportDate+"', but found '"+FirstRecordData.get("date")+"'"); 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")+"'"); BackEnd_NewsletterEditPage be_newsletterEditPage = be_newslettersPage.editFirstRecordFromTable(); for(Entry> field : fields.entrySet()){ switch (field.getValue().get("type")) { case "fck-editor": { assertTrue(be_newsletterEditPage.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": { String fileNameWithOutExt = FilenameUtils.removeExtension(field.getValue().get("value")); assertTrue(be_newsletterEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")).contains(fileNameWithOutExt), "File for "+field.getKey()+" doesn't match"); } break; default: assertEquals(be_newsletterEditPage.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="TestValidNewsletter", priority=2, enabled=true, description = "") public void TestIncorrectNewsletter(Map> fieldsData, Map> expectedErrors) throws InterruptedException{ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_NewslettersPage be_newslettersPage = new BackEnd_NewslettersPage(driver); BackEnd_NewsletterAddPage be_newsAddPage = be_newslettersPage.openAddNewForm(); be_newsAddPage = be_newsAddPage.submitIncorrectData(fieldsData); Map>realFormErrors = be_newsAddPage.getFieldsAlerts(); compareFormErrors(realFormErrors, expectedErrors); } @Test(dataProvider = "Valid_Data", dependsOnMethods="TestValidNewsletter", priority=1, description = "") public void TestFrontEnd(Map> fields){ Date date = Strtotime.strtotime(fields.get("_date").get("value")); Calendar cal = Calendar.getInstance(); cal.setTime(date); String year = Integer.toString(cal.get(Calendar.YEAR)); driver.get(settings.http + settings.siteUrl + moduleFrontEndPath + year); FrontEnd_NewslettersPage fe_newslettersPage = new FrontEnd_NewslettersPage(driver); List> recordsData = fe_newslettersPage.getRecords(); Boolean recordFound = false; for(Map recordData : recordsData){ if(recordData.get("title").equals(fields.get("title").get("value"))){ DateFormat df = new SimpleDateFormat("MMM d, yyyy"); String reportDate = df.format(date); assertTrue(recordData.get("_date").contains(reportDate), "Expected date to be '"+reportDate+"' but found +'"+recordData.get("_date")+"'"); String fileNameWithOutExt = FilenameUtils.removeExtension(fields.get("file").get("value")); assertTrue(recordData.get("file").contains(fileNameWithOutExt), "Expected file to be '"+fileNameWithOutExt+"' but found +'"+recordData.get("file")+"'"); recordFound = true; } } if(!recordFound){ throw new NoSuchElementException("Newsletter was not found on Front End"); } } @Test(dataProvider = "Edit_Data", dependsOnMethods="TestValidNewsletter", priority=3, enabled=true, description = "") public void TestEditNewsletter(Map> fields){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_NewslettersPage be_newsletterPage = new BackEnd_NewslettersPage(driver); be_newsletterPage.doSearch(searchText); BackEnd_NewsletterEditPage be_newsletterEditPage = be_newsletterPage.editFirstRecordFromTable(); be_newsletterPage = be_newsletterEditPage.editRecord(fields); assertTrue(be_newsletterPage.getAlertInfo().contains(updatedAlert), "Expected '"+updatedAlert+"' to appear on page, but found: '"+be_newsletterPage.getAlertInfo()+"'"); searchText = fields.get("title").get("value"); be_newsletterPage.doSearch(searchText); Map FirstRecordData = be_newsletterPage.getFirstRecordDataFromTable(); DateFormat df = new SimpleDateFormat("MMM d, yyyy"); String reportDate = df.format(Strtotime.strtotime(fields.get("_date").get("value"))); assertTrue(reportDate.equals(FirstRecordData.get("date")), "Expected first date to be '"+fields.get("_date").get("value")+"', but found '"+FirstRecordData.get("date")+"'"); 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")+"'"); be_newsletterEditPage = be_newsletterPage.editFirstRecordFromTable(); for(Entry> field : fields.entrySet()){ switch (field.getValue().get("type")) { case "fck-editor": { assertTrue(be_newsletterEditPage.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": { String fileNameWithOutExt = FilenameUtils.removeExtension(field.getValue().get("value")); assertTrue(be_newsletterEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")).contains(fileNameWithOutExt), "File for "+field.getKey()+" doesn't match"); } break; default: assertEquals(be_newsletterEditPage.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 = "Edit_Data", dependsOnMethods="TestEditNewsletter", priority=4, enabled=true, description="") public void TestDisabledNewsletterFrontEnd(Map> fields){ Date date = Strtotime.strtotime(fields.get("_date").get("value")); Calendar cal = Calendar.getInstance(); cal.setTime(date); String year = Integer.toString(cal.get(Calendar.YEAR)); driver.get(settings.http + settings.siteUrl + moduleFrontEndPath + year); FrontEnd_NewslettersPage fe_newslettersPage = new FrontEnd_NewslettersPage(driver); List> recordsData = fe_newslettersPage.getRecords(); Boolean recordFound = false; for(Map recordData : recordsData){ if(recordData.get("title").equals(fields.get("title").get("value"))){ recordFound = true; } } assertFalse(recordFound, "Disabled Record is available on FrontEnd"); } @Test(dataProvider = "Incorrect_Data", dependsOnMethods="TestEditNewsletter", priority=5, enabled=true, description = "") public void TestIncorrectEditNewsletter(Map> fieldsData, Map> expectedErrors) throws InterruptedException{ if(fieldsData.get("file").get("value")==""){ throw new SkipException("It is unable to test empty file field during editing"); } driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_NewslettersPage be_newslettersPage = new BackEnd_NewslettersPage(driver); be_newslettersPage.doSearch(searchText); BackEnd_NewsletterEditPage be_newsletterEditPage = be_newslettersPage.editFirstRecordFromTable(); Map> clearData = dp.GenerateClearForm(); clearData.remove("linkType"); be_newsletterEditPage.clearForm(clearData); be_newsletterEditPage = be_newsletterEditPage.submitIncorrectData(fieldsData); Map>realFormErrors = be_newsletterEditPage.getFieldsAlerts(); compareFormErrors(realFormErrors, expectedErrors); } @Test(dataProvider = "CleanUp_Data", dependsOnMethods="TestEditNewsletter", priority=6, description = "") public void cleanUpNews(Map> fields){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_NewslettersPage be_newslettersPage = new BackEnd_NewslettersPage(driver); searchText = fields.get("title").get("value"); be_newslettersPage.doSearch(searchText); try { be_newslettersPage.deleteFirstRecordFromTable(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } assertTrue(be_newslettersPage.getAlertInfo().contains(deletedAlert), "Expected '"+deletedAlert+"' to appear on page, but found: '"+be_newslettersPage.getAlertInfo()+"'"); } @Test(priority=7, description = "") public void testCancelButton(){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_NewslettersPage be_newslettersPage = new BackEnd_NewslettersPage(driver); BackEnd_NewsletterAddPage be_newsletterAddPage = be_newslettersPage.openAddNewForm(); be_newslettersPage = be_newsletterAddPage.clickCancel(); be_newslettersPage.doSearch(searchText); driver.findElement(By.id("btnCancel")).click(); be_newslettersPage = new BackEnd_NewslettersPage(driver); } @AfterClass public void tearDown(){ driver.quit(); //driver.close(); } }