selenium之如何启动不同浏览器的driver,兼容性就是这么简单!

一.概述

大家都知道selenium支持不同的浏览器,而webdriver启动项目时需要启动浏览器的driver,于是乎配置不同的浏览器来启动不同的driver势在必行了,

下面请看代码;

二.编写一个初始化selenium测试框架driver类

public class SeleniumDriver {
    
    private Log log=new Log(this.getClass());
    
    private WebDriver driver;
    
    public WebDriver getDriver() {
        return driver;
    }

    public SeleniumDriver(){
        this.initialDriver();
    }
    
    public void initialDriver(){
        if("firefox".equals(Config.browser)){
            ProfilesIni allProfiles = new ProfilesIni(); 
            FirefoxProfile profile = allProfiles.getProfile("default");
            System.setProperty("webdriver.firefox.bin", "D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
            driver = new FirefoxDriver();
        }else if("ie".equals(Config.browser)){
            System.setProperty("webdriver.ie.driver", "files/IEDriverServer64.exe");
            DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
            capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
            capabilities.setCapability("ignoreProtectedModeSettings",true);       
            driver = new InternetExplorerDriver(capabilities);
        }else if("chrome".equals(Config.browser)){
            System.setProperty("webdriver.chrome.driver", "files/chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--test-type");
            driver = new ChromeDriver(options);
        }else{    
            log.info(Config.browser+" 的值不正确,请检查!");
            throw new DefinedException(Config.browser+" 的值不正确,请检查!");
        }
    }

三.关于Config类的情况

Config是一个加载了浏览器信息的静态类,通过该类可以读取到浏览器的配置信息。。。。。。。哈哈,对于上面的代码,你是否豁然开朗了许多呢!!!!!!

原文地址:https://www.cnblogs.com/liwu/p/5010066.html