All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.thucydides.core.webdriver.WebDriverFacade Maven / Gradle / Ivy

There is a newer version: 0.9.275
Show newest version
package net.thucydides.core.webdriver;

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Set;

/**
 * A proxy class for webdriver instances, designed to prevent the browser being opened unnecessarily.
 */
public class WebDriverFacade implements WebDriver, TakesScreenshot {

    private final Class driverClass;

    protected WebDriver proxiedWebDriver;

    private static final Logger LOGGER = LoggerFactory.getLogger(WebDriverFacade.class);

    public WebDriverFacade(final Class driverClass) {
        this.driverClass = driverClass;
    }

    public WebDriver getProxiedDriver() {
        if (proxiedWebDriver == null) {
            proxiedWebDriver = newProxyDriver();
            WebdriverProxyFactory.getFactory().notifyListenersOfWebdriverCreationIn(this);
        }
        return proxiedWebDriver;
    }

    public void reset() {
        if (proxiedWebDriver != null) {
            forcedQuit();
        }
        proxiedWebDriver = null;

    }

    private void forcedQuit() {
        try {
            getDriverInstance().quit();
            proxiedWebDriver = null;
        } catch (WebDriverException e) {
            LOGGER.warn("Closing a driver that was already closed",e);
        }
    }

    protected WebDriver newProxyDriver() {
        WebDriver newDriver = null;
        if (usingAMockDriver()) {
            newDriver = WebdriverProxyFactory.getFactory().getMockDriver();
        } else {
            newDriver = newDriverInstance();
        }
        return newDriver;
    }

    private WebDriver newDriverInstance() {
        WebDriver newDriver = null;
        try {
            newDriver = driverClass.newInstance();
        } catch (Exception e) {
            throw new UnsupportedDriverException("Could not instantiate " + driverClass);
        }
        return newDriver;
    }

    private boolean usingAMockDriver() {
        return (WebdriverProxyFactory.getFactory().getMockDriver() != null);
    }

    public  X getScreenshotAs(final OutputType target) {
        if (proxyInstanciated() && driverCanTakeScreenshots()) {
            try {
                return ((TakesScreenshot) getProxiedDriver()).getScreenshotAs(target);
            } catch (WebDriverException e) {
                LOGGER.error("Failed to take screenshot - driver closed already?", e);
            }
        }
        return null;
    }

    private boolean driverCanTakeScreenshots() {
        return (TakesScreenshot.class.isAssignableFrom(getProxiedDriver().getClass()));
    }

    public void get(final String url) {
        getProxiedDriver().get(url);
    }

    public String getCurrentUrl() {
        return getProxiedDriver().getCurrentUrl();
    }

    public String getTitle() {
        return getProxiedDriver().getTitle();
    }

    public List findElements(final By by) {
        return getProxiedDriver().findElements(by);
    }

    public WebElement findElement(final By by) {
        return getProxiedDriver().findElement(by);
    }

    public String getPageSource() {
        return getProxiedDriver().getPageSource();
    }

    protected WebDriver getDriverInstance() {
        return proxiedWebDriver;
    }

    public void close() {
        if (proxyInstanciated()) {
            getDriverInstance().close();
        }
    }

    public void quit() {
        if (proxyInstanciated()) {
            getDriverInstance().quit();
            proxiedWebDriver = null;
        }
    }

    protected boolean proxyInstanciated() {
        return (getDriverInstance() != null);
    }

    public Set getWindowHandles() {
        return getProxiedDriver().getWindowHandles();
    }

    public String getWindowHandle() {
        return getProxiedDriver().getWindowHandle();
    }

    public TargetLocator switchTo() {
        return getProxiedDriver().switchTo();
    }

    public Navigation navigate() {
        return getProxiedDriver().navigate();
    }

    public Options manage() {
        return getProxiedDriver().manage();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy