Selenium2(WebDriver)總結(一)---啓動瀏覽器、設置profile&加載插件

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

環境搭建可參考個人另外一篇文章:http://www.cnblogs.com/puresoul/p/3483055.htmlweb

1、Driver下載地址chrome

  http://docs.seleniumhq.org/download/瀏覽器

2、啓動firefox瀏覽器(不須要下載驅動,原生支持)cookie

一、firefox安裝在默認路徑下:spa

複製代碼

1     //啓動默認安裝路徑下的ff2     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     }

複製代碼

二、firefox未安裝在默認路徑下: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     }

複製代碼

三、啓動firefox時加載插件:插件

  首先,要知道咱們爲何須要加載插件?緣由是webdriver在啓動瀏覽器時,啓動的一個乾淨的沒有任務、插件及cookies信息的瀏覽器(即便你本機的firefox安裝了某些插件,webdriver啓動firefox也是沒有這些插件的),可是有可能被測系統自己須要插件或者須要調試等等,此時能夠用以下方法在啓動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 extensions14         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     }

複製代碼

 

四、啓動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     }

複製代碼

 五、啓動本機器的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     }

複製代碼

六、若是在機器B上要啓動機器A上的firefox配置,能夠先導出A的配置,而後加載:

一、將A機器上的Profiles文件夾」C:\Users\cloudchen\AppData\Local\Mozilla\Firefox\Profiles」給拷貝出來到某個目錄

二、代碼:

複製代碼

 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文件夾下是否包含插件信息。

 

3、啓動chrome瀏覽器

 一、啓動chrome須要chromedriver的驅動:

複製代碼

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

複製代碼

  另,若是不想用setProperty的方式,能夠將chromedriver.exe 放在」C:\Windows\System32」路徑下或者path能夠找到的路徑下並重啓電腦便可。

二、加載插件:

複製代碼

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

複製代碼

三、設置profile: 未完待續 ...

 

 

4、啓動IE瀏覽器

一、IE啓動和chrome相似也須要下載相應的驅動:

複製代碼

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

複製代碼

二、IE下沒有插件加載

三、IE的放大比例爲要設置100%

四、啓動IE時,需關閉以下圖中4個區域的保護模式:

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

複製代碼

 1     //啓動IE瀏覽器並關閉保護模式 2     public static void StartIEAndCloseProtectedMode(){ 3         System.out.println("start firefox browser...");        
 4         System.setProperty("webdriver.ie.driver", "files\\IEDriverServer.exe"); 5         DesiredCapabilities dc = DesiredCapabilities.internetExplorer(); 6         dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); 7      8         //IE默認啓動保護模式,要麼手動在瀏覽器的設置中關閉保護模式,要麼在代碼中加上這一句,便可 9         dc.setCapability("ignoreProtectedModeSettings", true);10         WebDriver driver = new InternetExplorerDriver(dc);11         driver.get("http://www.baidu.com/");12         System.out.println("start firefox browser succeed...");        
13     }

複製代碼

相關文章
相關標籤/搜索