自學(xué)Selenium2 ( WebDriver ),理論和實(shí)踐的差距還是很大的,所以學(xué)習(xí)任何編程語言、工具,實(shí)踐是最好的老師。 進(jìn)入正題,這篇文章講述,在自動(dòng)化測試時(shí),對(duì)Firefox瀏覽器的profile設(shè)置、啟動(dòng)有所不同,需根據(jù)自己情況進(jìn)行相應(yīng)修改。 1 Firefox的profie設(shè)置自動(dòng)化測試時(shí),有可能會(huì)遇到下載文件的情況,如下圖;目前Selenium2還無法處理這樣的對(duì)話框,但可通過對(duì)Firefox的profile預(yù)先進(jìn)行設(shè)置達(dá)到自動(dòng)下載的效果。 1.1 創(chuàng)建FirefoxProfile對(duì)象 FirefoxProfile profile = new FirefoxProfile();
1.2 設(shè)置下載路徑 // 設(shè)置是否詢問下載位置(可忽略);默認(rèn)true——不詢問,直接下載到指定路徑,具體設(shè)置見browser.folderList,false——詢問 profile.setPreference("browser.download.useDownloadDir",true); // 指定下載位置 profile.setPreference("browser.download.downloadDir", "c:\\OutputFolder"); // 設(shè)置下載方式;0——下載到桌面,默認(rèn)1——下載到Firefox默認(rèn)位置,2——下載到指定位置 profile.setPreference("browser.download.folderList", 2); // 當(dāng)一個(gè)下載開始時(shí),設(shè)置是否顯示下載管理器;默認(rèn)true——顯示,flase——不顯示 profile.setPreference("browser.download.manager.showWhenStarting",false);
2 設(shè)置無需確認(rèn)即可下載的文件格式profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream, application/vnd.ms-excel, text/csv, application/zip,application/xml");
注意: MIME類型是設(shè)置某種拓展名的文件用一種應(yīng)用程序打開的方式類型。 常見文件的MIME類型:
有時(shí),需要的文件無法在搜索引擎上查詢到其對(duì)應(yīng)文件類型的MIME類型,可在瀏覽器中查看,如Firefox瀏覽器的工具欄 -> 選項(xiàng) -> 應(yīng)用程序。 3 啟動(dòng)Firefox時(shí)加載插件WebDriver啟動(dòng)的是一個(gè)干凈的沒有任務(wù)、插件、cookies信息的Firefox瀏覽器(即使本機(jī)Firefox安裝某些插件),但在自動(dòng)化測試中可能需要插件(如Firebug)進(jìn)行調(diào)試。 注意:需要下載firebug.xpi,且最好使用非Firefox瀏覽器下載,不然提示直接安裝到Firefox;最好不要在Firebug官網(wǎng)中下載,因?yàn)樘崾灸阈枰褂肍irefox瀏覽器。 // 定義插件所在位置 File file = new File("E:\\Firefox\\files\\firebug-2.0.17.xpi"); // 創(chuàng)建一個(gè)FirefoxProfile對(duì)象profile FirefoxProfile profile = new FirefoxProfile(); try{ // 將Firebug加載到profile對(duì)象中 profile.addExtension(file); }catch (IOException e){ e.printStackTrace(); } // 設(shè)置Firebug的當(dāng)前版本號(hào) profile.setPreference("extensions.firebug.currentVersion","2.0.17"); // 激活Firebug profile.setPreference("extensions.firebug.allPagesActivation","on"); 4 啟動(dòng)Firefox瀏覽器4.1 Firefox安裝在默認(rèn)路徑下 直接創(chuàng)建一個(gè)FirefoxDriver對(duì)象。 WebDriver driver = new FirefoxDriver();
4.2 Firefox未安裝在默認(rèn)路徑下 需要指定Firefox的可執(zhí)行程序firefox.exe的路徑,再創(chuàng)建FirefoxDriver對(duì)象。 System.setProperty("webdriver.firefox.bin", "E:\\Firefox\\firefox.exe"); WebDriver driver = new FirefoxDriver(); 結(jié)合起來,就是如下代碼: 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();
|
|