com.coderoad.automation.rocketTruedx.RocketTruedxNaBasePage.java Source code

Java tutorial

Introduction

Here is the source code for com.coderoad.automation.rocketTruedx.RocketTruedxNaBasePage.java

Source

/*
 * CP3BasePage.java
 * Copyright (c) 2014, CODEROAD, All Rights Reserved.
 *
 * This software is the confidential and proprietary information of CODEROAD
 * ("Confidential Information"). You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement you entered into with
 * CODEROAD.
 */
package com.coderoad.automation.rocketTruedx;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

import com.coderoad.automation.common.enums.LogLevel;
import com.coderoad.automation.common.log.LogUtil;
import com.coderoad.automation.common.util.WaitUtil;
import com.coderoad.automation.common.util.old.BasePage;
import com.coderoad.automation.common.util.old.TestLogger;
import com.coderoad.automation.common.util.old.TestUtils;

/**
 * The Class CP3BasePage.
 * 
 * @author Ruth Chirinos
 * @version 1
 */
public abstract class RocketTruedxNaBasePage extends BasePage {

    public final static String FRAMESET_PARENT = "frame1";

    public final static String HEADER = "headerMenu";

    public final static String FRAMESET_SUBPARENT = "frameset1";
    public final static String MENU = "menu";
    public final static String MAIN = "main";

    /**
     * Instantiates a new c p3 base page.
     * 
     * @param driver the driver
     */
    public RocketTruedxNaBasePage(WebDriver driver) {

        super(driver);
    }

    /*
     * Method override
     */
    public void openPage(String url) throws InterruptedException {

        super.openPage(url);
        Thread.sleep(2000);
        this.waitForVisibility(getDriver().findElement(By.name("userName")));
    }

    /**
     * this method sets the state of the checkbox.
     * 
     * @param fieldName the field name
     * @param state the state
     */
    protected void setCheckBoxState(WebElement fieldName, boolean state) {

        String id = fieldName.getAttribute("id");
        String classStr = fieldName.getAttribute("class");
        TestLogger.logDebug(id + ":" + classStr);
        WebElement checkboxElem = getDriver().findElement(By.id(id + "-inputEl")); // rememberMeCb-inputEl
        if (classStr.contains("x-form-cb-checked"))
            if (!state) {
                checkboxElem.click();
            } else {
                TestLogger.logDebug("Check box already Checked");
            }
        else {
            if (state) {
                checkboxElem.click();
            } else {
                TestLogger.logDebug("Check box already unchecked");
            }
        }
        classStr = fieldName.getAttribute("class");
        TestLogger.logDebug(id + ":" + classStr + ":" + state);
    }

    /**
     * this method return the state of the check box.
     * 
     * @param fieldName the field name
     * @return the check box state
     */
    protected boolean getCheckBoxState(WebElement fieldName) {

        String classStr = fieldName.getAttribute("class");
        if (classStr.contains("x-form-cb-checked"))
            return true;
        else
            return false;
    }

    /*
     * Method override
     */
    protected WebElement waitForElement(final By by) {

        WebElement element = null;
        TestLogger.logMsg("Start waitForElement : + " + this.getTimeStamp());
        for (int i = 0; i < 1; i++) {
            try {
                element = new WebDriverWait(driver, TestUtils.timeout).until(new ExpectedCondition<WebElement>() {

                    @Override
                    public WebElement apply(WebDriver d) {

                        return d.findElement(by);
                    }
                });
            } catch (Exception e) {
                TestLogger.logMsg("Wait waitForElement : + " + this.getTimeStamp());
            }
        }
        TestLogger.logMsg("Done waitForElement : + " + this.getTimeStamp());
        return element;
    }

    /*
     * Method override
     */
    protected void waitForVisibility(WebElement element) {

        TestLogger.logMsg("Start waitForVisibility : + " + this.getTimeStamp());
        for (int i = 0; i < 2; i++) {
            try {
                WebElement myDynamicElement = (new WebDriverWait(driver, TestUtils.timeout))
                        .until(ExpectedConditions.visibilityOf(element));
            } catch (Exception e) {
                TestLogger.logMsg("Wait waitForVisibility : + " + this.getTimeStamp());
            }
        }
        TestLogger.logMsg("Done waitForVisibility : + " + this.getTimeStamp());
    }

    /*
     * Method override
     */
    protected void waitForElementPresence(String element) {

        TestLogger.logMsg("Start waitForElementPresence : + " + this.getTimeStamp());
        for (int i = 0; i < 1; i++) {
            try {
                WebElement myDynamicElement = (new WebDriverWait(driver, TestUtils.timeout))
                        .until(ExpectedConditions.elementToBeClickable(By.id(element)));
            } catch (Exception e) {
                TestLogger.logMsg("Wait waitForElementPresence : + " + this.getTimeStamp());
            }
        }
        TestLogger.logMsg("Done waitForElementPresence : + " + this.getTimeStamp());
    }

    /*
     * Method override
     */
    protected void waitForElementPresence(final By by) {

        TestLogger.logMsg("Start waitForElementPresence : + " + this.getTimeStamp());
        for (int i = 0; i < 1; i++) {
            try {
                WebElement myDynamicElement = (new WebDriverWait(driver, TestUtils.timeout))
                        .until(ExpectedConditions.elementToBeClickable(by));
            } catch (Exception e) {
                TestLogger.logMsg("Wait waitForElementPresence : + " + this.getTimeStamp());
            }

        }
        TestLogger.logMsg("Done waitForElementPresence : + " + this.getTimeStamp());
    }

    /**
     * this method selects the options from the combobox.
     * 
     * @param dropDown the drop down
     * @param option the option
     */
    protected void selectOptionIn(WebElement dropDown, String option) {

        String triggerId = dropDown.getAttribute("id");
        WebElement arrow = dropDown.findElement(By.id(triggerId));
        if (arrow.isDisplayed()) {
            arrow.click();
        }
        WaitUtil.waitUntil(5);
        List<WebElement> options = getDriver().findElements(By.xpath("//li[@role='option']"));

        for (WebElement opt : options) {
            if (opt.getText().equalsIgnoreCase(option)) {
                click(opt);
                WaitUtil.waitUntil(2);
                break;
            }
        }
    }

    /**
     * this method send the click () event to the web element.
     * 
     * @param elem the elem
     */
    protected void click(WebElement elem) {

        // elem.click();
        JavascriptExecutor executor = (JavascriptExecutor) getDriver();
        executor.executeScript("arguments[0].click();", elem);
    }

    /*
     * Method override
     */
    public void waitForPageLoaded() {

        ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>() {

            public Boolean apply(WebDriver driver) {

                return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
            }
        };

        Wait<WebDriver> wait = new WebDriverWait(this.getDriver(), TestUtils.timeout);
        try {
            wait.until(expectation);
        } catch (Throwable error) {
            TestLogger.logError("Timeout waiting for Page Load Request to complete.");
        }
    }

    /**
     * this method returns the timestamp.
     * 
     * @return the time stamp
     */
    public String getTimeStamp() {

        java.util.Date date = new java.util.Date();
        return (new Timestamp(date.getTime()).toString());
    }

    /**
     * Takepic.
     * 
     * @param make the make
     */
    public void takepic(String make) {

        String browser = getBrowserName(getDriver());
        WebDriver augmentedDriver = new Augmenter().augment(getDriver());

        File scrFile = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE);

        DateFormat dateFormat = new SimpleDateFormat("dd_MMM_yyyy__hh_mm_ssaa");

        String destDir = "testresult/" + make;
        new File(destDir).mkdirs();
        /*
         * if(browser.contentEquals("Firefox")) browser="Firefox_"; else
         * if(browser.contentEquals("Internet Explorer"))
         * browser="InternetExplorer_";
         */
        String destFile = browser + "_" + dateFormat.format(new Date()) + ".png";

        try {
            FileUtils.copyFile(scrFile, new File(destDir + "/" + destFile));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Gets the browser name.
     * 
     * @param driver the driver
     * @return the browser name
     */
    protected String getBrowserName(WebDriver driver) {

        if (driver instanceof RemoteWebDriver) {
            Capabilities capabilities = ((RemoteWebDriver) driver).getCapabilities();

            return capabilities.getBrowserName() + "_" + capabilities.getVersion();
        }

        return "UNKNOWN_BROWSER";
    }

    /*
     * This method checks the validation when the phone number entered is
     * incorrect 1: correct number 0: incorrect number
     */
    /**
     * Verify phone number validation.
     * 
     * @param xpathInputPhoneNumber the xpath input phone number
     * @param phone the phone
     * @param typeValidation the type validation
     */
    public void verifyPhoneNumberValidation(String xpathInputPhoneNumber, String phone, String typeValidation) {

        WaitUtil.waitUntil(3);
        WebElement element = driver.findElement(By.xpath(xpathInputPhoneNumber));
        element.clear();
        element.sendKeys(phone);
        element = driver.findElement(By.xpath(xpathInputPhoneNumber));
        Assert.assertTrue(
                element.getAttribute("aria-invalid").contains(typeValidation.equals("1") ? "false" : "true"),
                "The validation does not function correctly.");
        LogUtil.log("The validation functions correctly.", LogLevel.LOW);
    }
}