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.openqa.selenium.By; import org.openqa.selenium.WebElement; 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_NewsAddPage; import qsf_page_object_pattern.BackEnd.BackEnd_NewsEditPage; import qsf_page_object_pattern.BackEnd.BackEnd_NewsPage; import qsf_page_object_pattern.FrontEnd.FrontEnd_NewsItemPage; import tools.DynamicDataProvider; import tools.Strtotime; public class be_news_test extends BaseTest{ final String addedAlert = "News Item has been added"; final String updatedAlert = "News Item has been updated"; final String deletedAlert = "News Item has been deleted"; final String modulePath = "news/recent-news"; final String moduleFrontEndPath = "news-1/recent-news/"; DynamicDataProvider dp = new DynamicDataProvider("BE_NewsAddPage.json"); private String searchText; public be_news_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 TestValidNewsItem(Map> fields){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_NewsPage be_newsPage = new BackEnd_NewsPage(driver); BackEnd_NewsAddPage be_newsAddPage = be_newsPage.openAddNewForm(); be_newsPage = be_newsAddPage.addNew(fields); assertTrue(be_newsPage.getAlertInfo().contains(addedAlert), "Expected '"+addedAlert+"' to appear on page, but found: '"+be_newsPage.getAlertInfo()+"'"); searchText = fields.get("title").get("value"); be_newsPage.doSearch(searchText); Map FirstRecordData = be_newsPage.getFirstRecordDataFromTable(); assertTrue(fields.get("_date").get("value").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")+"'"); assertTrue(FirstRecordData.get("intro").contains(fields.get("introduction").get("value")), "Expected content '"+fields.get("introduction").get("value")+"' to be present in '"+FirstRecordData.get("intro")+"'. But was not found."); BackEnd_NewsEditPage be_newsEditPage = be_newsPage.editFirstRecordFromTable(); for(Entry> field : fields.entrySet()){ switch (field.getValue().get("type")) { case "fck-editor": { assertTrue(be_newsEditPage.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_newsEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")).contains(fileNameWithOutExt), "File for "+field.getKey()+" doesn't match"); } break; default: assertEquals(be_newsEditPage.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="TestValidNewsItem", priority=2, enabled=true, description = "") public void TestIncorrectNewsItem(Map> fieldsData, Map> expectedErrors) throws InterruptedException{ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_NewsPage be_newsPage = new BackEnd_NewsPage(driver); BackEnd_NewsAddPage be_newsAddPage = be_newsPage.openAddNewForm(); be_newsAddPage = be_newsAddPage.submitIncorrectData(fieldsData); Map>realFormErrors = be_newsAddPage.getFieldsAlerts(); compareFormErrors(realFormErrors, expectedErrors); } @Test(dataProvider = "Valid_Data", dependsOnMethods="TestValidNewsItem", priority=1, description = "") public void TestFrontEnd(Map> fields){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_NewsPage be_newsPage = new BackEnd_NewsPage(driver); be_newsPage.doSearch(searchText); Map controls = be_newsPage.getFirstRecordControlsFromTable(); Map> params = getQueryParams(controls.get("edit").getAttribute("href")); String itemId = params.get("id").get(0); driver.get(settings.http + settings.siteUrl + moduleFrontEndPath + itemId); FrontEnd_NewsItemPage fe_newsItemPage = new FrontEnd_NewsItemPage(driver); for(Entry> field : fields.entrySet()){ if(fe_newsItemPage.getFrontEndValue(field.getKey())!=null){ switch (field.getKey()) { case "content":{ assertTrue(fe_newsItemPage.getFrontEndValue(field.getKey()).contains(field.getValue().get("value")), "Value for '"+field.getKey()+"' did not match with Front End"); } break; case "image":{ String fileNameWithOutExt = FilenameUtils.removeExtension(field.getValue().get("value")); assertTrue(fe_newsItemPage.getFrontEndValue(field.getKey()).contains(fileNameWithOutExt), "Value for '"+field.getKey()+"' did not match with Front End"); } break; case "file":{ String fileNameWithOutExt = FilenameUtils.removeExtension(field.getValue().get("value")); assertTrue(fe_newsItemPage.getFrontEndValue(field.getKey()).contains(fileNameWithOutExt), "Value for '"+field.getKey()+"' did not match with Front End"); } break; case "_date":{ assertEquals(Strtotime.strtotime(fe_newsItemPage.getFrontEndValue(field.getKey())), Strtotime.strtotime(field.getValue().get("value")), "Value for '"+field.getKey()+"' did not match with Front End"); } break; case "cms":{ assertTrue(!fe_newsItemPage.getFrontEndValue(field.getKey()).isEmpty(), "CMS link not foun on frontend"); } break; default:{ assertEquals(fe_newsItemPage.getFrontEndValue(field.getKey()), field.getValue().get("value"), "Value for '"+field.getKey()+"' did not match with Front End"); } } } } } @Test(dataProvider = "Edit_Data", dependsOnMethods="TestValidNewsItem", priority=3, enabled=true, description = "") public void TestEditNewsItem(Map> fields){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_NewsPage be_newsPage = new BackEnd_NewsPage(driver); be_newsPage.doSearch(searchText); BackEnd_NewsEditPage be_newsEditPage = be_newsPage.editFirstRecordFromTable(); be_newsPage = be_newsEditPage.editRecord(fields); assertTrue(be_newsPage.getAlertInfo().contains(updatedAlert), "Expected '"+updatedAlert+"' to appear on page, but found: '"+be_newsPage.getAlertInfo()+"'"); searchText = fields.get("title").get("value"); be_newsPage.doSearch(searchText); Map FirstRecordData = be_newsPage.getFirstRecordDataFromTable(); assertTrue(fields.get("_date").get("value").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")+"'"); assertTrue(FirstRecordData.get("intro").contains(fields.get("introduction").get("value")), "Expected content '"+fields.get("introduction").get("value")+"' to be present in '"+FirstRecordData.get("intro")+"'. But was not found."); be_newsEditPage = be_newsPage.editFirstRecordFromTable(); for(Entry> field : fields.entrySet()){ switch (field.getValue().get("type")) { case "fck-editor": { assertTrue(be_newsEditPage.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_newsEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")).contains(fileNameWithOutExt), "File for "+field.getKey()+" doesn't match"); } break; default: assertEquals(be_newsEditPage.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="TestEditNewsItem", priority=4, enabled=true, description="") public void TestDisabledNewsItemFrontEnd(Map> fields){ if(!fields.get("enabled").get("value").equals("true")){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_NewsPage be_newsPage = new BackEnd_NewsPage(driver); be_newsPage.doSearch(searchText); Map controls = be_newsPage.getFirstRecordControlsFromTable(); Map> params = getQueryParams(controls.get("edit").getAttribute("href")); String itemId = params.get("id").get(0); driver.get(settings.http + settings.siteUrl + moduleFrontEndPath + itemId); waitForPageLoaded(); assertTrue(driver.findElement(By.tagName("body")).getText().contains("404 - Not Found"), "This is not 404 page"); } else{ throw new SkipException("News item was not disabled during editing"); } } @Test(dataProvider = "Incorrect_Data", dependsOnMethods="TestEditNewsItem", priority=5, enabled=true, description = "") public void TestIncorrectEditNewsItem(Map> fieldsData, Map> expectedErrors) throws InterruptedException{ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_NewsPage be_newsPage = new BackEnd_NewsPage(driver); be_newsPage.doSearch(searchText); BackEnd_NewsEditPage be_newsEditPage = be_newsPage.editFirstRecordFromTable(); Map> clearData = dp.GenerateClearForm(); clearData.remove("linkType"); be_newsEditPage.clearForm(clearData); be_newsEditPage = be_newsEditPage.submitIncorrectData(fieldsData); Map>realFormErrors = be_newsEditPage.getFieldsAlerts(); compareFormErrors(realFormErrors, expectedErrors); } @Test(dataProvider = "CleanUp_Data", dependsOnMethods="TestEditNewsItem", priority=6, description = "") public void cleanUpNews(Map> fields){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_NewsPage be_newsPage = new BackEnd_NewsPage(driver); searchText = fields.get("title").get("value"); be_newsPage.doSearch(searchText); try { be_newsPage.deleteFirstRecordFromTable(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } assertTrue(be_newsPage.getAlertInfo().contains(deletedAlert), "Expected '"+deletedAlert+"' to appear on page, but found: '"+be_newsPage.getAlertInfo()+"'"); } @Test(priority=7, description = "") public void testCancelButton(){ driver.get(settings.http + settings.adminUrl + modulePath); BackEnd_NewsPage be_newsPage = new BackEnd_NewsPage(driver); BackEnd_NewsAddPage be_newsAddPage = be_newsPage.openAddNewForm(); be_newsPage = be_newsAddPage.clickCancel(); be_newsPage.doSearch(searchText); driver.findElement(By.id("btnCancel")).click(); be_newsPage = new BackEnd_NewsPage(driver); } @AfterClass public void tearDown(){ driver.quit(); //driver.close(); } }