您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
Selenium2/Webdriver啟動各種瀏覽器的方法
作者:網絡轉載 發(fā)布時間:[ 2017/2/15 11:31:39 ] 推薦標簽:功能測試 selenium

  本文主要記錄下在使用selenium2/webdriver時啟動各種瀏覽器的方法、以及如何加載插件、定制瀏覽器信息(設置profile)等
  一、Driver下載地址:
  http://docs.seleniumhq.org/download/
  二、啟動firefox瀏覽器(不需要下載驅動,原生支持)
  1、firefox安裝在默認路徑下:
1     //啟動默認安裝路徑下的ff
2     public void StartFireFoxByDefault(){
3         System.out.println("start firefox browser...");
4         WebDriver driver = new FirefoxDriver();      //直接new一個FirefoxDriver即可
5         Navigation navigation = driver.navigate();
6         navigation.to("http://www.baidu.com/");
7         System.out.println("start firefox browser succeed...");
8     }
  2、firefox未安裝在默認路徑下:
1 public static void StartFireFoxNotByDefault(){
2         System.out.println("start firefox browser...");
3         System.setProperty("webdriver.firefox.bin",     //指定firefox的安裝路徑
4                 "D:/Program Files/Mozilla Firefox/firefox.exe");
5         WebDriver driver = new FirefoxDriver();
6         Navigation navigation = driver.navigate();
7         navigation.to("http://www.baidu.com/");
8         System.out.println("start firefox browser succeed...");
9     }
  3、啟動firefox時加載插件:
  首先,要知道我們?yōu)槭裁葱枰虞d插件?原因是webdriver在啟動瀏覽器時,啟動的一個干凈的沒有任務、插件及cookies信息的瀏覽器(即使你本機的firefox安裝了某些插件,webdriver啟動firefox也是沒有這些插件的),但是有可能被測系統(tǒng)本身需要插件或者需要調試等等,此時可以用如下方法在啟動firefox時加載插件,下面示例加載firebug插件:
1     public static void StartFireFoxLoadPlugin(){
2         System.out.println("start firefox browser...");
3         System.setProperty("webdriver.firefox.bin",
4                 "D:/Program Files/Mozilla Firefox/firefox.exe");
5         File file = new File("files/firebug-2.0.7-fx.xpi");
6         FirefoxProfile profile = new FirefoxProfile();
7         try {
8             profile.addExtension(file);
9         } catch (IOException e) {
10             e.printStackTrace();
11         }
12         profile.setPreference("extensions.firebug.currentVersion", "2.0.7");
13         //active firebug extensions
14         profile.setPreference("extensions.firebug.allPagesActivation", "on");
15         WebDriver driver = new FirefoxDriver(profile);
16         driver.get("http://www.baidu.com");
17         System.out.println("start firefox browser succeed...");
18     }
  4、啟動firefox時設置profile:
  上面提到過webdriver啟動firefox時是啟動一個完全新的瀏覽器,我們除了可以使用上面提到的方法定制插件,webdriver還可以對profile進行定制(在firefox地址欄中輸入about:config,可以查看firefox的參數),下面設置代理和默認下載路徑:
1     public static void StartFireFoxByProxy(){
2         String proxyIp = "10.17.171.11";
3         int proxyPort = 8080;
4         System.out.println("start firefox browser...");
5         System.setProperty("webdriver.firefox.bin",
6                 "D:/Program Files/Mozilla Firefox/firefox.exe");
7
8         FirefoxProfile profile = new FirefoxProfile();
9         //設置代理參數
10         profile.setPreference("network.proxy.type", 1);
11         profile.setPreference("network.proxy.http", proxyIp);
12         profile.setPreference("network.proxy.http_port", proxyPort);
13
14         //設置默認下載路徑
15         profile.setPreference("browser.download.folderList", 2);
16         profile.setPreference("browser.download.dir", "D:\");
17
18         WebDriver driver = new FirefoxDriver(profile);
19         driver.get("http://www.baidu.com");
20
21         System.out.println("start firefox browser succeed...");
22     }
  5、啟動本機器的firefox配置:
  每次啟動如果都像上面那樣在代碼里面配置profile比較麻煩,可以使用下面的方法啟動本機器的firefox的配置,換句話說是我們可以 事先配置本機的firefox然后用webdriver啟動它,這樣本機上的firefox安裝了什么插件都可以直接使用了,不需要在配置 profile:
1     public static void StartLocalFirefox(){
2         System.out.println("start firefox browser...");
3         System.setProperty("webdriver.firefox.bin",
4                 "D:/Program Files/Mozilla Firefox/firefox.exe");
5         ProfilesIni pi = new ProfilesIni();
6         FirefoxProfile profile = pi.getProfile("default");
7         WebDriver driver = new FirefoxDriver(profile);
8         driver.get("http://www.baidu.com/");
9         System.out.println("start firefox browser succeed...");
10     }
  6、如果在機器B上要啟動機器A上的firefox配置,可以先導出A的配置,然后加載:
  1、將A機器上的Profiles文件夾”C:UserscloudchenAppDataLocalMozillaFirefoxProfiles”給拷貝出來到某個目錄
  2、代碼:
1     public static void StartFireFoxByOtherConfig(){
2         System.out.println("start firefox browser...");
3         System.setProperty("webdriver.firefox.bin",
4                 "D:/Program Files/Mozilla Firefox/firefox.exe");
5         File file = new File("files\lg6mie1i.default");        //profiles文件目錄,這里我是放在工程目錄下的files文件夾下
6         FirefoxProfile profile = new FirefoxProfile(file);
7         WebDriver driver = new FirefoxDriver(profile);
8         driver.get("http://www.baidu.com");
9         System.out.println("start firefox browser succeed...");
10     }
  PS:如果插件或其它東東未加載成功,可以檢查下profile文件夾下是否包含插件信息。

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd