標籤:
一、SELENIUM2啟動瀏覽器
注意: SELENIUM2在啟動瀏覽器時,都是啟動一個乾淨的沒有任務 外掛程式及cookies資訊的瀏覽器,即使是你之前的瀏覽器有設定過代理,到自動化啟動時,也是沒有代理的模式。
1.啟動firefox瀏覽器:
啟動不在預設安裝路徑的firefox瀏覽器:
2.啟動chrome瀏覽器:
需要chromedriver.exe的支援,驅動(http://docs.seleniumhq.org/download/)
如果不想用setProperty的方式,也可以將chromedriver.exe 放在”C:\Windows\System32”路徑下並重啟電腦即可
3.啟動IE瀏覽器:
需要IEDriverServer.exe 的支援,且 IE的exe檔案分64位與32位,請根據自已的機器選擇相應的exe檔案。
啟動代碼如下:
二、SELENIUM2啟動瀏覽器時載入外掛程式
載入外掛程式的作用:有的是為了調試,也有的是被測試的系統本身需要外掛程式
Firefox下載入firebug外掛程式:
Chrome下載入crx外掛程式:
且IE下沒有外掛程式載入。
三、SELENIUM2啟動firefox時的profile設定
在firefox地址欄中輸入about:config,可以看到有哪些可以設定
舉例:匯出檔案的地址設定
3.啟用預設情況下被firefox禁用的功能,以本地事件例,很簡單直接設定為true就可以了。
特別重要:啟動時,每次都是一個乾淨的firefox,如果要啟動本機器的firefox的配置:
如果在機器B上要啟動機器A上的firefox配置,則先匯出A的配置,然後載入:
將A機器上的Profiles檔案夾”C:\Users\cloudchen\AppData\Local\Mozilla\Firefox\Profiles”給拷貝出來
四、SELENIUM2啟動chrome時的profile設定
特別重要:將user data檔案夾” C:\Users\cloudchen\AppData\Local\Google\Chrome\User Data”拷貝出來
?
請自己驗證一下,加不加以下代碼的區別:
五、SELENIUM2啟動IE時的設定
特別重要:啟動IE時,需關閉保護模式。 4個紅框標記的地區中的保護模式的checkbox 都要uncheck
?也可以代碼關閉:
完整代碼如下:
package com.selenium.test;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebDriver.Navigation;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.firefox.FirefoxProfile;import org.openqa.selenium.ie.InternetExplorerDriver;public class launchBrowser {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stublaunchChrome();}public static void launchFireFox() {FirefoxProfile firefoxProfile = new FirefoxProfile();firefoxProfile.setPreference("browser.download.folderList", 2);firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);firefoxProfile.setPreference("browser.download.dir", "d:\\test");firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");String proxyIp = "child-prc.intel.com";int proxyPort = 913;firefoxProfile.setPreference("network.proxy.type", 1);firefoxProfile.setPreference("network.proxy.http", proxyIp);firefoxProfile.setPreference("network.proxy.http_port", proxyPort);firefoxProfile.setPreference("network.proxy.ssl", proxyIp);firefoxProfile.setPreference("network.proxy.ssl_port", proxyPort);firefoxProfile.setPreference("network.proxy.share_proxy_settings", true);firefoxProfile.setPreference("network.proxy.no_proxies_on", "localhost");WebDriver driver = new FirefoxDriver(firefoxProfile);// WebDriver driver = new FirefoxDriver();Navigation navigation = driver.navigate();navigation.to("https://baidu.com");// driver.close();// driver = null;}public static void launchChrome() {System.setProperty("webdriver.chrome.driver", "files/chromedriver.exe");WebDriver driver = new ChromeDriver();Navigation navigation = driver.navigate();navigation.to("https://www.baidu.com");driver.close();driver = null;}public static void launchIE() {System.setProperty("webdriver.ie.driver", "files/iedriver64.exe");WebDriver driver = new InternetExplorerDriver();Navigation navigation = driver.navigate();navigation.to("https://www.baidu.com");driver.close();driver = null;}}
Selenium2啟動瀏覽器且載入外掛程式