com.oracle.amc.sqe.util.webui.WebUIPage.java Source code

Java tutorial

Introduction

Here is the source code for com.oracle.amc.sqe.util.webui.WebUIPage.java

Source

package com.oracle.amc.sqe.util.webui;

import java.util.List;
import java.util.logging.Logger;

import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.oracle.amc.data.User;
import com.oracle.amc.sqe.util.RuntimeEnvironment;
import com.oracle.amc.sqe.util.TestLogger;
import com.oracle.amc.sqe.util.TestProperties;

/*
 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

public class WebUIPage {
    protected WebDriver webDriver;
    protected Logger logger;
    protected WebDriverWait wait;

    private String mainPage;
    private String loginPage;
    public static final String NAV_BUTTONS_PATH = "//div[@id='globalButtonsSet']";
    public static final String TAB_BUTTON_PATH = "//div[@id='globalButtonsSet']//span[text()='%s']";
    public static final String LOGIN_BUTTON_ID = "loginButton";
    public static final String EMAIL_INPUT_ID = "adminEmail";
    public static final String PASSWD_INPUT_ID = "adminPassword";
    public static final String LOGGED_IN_USER_EMAIL_PATH = "//button[@id='emailButton']/span[1]";
    public static final String LOG_OUT_BUTTON_ID = "logout";
    public static final String ERROR_DIALOG_CLOSE_PATH = "//button[@id='closeButton']";

    public WebUIPage() {
        try {
            webDriver = WebDriverBuilder.getWebDriver();
            logger = TestLogger.getLogger();
            mainPage = getMainPage();
            loginPage = getAppURL() + "/login.html";
            wait = new WebDriverWait(webDriver, WebDriverBuilder.getWaitTime());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public String getMainPage() {
        return getAppURL() + "/index.html";
    }

    public WebDriver getWebDriver() {
        return webDriver;
    }

    public String getAppURL() {
        String port = System.getProperty(TestProperties.HTTP_PORT).trim();
        String protocal = "http";
        String host = System.getProperty(TestProperties.SERVER_HOST).trim();
        if (RuntimeEnvironment.httpsEnabled()) {
            port = System.getProperty(TestProperties.HTTPS_PORT).trim();
            protocal = "https";
        }
        return String.format("%s://%s:%s/amcwebui", protocal, host, port);
    }

    public void selectTab(String tabName, By tabElement) {
        clickAndWait(By.xpath(String.format(TAB_BUTTON_PATH, tabName)), tabElement);
    }

    public void click(By clickElement) {
        clickAndWait(clickElement, null);
    }

    public void clickAndWait(By clickElement, By waitElement) {
        WebElement element = findElement(clickElement);
        element.click();
        wait(waitElement);
    }

    public void openPage(String subUrl) {
        String url = getAppURL() + "/" + subUrl;
        webDriver.get(url);
    }

    public void openHomePage() {
        logger.info("Going to open page: " + mainPage);
        openMainPage();
        wait(By.xpath(NAV_BUTTONS_PATH));
    }

    public void openMainPage() {
        logger.info("Going to open page: " + mainPage);
        webDriver.get(mainPage);
    }

    public void login(User user) {
        login(user.getEmail(), user.getUserPassword());
    }

    public void login(String emailStr, String passStr) {
        webDriver.get(loginPage);
        wait(By.id(EMAIL_INPUT_ID));
        WebElement email = findElement(By.id(EMAIL_INPUT_ID));
        email.sendKeys(emailStr);
        WebElement passwd = findElement(By.id(PASSWD_INPUT_ID));
        passwd.sendKeys(passStr);
        click(By.id(LOGIN_BUTTON_ID));
        //wait for page refresh
        sleep(1000);
    }

    public void wait(By waitElement) {
        if (waitElement != null) {
            wait.until(ExpectedConditions.visibilityOfElementLocated(waitElement));
        }
    }

    public void sleep(long milliSec) {
        try {
            Thread.sleep(milliSec);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public WebElement findElement(By elementPath) {
        WebElement element = webDriver.findElement(elementPath);
        return element;
    }

    public List<WebElement> findElements(By elementPath) {
        List<WebElement> elements = webDriver.findElements(elementPath);
        return elements;
    }

    public void moveTo(By elementPath) {
        Actions action = new Actions(webDriver);
        WebElement element = findElement(elementPath);
        action.moveToElement(element).build().perform();
    }

    public String getCurrentURL() {
        return webDriver.getCurrentUrl();
    }

    public String getLoginUserEmail() {
        WebElement element = findElement(By.xpath(LOGGED_IN_USER_EMAIL_PATH));
        return element.getText();
    }

    public void logout() {
        clickAndWait(By.xpath(LOGGED_IN_USER_EMAIL_PATH), By.id(LOG_OUT_BUTTON_ID));
        clickAndWait(By.id(LOG_OUT_BUTTON_ID), By.id(LOGIN_BUTTON_ID));
    }

    public boolean isErrorDialogShown() {
        WebElement errorDlgClose = findElement(By.xpath(ERROR_DIALOG_CLOSE_PATH));
        boolean displayed = errorDlgClose.isDisplayed();
        if (displayed) {
            errorDlgClose.click();
        }
        return displayed;
    }

    public void refresh() {
        webDriver.navigate().refresh();
    }

    public void waitText(By by, String str) {
        wait.until(ExpectedConditions.textToBePresentInElementLocated(by, str));
    }

    public void waitValue(By by, String str) {
        wait.until(ExpectedConditions.textToBePresentInElementValue(by, str));
    }

    public String collectCCData() {
        return (String) ((JavascriptExecutor) webDriver)
                .executeScript("return jscoverage_serializeCoverageToJSON()");
    }

}