com.htmlhifive.pitalium.core.selenium.PtlWebDriverWait.java Source code

Java tutorial

Introduction

Here is the source code for com.htmlhifive.pitalium.core.selenium.PtlWebDriverWait.java

Source

/*
 * Copyright (C) 2015-2016 NS Solutions Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.htmlhifive.pitalium.core.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.htmlhifive.pitalium.common.exception.TestRuntimeException;

/**
 * ??wait?
 */
public class PtlWebDriverWait extends WebDriverWait {

    private static final Logger LOG = LoggerFactory.getLogger(PtlWebDriverWait.class);
    private static final String DUMMY_TAG_ID = "dummy";
    private static final ExpectedCondition<WebElement> FIND_DUMMY_TAG_CONDITION = new ExpectedCondition<WebElement>() {
        @Override
        public WebElement apply(WebDriver webDriver) {
            return webDriver.findElement(By.id(DUMMY_TAG_ID));
        }
    };

    //@formatter:off
    //CHECKSTYLE:OFF
    private static final String LOAD_COMPLETE_SCRIPT = "function pitalium_loadComplete () {"
            + "  var element = document.createElement('div');" + "  element.id = '%s';"
            + "  document.body.appendChild(element);" + "}" + "if (document.readyState == 'complete') {"
            + "  pitalium_loadComplete();" + "} else {"
            + "  window.addEventListener('load', pitalium_loadComplete);" + "}";

    private static final String DELETE_TAG_SCRIPT = "document.getElementById('%s').parentNode.removeChild(document.getElementById('%s'));";
    //CHECKSTYLE:ON
    //@formatter:on

    private final PtlWebDriver driver;

    /**
     * 
     * 
     * @param driver WebDriver
     * @param timeOutInSeconds 
     */
    public PtlWebDriverWait(PtlWebDriver driver, long timeOutInSeconds) {
        super(driver, timeOutInSeconds);
        this.driver = driver;
    }

    /**
     * 
     * 
     * @param driver WebDriver
     * @param timeOutInSeconds 
     * @param sleepInMillis ???
     */
    public PtlWebDriverWait(PtlWebDriver driver, long timeOutInSeconds, long sleepInMillis) {
        super(driver, timeOutInSeconds, sleepInMillis);
        this.driver = driver;
    }

    /**
     * ?Load?????
     */
    public void untilLoad() {
        final int sleepMillis = 100;
        try {
            Thread.sleep(sleepMillis);
            String beforeURL = getURL();
            try {
                addElement(DUMMY_TAG_ID);

                // ?????????????
                until(FIND_DUMMY_TAG_CONDITION);

                deleteElement(DUMMY_TAG_ID);
            } catch (Exception ex) {
                String afterURL = getURL();
                if (!beforeURL.equals(afterURL)) {
                    LOG.warn("wait???????????wait???");

                    // URL????????????????
                    untilLoad();
                } else {
                    throw new TestRuntimeException(ex);
                }
            }
        } catch (InterruptedException e) {
            throw new TestRuntimeException(e);
        }
    }

    /**
     * ??URL????
     * 
     * @return ??URL
     */
    private String getURL() {
        return driver.executeJavaScript("return location.href");
    }

    /**
     * ??id??div????
     * 
     * @param id ???id
     */
    private void addElement(String id) {
        driver.executeScript(String.format(LOAD_COMPLETE_SCRIPT, id));
    }

    /**
     * ??id?????
     * 
     * @param id ???id
     */
    private void deleteElement(String id) {
        driver.executeScript(String.format(DELETE_TAG_SCRIPT, id, id));
    }

}