Selenium2——profile設定、啟動Firefox瀏覽器
阿新 • • 發佈:2019-01-23
自學Selenium2 ( WebDriver ),理論和實踐的差距還是很大的,所以學習任何程式語言、工具,實踐是最好的老師。
進入正題,這篇文章講述,在自動化測試時,對Firefox瀏覽器的profile設定、啟動有所不同,需根據自己情況進行相應修改。
1 Firefox的profie設定
自動化測試時,有可能會遇到下載檔案的情況,如下圖;目前Selenium2還無法處理這樣的對話方塊,但可通過對Firefox的profile預先進行設定達到自動下載的效果。
1.1 建立FirefoxProfile物件
FirefoxProfile profile = new FirefoxProfile();
1.2 設定下載路徑
// 設定是否詢問下載位置(可忽略);預設true——不詢問,直接下載到指定路徑,具體設定見browser.folderList,false——詢問 profile.setPreference("browser.download.useDownloadDir",true); // 指定下載位置 profile.setPreference("browser.download.downloadDir", "c:\\OutputFolder"); // 設定下載方式;0——下載到桌面,預設1——下載到Firefox預設位置,2——下載到指定位置 profile.setPreference("browser.download.folderList", 2); // 當一個下載開始時,設定是否顯示下載管理器;預設true——顯示,flase——不顯示 profile.setPreference("browser.download.manager.showWhenStarting",false);
2 設定無需確認即可下載的檔案格式
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream, application/vnd.ms-excel, text/csv, application/zip,application/xml");
注意:
MIME型別是設定某種拓展名的檔案用一種應用程式開啟的方式型別。
常見檔案的MIME型別:
.txt | text/plain 或 text/x-log |
application/pdf | |
.csv | text/csv |
.xls | application/vnd.ms-excel |
.doc | application/msword |
.zip | application/zip |
.xml | application/xml |
.rar | application/octet-stream |
.exe | application/octet-stream |
.gif | image/gif |
.jpeg | image/jpeg |
.html | text/html |
有時,需要的檔案無法在搜尋引擎上查詢到其對應檔案型別的MIME型別,可在瀏覽器中檢視,如Firefox瀏覽器的工具欄 -> 選項 -> 應用程式。
3 啟動Firefox時載入外掛
WebDriver啟動的是一個乾淨的沒有任務、外掛、cookies資訊的Firefox瀏覽器(即使本機Firefox安裝某些外掛),但在自動化測試中可能需要外掛(如Firebug)進行除錯。
注意:需要下載firebug.xpi,且最好使用非Firefox瀏覽器下載,不然提示直接安裝到Firefox;最好不要在Firebug官網中下載,因為提示你需要使用Firefox瀏覽器。
// 定義外掛所在位置
File file = new File("E:\\Firefox\\files\\firebug-2.0.17.xpi");
// 建立一個FirefoxProfile物件profile
FirefoxProfile profile = new FirefoxProfile();
try{
// 將Firebug載入到profile物件中
profile.addExtension(file);
}catch (IOException e){
e.printStackTrace();
}
// 設定Firebug的當前版本號
profile.setPreference("extensions.firebug.currentVersion","2.0.17");
// 啟用Firebug
profile.setPreference("extensions.firebug.allPagesActivation","on");
4 啟動Firefox瀏覽器
4.1 Firefox安裝在預設路徑下
直接建立一個FirefoxDriver物件。
WebDriver driver = new FirefoxDriver();
4.2 Firefox未安裝在預設路徑下
需要指定Firefox的可執行程式firefox.exe的路徑,再建立FirefoxDriver物件。
System.setProperty("webdriver.firefox.bin", "E:\\Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
結合起來,就是如下程式碼:
File file = new File("E:\\Firefox\\files\\firebug-2.0.17.xpi");
FirefoxProfile profile = new FirefoxProfile();
try{
profile.addExtension(file);
} catch (IOException e){
e.printStackTrace();
}
profile.setPreference("extensions.firebug.currentVersion","2.0.17");
profile.setPreference("extensions.firebug.allPagesActivation","on");
profile.setPreference("browser.download.downloadDir","C:\\Output");
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream, application/vnd.ms-excel, text/csv, application/zip,application/xml");
System.setPreperty("webdriver.firefox.bin","E:\\Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();