package abstract_classes; import java.util.List; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.support.ui.Wait; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import tools.Settings; abstract class BasePage extends Settings { protected WebDriver driver; protected By form; protected String formId; public BasePage(WebDriver driver) { this.driver = driver; } public By searchInput = By.id("query"); public By searchBtn = By.id("btnSearch"); public By searchCancelBtn = By.id("btnCancel"); public void doSearch(String query){ driver.findElement(searchInput).clear(); driver.findElement(searchInput).sendKeys(query); driver.findElement(searchBtn).click(); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void cancelSearch(String query){ driver.findElement(searchCancelBtn).click(); } public void mouseOver(By locator){ WebElement element = driver.findElement(locator); Actions builder = new Actions(driver); Actions hoverOverRegistrar = builder.moveToElement(element); hoverOverRegistrar.perform(); } protected boolean isElementPresent(By selector){ if(driver.findElements(selector).size() != 0){ return true; } else{ return false; } } protected String getALertText() throws InterruptedException{ //TimeOut to resolve Alert not found exception Thread.sleep(1000); WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.alertIsPresent()); Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); alert.accept(); return alertText; } public void waitForPageLoaded() { try { Thread.sleep(700); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } ExpectedCondition expectation = new ExpectedCondition() { public Boolean apply(WebDriver driver) { return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete"); } }; Wait wait = new WebDriverWait(driver,30); try { wait.until(expectation); } catch(Throwable error) { Assert.assertFalse(true,"Timeout waiting for Page Load Request to complete."); } } public void makeElementVisibleByJavascript(final WebElement element) { String script = "var element = arguments[0];" + "element.style.display='block';" ; ((JavascriptExecutor)driver).executeScript(script, element); } public void inputFormFieldValue(String fieldName, String value, String type){ switch (type) { case "text": { driver.findElement(form).findElement(By.name(fieldName)).clear(); driver.findElement(form).findElement(By.name(fieldName)).sendKeys(value); } break; case "textarea": { driver.findElement(form).findElement(By.name(fieldName)).clear(); driver.findElement(form).findElement(By.name(fieldName)).sendKeys(value); } break; case "file": { if(!value.isEmpty()){ driver.findElement(form).findElement(By.name(fieldName)).sendKeys(System.getProperty("user.dir")+"\\testing-file-resources\\"+value); } else{ } } break; case "checkbox": { boolean isChecked = driver.findElement(form).findElement(By.cssSelector("input[name='"+fieldName+"'][type='checkbox']")).isSelected(); if(isChecked){ if(!value.equals("true")){ driver.findElement(form).findElement(By.cssSelector("input[name='"+fieldName+"'][type='checkbox']")).click(); } } else{ if(value.equals("true")){ driver.findElement(form).findElement(By.cssSelector("input[name='"+fieldName+"'][type='checkbox']")).click(); } } } break; case "checkbox-set": { if(value.isEmpty()){ for(WebElement element : driver.findElements(By.cssSelector("input[name='"+fieldName+"'][type='checkbox']"))){ if(element.isSelected()){ element.click(); } } } else{ boolean isChecked = driver.findElement(form).findElement(By.cssSelector("input[name='"+fieldName+"'][type='checkbox'][value='"+value+"']")).isSelected(); if(!isChecked){ driver.findElement(form).findElement(By.cssSelector("input[name='"+fieldName+"'][type='checkbox'][value='"+value+"']")).click(); } else{ } } } break; case "undefined-fck-editor":{ WebElement iframe = driver.findElement(form).findElement(By.xpath("//*[contains(@id,'"+fieldName+"-element')]")).findElement(By.tagName("iframe")); driver.switchTo().frame(iframe); WebElement editable = driver.switchTo().activeElement(); String ctrlA = Keys.chord(Keys.CONTROL, "a"); String delete = Keys.chord(Keys.DELETE); editable.sendKeys(ctrlA + delete + value); driver.switchTo().defaultContent(); } break; case "fck-editor":{ WebElement iframe = driver.findElement(By.cssSelector("#"+fieldName+"-element iframe")); driver.switchTo().frame(iframe); WebElement editable = driver.switchTo().activeElement(); String ctrlA = Keys.chord(Keys.CONTROL, "a"); String delete = Keys.chord(Keys.DELETE); editable.sendKeys(ctrlA + delete + value); driver.switchTo().defaultContent(); } break; case "radio":{ if(driver.findElement(form).findElements(By.cssSelector("input[name='"+fieldName+"'][value='"+value+"']")).size()>0){ driver.findElement(form).findElement(By.cssSelector("input[name='"+fieldName+"'][value='"+value+"']")).click(); } else{ if(value.isEmpty()){ if(isElementPresent(By.cssSelector("input[name='"+fieldName+"'][checked]"))){ String id = driver.findElement(form).findElement(By.cssSelector("input[name='"+fieldName+"'][checked]")).getAttribute("id"); //System.out.print(id); String script = "document.getElementById('"+id+"').removeAttribute('checked')"; ((JavascriptExecutor)driver).executeScript(script); } } else{ driver.findElement(form).findElement(By.cssSelector("input[name='"+fieldName+"'][value='"+value+"']")).click(); } } } break; case "date":{ driver.findElement(form).findElement(By.name(fieldName)).clear(); //TimeOut to resolve problem when data was not entered to date field try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } driver.findElement(form).findElement(By.name(fieldName)).sendKeys(value); } break; case "select":{ Select select = new Select(driver.findElement(form).findElement(By.name(fieldName))); if(value.equals("")){ Boolean hasEmptyValue = false; Listoptions = select.getOptions(); for(WebElement option: options){ if(option.getAttribute("value").equals(value)){ hasEmptyValue = true; } } if(hasEmptyValue){ select.selectByValue(value); } else{ WebElement element = driver.findElement(form).findElement(By.name(fieldName)); String script = "var x = arguments[0];" + "var option=document.createElement('option'); option.text='None'; option.value=''; try { x.add(option,x.options[null]); } catch (e) { x.add(option,null); }"; ((JavascriptExecutor)driver).executeScript(script, element); select.selectByValue(value); } } else{ select.selectByVisibleText(value); } } break; default: { driver.findElement(form).findElement(By.name(fieldName)).clear(); driver.findElement(form).findElement(By.name(fieldName)).sendKeys(value); } } } public String getFormFieldValue(String fieldName, String type){ String value = null; switch (type) { case "text": { value = driver.findElement(form).findElement(By.name(fieldName)).getAttribute("value"); } break; case "textarea": { value = driver.findElement(form).findElement(By.name(fieldName)).getAttribute("value"); } break; case "file": { value = driver.findElement(form).findElement(By.name(fieldName)).findElement(By.xpath("./../a")).getAttribute("href"); } break; case "checkbox": { boolean isChecked = driver.findElement(form).findElement(By.cssSelector("input[name='"+fieldName+"'][type='checkbox']")).isSelected(); if(isChecked){ value = "true"; } else{ value = "false"; } } break; case "checkbox-set": { value = driver.findElement(form).findElement(By.cssSelector("input[name='"+fieldName+"'][type='checkbox'][checked]")).getAttribute("value"); } break; case "fck-editor":{ value = driver.findElement(form).findElement(By.name(fieldName)).getAttribute("value"); } break; case "undefined-fck-editor":{ value = driver.findElement(form).findElement(By.xpath("//*[contains(@name,'"+fieldName+"')]")).getAttribute("value"); } break; case "radio":{ value = driver.findElement(form).findElement(By.cssSelector("input[name='"+fieldName+"'][checked]")).getAttribute("value"); } break; case "date":{ value = driver.findElement(form).findElement(By.name(fieldName)).getAttribute("value"); } break; case "select":{ Select select = new Select(driver.findElement(form).findElement(By.name(fieldName))); if(select.getFirstSelectedOption().getAttribute("value").isEmpty()){ value = select.getFirstSelectedOption().getAttribute("value"); } else{ value = select.getFirstSelectedOption().getText(); } } break; default: { value = driver.findElement(form).findElement(By.name(fieldName)).getAttribute("value"); } } return value; } }