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_TeamAddPage; import qsf_page_object_pattern.BackEnd.BackEnd_TeamEditPage; import qsf_page_object_pattern.BackEnd.BackEnd_TeamPage; import qsf_page_object_pattern.FrontEnd.FrontEnd_TeamItemPage; import tools.DynamicDataProvider; public class be_ourTeam_test extends BaseTest{ final String addedAlert = "Team Member has been added"; final String updatedAlert = "Team Member has been updated"; final String deletedAlert = "Team Member has been deleted"; final String moduleBackEndPath = "site-content/people"; final String moduleFrontEndPath = "company/our-team/"; DynamicDataProvider dp = new DynamicDataProvider("BE_TeamAddPage.json"); private String searchText; public be_ourTeam_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") public void TestValidTeamItem(Map> fields){ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_TeamPage be_teamPage = new BackEnd_TeamPage(driver); BackEnd_TeamAddPage be_teamAddPage = be_teamPage.openAddNewForm(); be_teamPage = be_teamAddPage.addNew(fields); assertTrue(be_teamPage.getAlertInfo().contains(addedAlert), "Expected '"+addedAlert+"' to appear on page, but found: '"+be_teamPage.getAlertInfo()+"'"); searchText = fields.get("name").get("value"); be_teamPage.doSearch(searchText); Map FirstRecordData = be_teamPage.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("jobTitle").get("value").equals(FirstRecordData.get("jobTitle")), "Expected Job Title to be '"+fields.get("jobTitle").get("value")+"', but found '"+FirstRecordData.get("jobTitle")+"'"); assertTrue(FirstRecordData.get("fullBio").contains(fields.get("fullBio").get("value")), "Expected Full Bio '"+fields.get("fullBio").get("value")+"' to be present in '"+FirstRecordData.get("fullBio")+"'. But was not found."); 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_TeamEditPage be_teamEditPage = be_teamPage.editFirstRecordFromTable(); for(Entry> field : fields.entrySet()){ switch (field.getValue().get("type")) { case "fck-editor": { assertTrue(be_teamEditPage.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_teamEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")).contains(fileNameWithOutExt), "File for "+field.getKey()+" doesn't match"); } break; default: assertEquals(be_teamEditPage.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="TestValidTeamItem", priority=2, enabled=true) public void TestIncorrectTeamItem(Map> fieldsData, Map> expectedErrors) throws InterruptedException{ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_TeamPage be_teamPage = new BackEnd_TeamPage(driver); BackEnd_TeamAddPage be_teamAddPage = be_teamPage.openAddNewForm(); be_teamAddPage = be_teamAddPage.submitIncorrectData(fieldsData); Map>realFormErrors = be_teamAddPage.getFieldsAlerts(); compareFormErrors(realFormErrors, expectedErrors); } @Test(dataProvider = "Valid_Data", dependsOnMethods="TestValidTeamItem", priority=1) public void TestFrontEnd(Map> fields){ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_TeamPage be_teamPage = new BackEnd_TeamPage(driver); be_teamPage.doSearch(searchText); Map controls = be_teamPage.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_TeamItemPage fe_teamItemPage = new FrontEnd_TeamItemPage(driver); for(Entry> field : fields.entrySet()){ if(fe_teamItemPage.getFrontEndValue(field.getKey())!=null){ switch (field.getKey()) { case "fullBio":{ assertTrue(fe_teamItemPage.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_teamItemPage.getFrontEndValue(field.getKey()).contains(fileNameWithOutExt), "Value for '"+field.getKey()+"' did not match with Front End"); } break; default:{ assertEquals(fe_teamItemPage.getFrontEndValue(field.getKey()), field.getValue().get("value"), "Value for '"+field.getKey()+"' did not match with Front End"); } } } } } @Test(dataProvider = "Edit_Data", dependsOnMethods="TestValidTeamItem", priority=3, enabled=true) public void TestEditTeamItem(Map> fields){ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_TeamPage be_teamPage = new BackEnd_TeamPage(driver); be_teamPage.doSearch(searchText); BackEnd_TeamEditPage be_teamEditPage = be_teamPage.editFirstRecordFromTable(); be_teamPage = be_teamEditPage.editRecord(fields); assertTrue(be_teamPage.getAlertInfo().contains(updatedAlert), "Expected '"+updatedAlert+"' to appear on page, but found: '"+be_teamPage.getAlertInfo()+"'"); searchText = fields.get("name").get("value"); be_teamPage.doSearch(searchText); Map FirstRecordData = be_teamPage.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("jobTitle").get("value").equals(FirstRecordData.get("jobTitle")), "Expected Job Title to be '"+fields.get("jobTitle").get("value")+"', but found '"+FirstRecordData.get("jobTitle")+"'"); assertTrue(FirstRecordData.get("fullBio").contains(fields.get("fullBio").get("value")), "Expected Full Bio '"+fields.get("fullBio").get("value")+"' to be present in '"+FirstRecordData.get("fullBio")+"'. But was not found."); 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_teamEditPage = be_teamPage.editFirstRecordFromTable(); for(Entry> field : fields.entrySet()){ switch (field.getValue().get("type")) { case "fck-editor": { assertTrue(be_teamEditPage.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_teamEditPage.getFormFieldValue(field.getKey(), field.getValue().get("type")).contains(fileNameWithOutExt), "File for "+field.getKey()+" doesn't match"); } break; default: assertEquals(be_teamEditPage.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="TestEditTeamItem", priority=4, enabled=true) public void TestDisabledTeamItemFrontEnd(Map> fields){ if(!fields.get("enabled").get("value").equals("true")){ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_TeamPage be_teamPage = new BackEnd_TeamPage(driver); be_teamPage.doSearch(searchText); Map controls = be_teamPage.getFirstRecordControlsFromTable(); Map> params = getQueryParams(controls.get("edit").getAttribute("href")); String itemId = params.get("id").get(0); driver.get(settings.http + settings.siteUrl + "company/our-team/"+itemId); waitForPageLoaded(); assertTrue(driver.findElement(By.tagName("body")).getText().contains("404 - Not Found"), "This is not 404 page"); } else{ throw new SkipException("Team item was not disabled during editing"); } } @Test(dataProvider = "Incorrect_Data", dependsOnMethods="TestEditTeamItem", priority=5, enabled=true) public void TestIncorrectEditTeamItem(Map> fieldsData, Map> expectedErrors) throws InterruptedException{ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_TeamPage be_teamPage = new BackEnd_TeamPage(driver); be_teamPage.doSearch(searchText); BackEnd_TeamEditPage be_teamEditPage = be_teamPage.editFirstRecordFromTable(); Map> clearData = dp.GenerateClearForm(); be_teamEditPage.clearForm(clearData); be_teamEditPage = be_teamEditPage.submitIncorrectData(fieldsData); Map>realFormErrors = be_teamEditPage.getFieldsAlerts(); compareFormErrors(realFormErrors, expectedErrors); } @Test(dataProvider = "CleanUp_Data", dependsOnMethods="TestEditTeamItem", priority=6) public void cleanUpTeam(Map> fields){ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_TeamPage be_teamPage = new BackEnd_TeamPage(driver); searchText = fields.get("name").get("value"); be_teamPage.doSearch(searchText); try { be_teamPage.deleteFirstRecordFromTable(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } assertTrue(be_teamPage.getAlertInfo().contains(deletedAlert), "Expected '"+deletedAlert+"' to appear on page, but found: '"+be_teamPage.getAlertInfo()+"'"); } @Test(priority=7) public void testCancelButton(){ driver.get(settings.http + settings.adminUrl + moduleBackEndPath); BackEnd_TeamPage be_teamPage = new BackEnd_TeamPage(driver); BackEnd_TeamAddPage be_teamAddPage = be_teamPage.openAddNewForm(); be_teamPage = be_teamAddPage.clickCancel(); be_teamPage.doSearch(searchText); driver.findElement(By.id("btnCancel")).click(); be_teamPage = new BackEnd_TeamPage(driver); } @AfterClass public void tearDown(){ driver.quit(); //driver.close(); } }