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

br.com.jarch.test.BrowseFactory Maven / Gradle / Ivy

There is a newer version: 24.9.0
Show newest version
package br.com.jarch.test;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.safari.SafariOptions;

import java.io.File;
import java.time.Duration;

public final class BrowseFactory {

    private static WebDriver webDriver;

    public static String chromeDriver = System.getenv("CHROME_DRIVER") == null ? "/usr/local/bin/chromedriver" : System.getenv("CHROME_DRIVER");
    public static String fireFoxDriver = System.getenv("FIREFOX_DRIVER") == null ? "/usr/local/bin/geckodriver" : System.getenv("FIREFOX_DRIVER");
    public static String safariDriver = System.getenv("OPERA_DRIVER") == null ? "/usr/local/bin/safaridriver" : System.getenv("SAFARI_DRIVER");

    private BrowseFactory() {

    }

    public static WebDriver getChromeDriver() {
        if (webDriver instanceof ChromeDriver) {
            return webDriver;
        }

        webDriver = getChromeDriver(1365);
        return webDriver;
    }

    public static WebDriver getChromeDriver(int width) {
        if (webDriver instanceof ChromeDriver) {
            return webDriver;
        }

        if (!new File(chromeDriver).exists()) {
            throw new RuntimeException("Driver do CHROME não localizado! Configurar a variável de ambiente CHROME_DRIVER...");
        }

        System.setProperty("webdriver.chrome.driver", chromeDriver);

        webDriver = new ChromeDriver();
        configureBrowse(width, webDriver);
        return webDriver;
    }

    public static WebDriver getChromeHeadlessDriver() {
        if (webDriver instanceof ChromeDriver) {
            return webDriver;
        }

        if (!new File(chromeDriver).exists()) {
            throw new RuntimeException("Driver do CHROME não localizado! Configurar a variável de ambiente CHROME_DRIVER...");
        }

        System.setProperty("webdriver.chrome.driver", chromeDriver);

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");
        options.addArguments("--window-size=1365,735");

        webDriver = new ChromeDriver(options);

        webDriver
                .manage()
                .timeouts()
                .implicitlyWait(Duration.ofSeconds(5))
                .pageLoadTimeout(Duration.ofSeconds(30));

//        configureBrowse(monitor, width, height, webDriver);

        return webDriver;
    }

    public static WebDriver getFirefoxDriver() {
        if (webDriver instanceof FirefoxDriver) {
            return webDriver;
        }

        if (!new File(fireFoxDriver).exists()) {
            throw new RuntimeException("Driver do FIREFOX não localizado! Configurar a variável de ambiente FIREFOX_DRIVER...");
        }

        webDriver = getFirefoxDriver(1365);

        return webDriver;
    }

    public static WebDriver getFirefoxDriver(int width) {
        if (webDriver instanceof FirefoxDriver) {
            return webDriver;
        }

        System.setProperty("webdriver.gecko.driver", fireFoxDriver);
        webDriver = new FirefoxDriver();
        configureBrowse(width, webDriver);

        return webDriver;
    }

    public static WebDriver getSafariDriver() {
        if (webDriver instanceof SafariDriver) {
            return webDriver;
        }

        if (!new File(safariDriver).exists()) {
            throw new RuntimeException("Driver do SAFARI não localizado! Configurar a variável de ambiente SAFARI_DRIVER...");
        }

        webDriver = getSafariDriver(1365);

        return webDriver;
    }

    public static WebDriver getSafariDriver(int width) {
        if (webDriver instanceof SafariDriver) {
            return webDriver;
        }

        System.setProperty("webdriver.safari.driver", safariDriver);
        System.setProperty("opera.binary", "/usr/bin/safari");
        SafariOptions oo = new SafariOptions();
//        oo.setBinary("/usr/bin/safari");
        webDriver = new SafariDriver(oo);
        configureBrowse(width, webDriver);

        return webDriver;
    }

    private static void configureBrowse(int width, WebDriver driver) {
        driver
                .manage()
                .timeouts()
                .implicitlyWait(Duration.ofSeconds(5))
                .pageLoadTimeout(Duration.ofSeconds(30));
//        driver.manage().window().setPosition(new Point(monitor == 1 ? -3000 : 3000, 0));

        driver.manage().window().fullscreen();
        driver.manage().window().maximize();

        if (driver.manage().window().getSize().width > 2000) {
            driver.manage().window().setSize(new Dimension(driver.manage().window().getSize().width / 2, driver.manage().window().getSize().height));
            driver.manage().window().setPosition(new Point(driver.manage().window().getSize().width, 0));
        }

        if (driver.manage().window().getSize().width > width) {
            driver.manage().window().setSize(new Dimension(width, driver.manage().window().getSize().height));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy