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

com.clarolab.selenium.pages.browser.web.WebBrowser Maven / Gradle / Ivy

Go to download

Framework for automated testing using Selenium. Provides easy configuration of WebDrivers with BrowserFactory. Provides a Page abstraction.

The newest version!
package com.clarolab.selenium.pages.browser.web;

import com.clarolab.selenium.pages.browser.Browser;
import com.clarolab.selenium.pages.config.TimeoutsConfig;
import com.clarolab.selenium.pages.exception.FrameworkWebDriverException;
import com.clarolab.selenium.pages.pages.BaseTopLevelPage;
import com.clarolab.selenium.pages.pages.TopLevelPage;
import com.clarolab.selenium.pages.webservice.EndpointBuilder;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.net.URI;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

public abstract class WebBrowser extends Browser {
    private static final Logger logger = LoggerFactory.getLogger(WebBrowser.class);

    private final Optional webDriverPath;
    private final Optional browserBinaryPath;
    private final Optional browserVersion;
    private final Optional browserLocale;
    private final Optional startWindowWidth;
    private final Optional startWindowHeight;
    private final Optional browserLogLevel;
    private final Optional browserLogFile;
    private final Optional platform;

    public WebBrowser(String baseTestUrl,
                      TimeoutsConfig timeouts,
                      Optional webDriverPath,
                      Optional browserBinaryPath,
                      Optional browserVersion,
                      Optional browserLocale,
                      Optional startWindowWidth,
                      Optional startWindowHeight,
                      Optional platform) {

        this(baseTestUrl, timeouts, webDriverPath, browserBinaryPath, browserVersion, browserLocale,
                startWindowWidth, startWindowHeight,
                Optional.empty(), Optional.empty(), platform);

    }

    public WebBrowser(String baseTestUrl,
                      TimeoutsConfig timeouts,
                      Optional webDriverPath,
                      Optional browserBinaryPath,
                      Optional browserVersion,
                      Optional browserLocale,
                      Optional startWindowWidth,
                      Optional startWindowHeight,
                      Optional browserLogLevel,
                      Optional browserLogFile,
                      Optional platform) {
        super(baseTestUrl, timeouts);
        this.webDriverPath = webDriverPath;
        this.browserBinaryPath = browserBinaryPath;
        this.browserVersion = browserVersion;
        this.browserLocale = browserLocale;
        this.startWindowWidth = startWindowWidth;
        this.startWindowHeight = startWindowHeight;
        this.browserLogLevel = browserLogLevel;
        this.browserLogFile = browserLogFile;
        this.platform = platform;
    }

    public void initializeBrowser() throws FrameworkWebDriverException {
        this.webDriver = createWebDriver();
        if (startWindowWidth.isPresent() && startWindowHeight.isPresent()) {
            this.webDriver.manage().window().setSize(new Dimension(startWindowWidth.get(), startWindowHeight.get()));
        }
        // Safari web driver doesn't support setting timeouts.
        if (getBrowserType() != WebBrowserType.SAFARI) {
            this.webDriver.manage().timeouts().pageLoadTimeout(getPageTimeoutSeconds(), TimeUnit.SECONDS);
            this.webDriver.manage().timeouts().implicitlyWait(getImplicitWaitTimeoutMillis(), TimeUnit.MILLISECONDS);
        }
        logger.info("SUCCESS - Created WebBrowser of type {}: {}", getBrowserType(), webDriver);
    }

    public abstract WebBrowserType getBrowserType();

    public abstract DesiredCapabilities getDesiredCapabilities();

    public LoggingPreferences getLoggingPreferences() {
        Level level = getLogLevel();
        LoggingPreferences loggingPreferences = new LoggingPreferences();
        loggingPreferences.enable(LogType.BROWSER, level);
        loggingPreferences.enable(LogType.CLIENT, level);
        loggingPreferences.enable(LogType.DRIVER, level);
        loggingPreferences.enable(LogType.SERVER, level);
        return loggingPreferences;
    }

    public Optional getStartWindowWidth() {
        return startWindowWidth;
    }

    public Optional getStartWindowHeight() {
        return startWindowHeight;
    }

    public Optional getWebDriverPath() {
        return webDriverPath;
    }

    public Optional getBrowserBinaryPath() {
        return browserBinaryPath;
    }

    public Optional getBrowserVersion() {
        return browserVersion;
    }

    public Optional getBrowserLocale() {
        return browserLocale;
    }

    public Optional getBrowserLogLevel() {
        return browserLogLevel;
    }

    public Level getLogLevel() {
        return browserLogLevel.isPresent() ? browserLogLevel.get() : Level.WARNING;
    }

    public Optional getBrowserLogFile() {
        return browserLogFile;
    }

    public Optional getPlatform() {
        return platform;
    }

    public TimeoutsConfig getTimeouts() {
        return timeouts;
    }

    public TopLevelPage openPageByURL(String href) {
        return openPageByURL(href, BaseTopLevelPage.class);
    }

    public  T openPageByURL(URI uri, Class pageClass) {
        URI absoluteURI;
        if (uri.isAbsolute()) {
            absoluteURI = uri;
        } else {
            String fullURIStr = EndpointBuilder.uri(baseTestUrl, "/", uri.toString());
            absoluteURI = URI.create(fullURIStr);
        }
        logger.info("Opening web page by URL {}", absoluteURI);
        runLeavePageHook();
        invalidateCachedPage();
        T page = PAGE_UTILS.loadPageFromURL(absoluteURI, pageClass, getWebDriver(), getActions());
        setCachedPage(page);
        return page;
    }

    public  T openPageByURL(String href, Class pageClass) {
        URI uri = URI.create(href);
        return openPageByURL(uri, pageClass);
    }

    public void refreshPage() {
        runLeavePageHook();
        webDriver.navigate().refresh();
        if (optionalCachedPage.isPresent()) {
            TopLevelPage cachedPage = optionalCachedPage.get().getCachedPage();
            cachedPage.refreshElements();
        }
    }

    public  T refreshPage(Class pageClass) {
        runLeavePageHook();
        invalidateCachedPage();
        webDriver.navigate().refresh();
        T page = loadTopLevelPage(pageClass);
        setCachedPage(page);
        return page;
    }

    public void cleanSession() {
        webDriver.manage().deleteAllCookies();
    }

    @Nullable
    public LogEntries getBrowserLogEntries() {
        if (webDriver == null) {
            logger.info(
                String.format("WebDriver was null in %s#getBrowserLogEntries! Returning null.", this.getClass().toString()));
            return null;
        }
        logger.debug("Getting available log types...");
        Set availableLogTypes = webDriver.manage().logs().getAvailableLogTypes();
        logger.debug("Found log types: {}", availableLogTypes);
        if (availableLogTypes == null || !availableLogTypes.contains(LogType.BROWSER)) {
            return null;
        }
        LogEntries logs = webDriver.manage().logs().get(LogType.BROWSER);
        logger.info(
            String.format("Success - obtained Browser logs for a local %s!", this.getClass().toString()));
        return logs;
    }

    protected void setCommonWebBrowserCapabilities(DesiredCapabilities desiredCapabilities) {
        // If a required version is present, then set this as a desired capability. Only affects Remote browsers.
        Optional browserVersion = getBrowserVersion();
        if (browserVersion.isPresent() && !browserVersion.get().isEmpty()) {
            desiredCapabilities.setCapability(CapabilityType.VERSION, browserVersion.get());
        }

        // Set logging preferences.
        LoggingPreferences loggingPreferences = getLoggingPreferences();
        desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, loggingPreferences);

        // If a platform is specified, set this desired capability. Only affects Remote browsers.
        Optional platform = getPlatform();
        platform.ifPresent(desiredCapabilities::setPlatform);
    }


}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy