Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.accelior.mars.test;
- import com.accelior.mars.test.bo.enumerators.PageUrlEnum;
- import com.accelior.mars.test.utility.ConfigProperties;
- import org.openqa.selenium.*;
- import org.openqa.selenium.support.FindBy;
- import org.openqa.selenium.support.ui.Wait;
- import org.openqa.selenium.support.ui.WebDriverWait;
- import java.lang.reflect.Field;
- import java.util.concurrent.TimeUnit;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public abstract class BasePage extends ContainerWebDriver {
- /** Common class for all pages **/
- private final PageUrlEnum pageUrl;
- public Wait<WebDriver> waitFor = new WebDriverWait(driver, 4, 500)
- .withMessage("Element was not found");
- /**
- * @param pageUrl
- * url of testing page
- **/
- public BasePage(WebDriver driver, PageUrlEnum pageUrl) {
- super(driver);
- this.pageUrl = pageUrl;
- }
- protected void type(WebElement webElement, String text) {
- webElement.clear();
- webElement.sendKeys(text);
- }
- public void open() {
- System.out.println("Open page: " + getPageFullUrl());
- driver.get(getPageFullUrl());
- }
- public < T extends BasePage> T refresh() {
- System.out.println("Refreshing page...");
- driver.navigate().refresh();
- return (T) this;
- }
- public String getPageFullUrl() {
- return pageUrl.getUrl();
- }
- public boolean isAlertPresent() {
- try {
- driver.switchTo().alert();
- return true;
- }
- catch (NoAlertPresentException Ex) {
- return false;
- }
- }
- public boolean isElementVisible(By by) {
- try {
- driver.findElement(by).isDisplayed();
- return true;
- } catch (NoSuchElementException ignored) {
- return false;
- } catch (ElementNotVisibleException ignored) {
- return false;
- } catch (StaleElementReferenceException ignored) {
- return false;
- }
- }
- public boolean isElementVisible(WebElement element) {
- try {
- element.isDisplayed();
- return true;
- } catch (NoSuchElementException ignored) {
- return false;
- } catch (ElementNotVisibleException ignored) {
- return false;
- } catch (StaleElementReferenceException ignored) {
- return false;
- }
- }
- /**
- * @return true if element is absent, and false if element is present
- ** @param locator
- * of element, for example isElementAbsent(By.xpath(
- * "//a[text()='Forgot your password?']")
- **/
- public boolean isElementAbsent(By locator) {
- if (isElementPresent(locator)) {
- System.out.println("False: Element " + locator + " is present");
- return false;
- } else {
- System.out.println("True: Element " + locator + " is absent");
- return true;
- }
- }
- /**
- * @return true if element is present, and false if element is absent
- * @param locator
- * of element, for example isElementPresent(By.xpath(
- * "//a[text()='Forgot your password?']")
- **/
- public boolean isElementPresent(By locator) {
- driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
- boolean result = driver.findElements(locator).size() > 0;
- driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
- return result;
- }
- /** @return true if element is present, and false if element is absent. Waiting for 5sec **/
- public boolean isElementPresent(WebElement element) {
- boolean result = false;
- driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
- try {
- element.getTagName();
- result = true;
- } catch (NoSuchElementException e) {
- System.out.println(element + " wasn't found");
- }
- driver.manage().timeouts()
- .implicitlyWait(ConfigProperties.getWaitTime(),TimeUnit.SECONDS);
- return result;
- }
- /** same as isElementPeresent, but don't wait 5 seconds until evaluate **/
- public boolean isElementPresent0(WebElement element) {
- driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
- try {
- element.getTagName();
- System.out.println(element + " is present");
- return true;
- } catch (NoSuchElementException e) {
- System.out.println(element + " wasn't found");
- return false;
- } finally {
- driver.manage().timeouts()
- .implicitlyWait(ConfigProperties.getWaitTime(),TimeUnit.SECONDS);
- }
- }
- public boolean isElementPresent0(By by) {
- driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
- boolean result = driver.findElements(by).size() > 0;
- driver.manage().timeouts().implicitlyWait(ConfigProperties.getWaitTime(), TimeUnit.SECONDS);
- return result;
- }
- // TODO Not The best implementation!!!!
- public void waitForElementAbsent(By locator) {
- driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
- System.out.print("Waiting");
- while (isElementVisible(locator)) {
- try {
- driver.findElement(By.id("NOT EXISTING ELEMENT"));
- } catch (NoSuchElementException e) {
- System.out.print(".");
- }
- }
- driver.manage().timeouts()
- .implicitlyWait(ConfigProperties.getWaitTime(),TimeUnit.SECONDS);
- }
- public static <T extends BasePage> T create(WebDriver driver,
- Class<T> pageClass) {
- return PageFactoryEx.init(driver, pageClass);
- }
- public String getMatcherByRegExp(String link, String pattern) {
- String otp = "";
- Matcher matcher = Pattern.compile(pattern).matcher(link);
- while (matcher.find()) {
- otp = matcher.group(1);
- }
- return otp;
- }
- // TODO explore how it should be use
- public static String getIdValueOfAnnotation(String paramName, Class<?> clazz)
- throws NoSuchFieldException {
- Field field = clazz.getDeclaredField(paramName);
- FindBy annotation = field.getAnnotation(FindBy.class);
- return annotation.id();
- }
- public WebElement scrollToElement(By by) {
- return scrollToElement(driver.findElement(by));
- }
- public WebElement scrollToElement(WebElement element) {
- System.out.println("text_ " + element.getText());
- int elementPosition = element.getLocation().getY();
- System.out.println("elementPosition " + elementPosition);
- String js = String.format("window.scroll(0, %s) ", elementPosition);
- ((JavascriptExecutor) driver).executeScript(js);
- System.out.println("Scrolled");
- return element;
- }
- public void scrollUntilVisible(WebElement by, By expected) {
- while (!isElementPresent0(expected)) {
- scrollToElement(by);
- }
- }
- public void acceptAllert() {
- Alert alert = driver.switchTo().alert();
- alert.accept();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement