SetUp Driver

博客提及通过一个通用类来管理驱动,涉及到配置方面的内容,与信息技术中驱动管理相关。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Manage driver via a common class: Config 

public final class Config {
    private static final String DEFAULT_BROWSER = "ie";

    private static Logger log = LoggerFactory.getLogger(Config.class);
    private static ThreadLocal<RemoteWebDriver> drivers = new ThreadLocal<RemoteWebDriver>();

    public static RemoteWebDriver driver() {
        return Config.drivers.get();
    }

    public static void setupRemoteWebDriver(final String url, final DesiredCapabilities capabilities) throws MalformedURLException {
        Config.drivers.set(new RemoteWebDriver(new URL(url + "/wd/hub"), capabilities));

 public static void setupWebDriver(String browser, final String url) throws Exception {
        if (StringUtils.isEmpty(browser)) {
            browser = Config.DEFAULT_BROWSER;
        }

        if (url != null) {// remote browser
            if (browser.equalsIgnoreCase("ie")) {
                DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
                capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
                capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
                capabilities.setCapability(InternetExplorerDriver.NATIVE_EVENTS, false);
                capabilities.setCapability(InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING, true);
                
                setupRemoteWebDriver(url, capabilities);
            } else if (browser.equalsIgnoreCase("firefox")) {
                setupRemoteWebDriver(url, DesiredCapabilities.firefox());               
            } else if (browser.equalsIgnoreCase("chrome")) {
               DesiredCapabilities capabilities = DesiredCapabilities.chrome();
                ChromeOptions options = new ChromeOptions();
                //options.addArguments("--disable-extensions");
                options.setExperimentalOption("useAutomationExtension", false);
                //options.addArguments("disable-infobars");
               // options.addArguments("--enable-precise-memory-info");
               // options.addArguments("--disable-popup-blocking");
               // options.addArguments("--disable-default-apps");
               // options.addArguments("test-type=browser");
                
                capabilities.setCapability(ChromeOptions.CAPABILITY, options);
                setupRemoteWebDriver(url, capabilities);
            } else {
                Config.log.error("Browser do not support: " + browser);
            }
            Config.drivers.set(driver());
            Config.drivers.get().manage().window().maximize();
        } else {// local browser
            RemoteWebDriver driver = null;
            if (browser.equalsIgnoreCase("ie")) {
                DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
                capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
                System.setProperty("webdriver.ie.driver",
                    "C:/xxxx/IEDriverServer.exe");
                driver = new InternetExplorerDriver(capabilities);
            } else if (browser.equalsIgnoreCase("firefox")) {
                // FirefoxDriverManager.getInstance().version("0.14").setup();
                // driver = new FirefoxDriver();
                System.setProperty("webdriver.gecko.driver",
                    "C:/xxxxx/geckodriver-v0.17.0-win64/geckodriver.exe");
                driver = new FirefoxDriver();
            } else if (browser.equalsIgnoreCase("chrome")) {
               // ChromeDriverManager.getInstance().setup();
                System.setProperty("webdriver.chrome.driver",
                    "C:/xxxxxx/chromedriver_win32_v2.34/chromedriver.exe");
                driver = new ChromeDriver();
            } else {
                throw new IllegalArgumentException("Browser not support: " + browser);
            }
            Config.drivers.set(driver);
            Config.drivers.get().manage().window().maximize();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值