0

我需要在 Microsoft Edge 和 Opera 上运行我的睾丸。但我在任何地方都找不到如何做到这一点。我有这两种浏览器的驱动程序,并且知道我可以在哪里运行,但是配置它并不像我已经完成的那样比 Firefox 和 Chrome 容易。下面是我的 WebDriverFactory 类:

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import com.microsoft.edge.seleniumtools.EdgeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeDriverService;
import org.openqa.selenium.edge.EdgeOptions;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.opera.OperaDriverService;
import org.openqa.selenium.opera.OperaOptions;

import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public final class WebDriverFactory {

    private static String executionMode;
    private static String browserVersion;

    private static WebDriver createLocalDriver() {
        browserVersion = System.getProperty("browser", "opera");

        switch (browserVersion) {
        case "firefox":
            System.setProperty("webdriver.firefox.bin", WebDriverFactory.getFirefoxBrowserBinary());
            System.setProperty("webdriver.gecko.driver", WebDriverFactory.getFirefoxDriverBinary());
            FirefoxOptions ffoptions = new FirefoxOptions();
            ffoptions.setCapability("--ignore-certificate-errors", false);
            ffoptions.setCapability("-inprivate", false);
            ffoptions.setCapability("disable-gpu", false);
            ffoptions.setCapability("start-maximized", false);
            ffoptions.setCapability("acceptInsecureCerts", true);

            return new FirefoxDriver(ffoptions);

        case "chrome":
            System.setProperty("webdriver.chrome.driver", WebDriverFactory.getChromeDriverBinary());
            ChromeOptions chromeoptions = new ChromeOptions();
            chromeoptions.setExperimentalOption("useAutomationExtension", false);
            chromeoptions.addArguments("start-maximized", "--incognito", "--no-sandbox", "--disable-gpu");
            chromeoptions.addArguments("--ignore-ssl-errors=yes", "--ignore-certificate-errors", "ignore-urlfetcher-cert-requests");
            chromeoptions.addArguments("disable-popup-blocking", "disable-infobars", "--disable-gpu");
            ChromeDriver driver = new ChromeDriver(options);

            DesiredCapabilities dcChrome = DesiredCapabilities.chrome();
            dcChrome.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

            return new ChromeDriver(chromeoptions);

        case "chrome_headless":
            System.setProperty("webdriver.chrome.driver", WebDriverFactory.getChromeDriverBinary());
            ChromeOptions chromeoptionsHeadless = new ChromeOptions();
            chromeoptionsHeadless.setExperimentalOption("useAutomationExtension", false);
            chromeoptionsHeadless.addArguments("--window-size=1920x1080", "--headless", "--ignore-certificate-errors",
                    "start-maximized");

            return new ChromeDriver(chromeoptionsHeadless);

        case "edge":
            System.setProperty("webdriver.edge.driver", WebDriverFactory.getEdgeDriverBinary());
            System.setProperty("webdriver.edge.verboseLogging", "true");
            WebDriver edgedriver = new EdgeDriver();
            
            EdgeOptions edgeOptions = new EdgeOptions();

            edgeOptions.UseChromium = true; 
            edgeOptions.AddArgument("-inprivate");
            edgeOptions.AddArgument("headless");
            edgeOptions.AddArgument("disable-gpu");
            edgeOptions.setBinary("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe");         
            
            edgeOptions.setCapability("--ignore-certificate-errors", false);
            edgeOptions.setCapability("-inprivate", false);
            edgeOptions.setCapability("disable-gpu", false);
            edgeOptions.setCapability("start-maximized", false);
            edgeOptions.setCapability("acceptInsecureCerts", true);
            
            Capabilities edgecapabilities = ((RemoteWebDriver) edgedriver).getCapabilities();
            EdgeDriverService service = EdgeDriverService.createDefaultService();
            WebDriver edgedriver2 = new EdgeDriver(service, edgeOptions);

            return new EdgeDriver(edgeOptions);
            
        case "opera":
            System.setProperty("webdriver.opera.driver", WebDriverFactory.getOperaDriverBinary());
            WebDriver operadriver = new OperaDriver();
            
            OperaOptions operaoptions = new OperaOptions();
            operaoptions.addArguments("start-maximized", "--incognito", "--no-sandbox", "--disable-gpu");
            operaoptions.addArguments("--ignore-ssl-errors=yes", "--ignore-certificate-errors", "ignore-urlfetcher-cert-requests");
            operaoptions.addArguments("disable-popup-blocking", "disable-infobars", "--disable-gpu");
            operaoptions.setCapability("acceptInsecureCerts", true);
            
            Capabilities operacapabilities = ((RemoteWebDriver) operadriver).getCapabilities();
            OperaDriverService operadriverservice = OperaDriverService.createDefaultService();
            OperaDriver operadriver2 = new OperaDriver(operadriverservice, operaoptions);

            return new OperaDriver(operaoptions);

        default:
            throw new RuntimeException("Unsupported browser version");
        }
    }

    private static String getChromeDriverBinary() {
        String resourcesPath = new File("src/test/resources").getAbsolutePath();
        return resourcesPath + "/browsers/" + "chrome" + "/driver_v94/chromedriver.exe";
    }

    private static String getFirefoxDriverBinary() {
        String resourcesPath = new File("src/test/resources").getAbsolutePath();
        return resourcesPath + "/browsers/" + "firefox" + "/firefox76/driver_v0.26.0/geckodriver.exe";
    }

    private static String getFirefoxBrowserBinary() {
        String resourcesPath = new File("src/test/resources").getAbsolutePath();
        return resourcesPath + "/browsers/" + "firefox" + "/firefox76/install/firefox.exe";
    }

    private static String getEdgeDriverBinary() {
        String resourcesPath = new File("src/test/resources").getAbsolutePath();
        return resourcesPath + "/browsers/" + "edge" + "/x64/v94.0.992.50/msedgedriver.exe";
    }
    
    private static String getOperaDriverBinary() {
        String resourcesPath = new File("src/test/resources").getAbsolutePath();
        return resourcesPath + "/browsers/" + "opera" + "/x64/94.0.4606.61/operadriver.exe";
    }


    private static RemoteWebDriver createRemoteDriver() {

        browserVersion = System.getProperty("browser", "chrome");
        DesiredCapabilities capabilities = null;

        switch (browserVersion) {
        case "firefox":
            FirefoxOptions options = new FirefoxOptions();
            options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
            capabilities = DesiredCapabilities.firefox();
            capabilities.setBrowserName("firefox");
            capabilities.setCapability("moz:firefoxOptions", options);
            break;

        case "chrome":
            capabilities = DesiredCapabilities.chrome();
            capabilities.setBrowserName("chrome");
            capabilities.setCapability("--ignore-certificate-errors", 94);
            DesiredCapabilities dcChrome = DesiredCapabilities.chrome();
            dcChrome.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
            break;

        case "edge":
            EdgeOptions edgeOptions = new EdgeOptions();
//          edgeOptions.setBinary("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe");
            DesiredCapabilities caps = DesiredCapabilities.edge();
            caps = DesiredCapabilities.edge();
            caps.setBrowserName("edge");

            break;

        case "remoteChromeHeadless":
            return getRemoteWebDriverForRemoteChromeHeadless();
        }

        try {
            return new RemoteWebDriver(new URL("http://10.243.139.69:4444/wd/hub"), capabilities);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * Configures the Chrome for the remote machine
     * 
     * @return the RemoveWebDriver created
     * @throws RuntimeException if the URL is not possible to be created
     */
    private static RemoteWebDriver getRemoteWebDriverForRemoteChromeHeadless() {

        final String proxyUrl = System.getProperty("proxy-server", "bizproxy:3131");

        ChromeOptions optionsChrome = new ChromeOptions();
        optionsChrome.addArguments("--headless");
        optionsChrome.addArguments("--no-sandbox");
        optionsChrome.addArguments("--disable-extensions");
        optionsChrome.addArguments("--proxy-server=" + proxyUrl);
        optionsChrome.addArguments("--disable-dev-shm-usage");
        optionsChrome.addArguments("--ignore-ssl-errors=yes");
        optionsChrome.addArguments("--ignore-certificate-errors");
        optionsChrome.addArguments("excludeSwitches");
        optionsChrome.addArguments("disable-popup-blocking");
        optionsChrome.addArguments("--disable-gpu");
        optionsChrome.addArguments("window-size=1920,1080");
        optionsChrome.addArguments("-incognito");
        optionsChrome.addArguments("start-maximized");
        optionsChrome.addArguments("disable-infobars");

        DesiredCapabilities dcChrome = DesiredCapabilities.chrome();
        dcChrome.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
        dcChrome.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        dcChrome.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);

        try {
            return new RemoteWebDriver(new URL(System.getProperty("seleniumServerUrl")), optionsChrome);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static WebDriver createDriver() {
        executionMode = System.getProperty("execution", "local");

        switch (executionMode) {
        case "local":
            return createLocalDriver();

        case "remote":
            return createRemoteDriver();

        default:
            throw new RuntimeException("Unsupported execution mode");
        }
    }
}

我是这样实现的,但是“AddArguments”功能不好,所以我必须发表评论。有人可以帮忙吗?

4

0 回答 0