Java webdriver之啟動不同瀏覽器及加載配置(轉(zhuǎn)載學習)

本文主要記錄下在使用selenium2/webdriver時啟動各種瀏覽器的方法、以及如何加載插件、定制瀏覽器信息(設置profile)等

一、Driver下載地址:
  http://docs.seleniumhq.org/download/
二、啟動firefox瀏覽器
(不需要下載驅(qū)動,原生支持)
1、firefox安裝在默認路徑下:

//啟動默認安裝路徑下的ff
    public void StartFireFoxByDefault(){
        System.out.println("start firefox browser...");
        WebDriver driver = new FirefoxDriver();      //直接new一個FirefoxDriver即可
        Navigation navigation = driver.navigate();
        navigation.to("http://www.baidu.com/");
        System.out.println("start firefox browser succeed...");        
    }

2、firefox未安裝在默認路徑下:

public static void StartFireFoxNotByDefault(){
        System.out.println("start firefox browser...");
        System.setProperty("webdriver.firefox.bin",     //指定firefox的安裝路徑
                "D:/Program Files/Mozilla Firefox/firefox.exe");  
        WebDriver driver = new FirefoxDriver();
        Navigation navigation = driver.navigate();
        navigation.to("http://www.baidu.com/");
        System.out.println("start firefox browser succeed...");        
    }

3、啟動firefox時加載插件:

首先,要知道我們?yōu)槭裁葱枰虞d插件?原因是webdriver在啟動瀏覽器時,啟動的一個干凈的沒有任務、插件及cookies信息的瀏覽器(即使你本機的firefox安裝了某些插件,webdriver啟動firefox也是沒有這些插件的),但是有可能被測系統(tǒng)本身需要插件或者需要調(diào)試等等,此時可以用如下方法
在啟動firefox時加載插件,下面示例加載firebug插件:

public static void StartFireFoxLoadPlugin(){
       System.out.println("start firefox browser...");
       System.setProperty("webdriver.firefox.bin", 
               "D:/Program Files/Mozilla Firefox/firefox.exe");
       File file = new File("files/firebug-2.0.7-fx.xpi");
       FirefoxProfile profile = new FirefoxProfile();
       try {
           profile.addExtension(file);
       } catch (IOException e) {
           e.printStackTrace();
       }
       profile.setPreference("extensions.firebug.currentVersion", "2.0.7");
       //active firebug extensions
       profile.setPreference("extensions.firebug.allPagesActivation", "on");    
       WebDriver driver = new FirefoxDriver(profile);
       driver.get("http://www.baidu.com");
       System.out.println("start firefox browser succeed...");    
   }

4、啟動firefox時設置profile:

上面提到過webdriver啟動firefox時是啟動一個完全新的瀏覽器,我們除了可以使用上面提到的方法定制插件,webdriver還可以對profile進行定制(在firefox地址欄中輸入[about:config
,可以查看firefox的參數(shù)),下面設置代理和默認下載路徑:

public static void StartFireFoxByProxy(){
        String proxyIp = "10.17.171.11";
        int proxyPort = 8080;
        System.out.println("start firefox browser...");
        System.setProperty("webdriver.firefox.bin", 
                "D:/Program Files/Mozilla Firefox/firefox.exe");
        
        FirefoxProfile profile = new FirefoxProfile();
        //設置代理參數(shù)
        profile.setPreference("network.proxy.type", 1);
        profile.setPreference("network.proxy.http", proxyIp);
        profile.setPreference("network.proxy.http_port", proxyPort);
        
        //設置默認下載路徑
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.dir", "D:\\");
        
        WebDriver driver = new FirefoxDriver(profile);
        driver.get("http://www.baidu.com");
        
        System.out.println("start firefox browser succeed...");    
    }

5、啟動本機器的firefox配置:
  每次啟動如果都像上面那樣在代碼里面配置profile比較麻煩,可以使用下面的方法啟動本機器的firefox的配置,換句話說就是我們可以事先配置本機的firefox然后用webdriver啟動它,這樣本機上的firefox安裝了什么插件都可以直接使用了,不需要在配置profile:

public static void StartLocalFirefox(){
        System.out.println("start firefox browser...");
        System.setProperty("webdriver.firefox.bin", 
                "D:/Program Files/Mozilla Firefox/firefox.exe");
        ProfilesIni pi = new ProfilesIni();
        FirefoxProfile profile = pi.getProfile("default");
        WebDriver driver = new FirefoxDriver(profile);
        driver.get("http://www.baidu.com/");
        System.out.println("start firefox browser succeed...");    
    }

6、如果在機器B上要啟動機器A上的firefox配置,可以先導出A的配置,然后加載:
1、將A機器上的Profiles文件夾”C:\Users\cloudchen\AppData\Local\Mozilla\Firefox\Profiles”給拷貝出來到某個目錄
2、代碼:

public static void StartFireFoxByOtherConfig(){
        System.out.println("start firefox browser...");
        System.setProperty("webdriver.firefox.bin", 
                "D:/Program Files/Mozilla Firefox/firefox.exe");        
        File file = new File("files\\lg6mie1i.default");        //profiles文件目錄,這里我是放在工程目錄下的files文件夾下
        FirefoxProfile profile = new FirefoxProfile(file);    
        WebDriver driver = new FirefoxDriver(profile);
        driver.get("http://www.baidu.com");        
        System.out.println("start firefox browser succeed...");    
    }

PS:如果插件或其它東東未加載成功,可以檢查下profile文件夾下是否包含插件信息。

三、啟動chrome瀏覽器

1、啟動chrome需要chromedriver的驅(qū)動:

public static void StartChrome(){
        System.out.println("start firefox browser...");        
        System.setProperty("webdriver.chrome.driver", "files\\chromedriver.exe");  //指定驅(qū)動路徑
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.baidu.com/");
        System.out.println("start firefox browser succeed...");        
    }

另,如果不想用setProperty的方式,可以將chromedriver.exe 放在”C:\Windows\System32”路徑下或者path可以找到的路徑下并重啟電腦即可。
2、加載插件:

public static void StartChromeLoadPlugin(){
        System.out.println("start firefox browser...");
        System.setProperty("webdriver.chrome.driver", "files\\chromedriver.exe");
        File file = new File ("files\\youtube.crx");
        ChromeOptions options = new ChromeOptions();
        options.addExtensions(file);
        WebDriver driver = new ChromeDriver(options);
        driver.get("http://www.baidu.com/");
        System.out.println("start firefox browser succeed...");    
    }

3、設置profile: 未完待續(xù) ...

四、啟動IE瀏覽器

1、IE啟動和chrome類似也需要下載相應的驅(qū)動:

public static void StartIE(){
        System.out.println("start firefox browser...");        
        System.setProperty("webdriver.ie.driver", "files\\IEDriverServer.exe");
        WebDriver driver = new InternetExplorerDriver();
        driver.get("http://www.baidu.com/");
        System.out.println("start firefox browser succeed...");        
    }

2、IE下沒有插件加載
3、IE的放大比例為要設置100%
4、啟動IE時,需關閉如下圖中4個區(qū)域的保護模式:



5、對于第4點提到的關閉保護模式,還可以使用代碼關閉:

//啟動IE瀏覽器并關閉保護模式
    public static void StartIEAndCloseProtectedMode(){
        System.out.println("start firefox browser...");        
        System.setProperty("webdriver.ie.driver", "files\\IEDriverServer.exe");
        DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
        dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
    
        //IE默認啟動保護模式,要么手動在瀏覽器的設置中關閉保護模式,要么在代碼中加上這一句,即可
        dc.setCapability("ignoreProtectedModeSettings", true);
        WebDriver driver = new InternetExplorerDriver(dc);
        driver.get("http://www.baidu.com/");
        System.out.println("start firefox browser succeed...");        
    }
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容