本文主要記錄下在使用selenium2/webdriver時啟動各種瀏覽器的方法蕴潦、以及如何加載插件像啼、定制瀏覽器信息(設置profile)等
一、Driver下載地址:
http://docs.seleniumhq.org/download/
二品擎、啟動firefox瀏覽器
(不需要下載驅(qū)動埋合,原生支持)
1备徐、firefox安裝在默認路徑下:
//啟動默認安裝路徑下的ff
public void StartFireFoxByDefault(){
System.out.println("start firefox browser...");
WebDriver driver = new FirefoxDriver(); //直接new一個FirefoxDriver即可
Navigation navigation = driver.navigate();
navigation.to("http://www.baidu.com/");
System.out.println("start firefox browser succeed...");
}
2萄传、firefox未安裝在默認路徑下:
public static void StartFireFoxNotByDefault(){
System.out.println("start firefox browser...");
System.setProperty("webdriver.firefox.bin", //指定firefox的安裝路徑
"D:/Program Files/Mozilla Firefox/firefox.exe");
WebDriver driver = new FirefoxDriver();
Navigation navigation = driver.navigate();
navigation.to("http://www.baidu.com/");
System.out.println("start firefox browser succeed...");
}
3、啟動firefox時加載插件:
首先秀菱,要知道我們?yōu)槭裁葱枰虞d插件振诬?原因是webdriver在啟動瀏覽器時,啟動的一個干凈的沒有任務、插件及cookies信息的瀏覽器(即使你本機的firefox安裝了某些插件,webdriver啟動firefox也是沒有這些插件的)砰粹,但是有可能被測系統(tǒng)本身需要插件或者需要調(diào)試等等照雁,此時可以用如下方法
在啟動firefox時加載插件,下面示例加載firebug插件:
public static void StartFireFoxLoadPlugin(){
System.out.println("start firefox browser...");
System.setProperty("webdriver.firefox.bin",
"D:/Program Files/Mozilla Firefox/firefox.exe");
File file = new File("files/firebug-2.0.7-fx.xpi");
FirefoxProfile profile = new FirefoxProfile();
try {
profile.addExtension(file);
} catch (IOException e) {
e.printStackTrace();
}
profile.setPreference("extensions.firebug.currentVersion", "2.0.7");
//active firebug extensions
profile.setPreference("extensions.firebug.allPagesActivation", "on");
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://www.baidu.com");
System.out.println("start firefox browser succeed...");
}
4宪萄、啟動firefox時設置profile:
上面提到過webdriver啟動firefox時是啟動一個完全新的瀏覽器,我們除了可以使用上面提到的方法定制插件,webdriver還可以對profile進行定制(在firefox地址欄中輸入[about:config
放闺,可以查看firefox的參數(shù)),下面設置代理和默認下載路徑:
public static void StartFireFoxByProxy(){
String proxyIp = "10.17.171.11";
int proxyPort = 8080;
System.out.println("start firefox browser...");
System.setProperty("webdriver.firefox.bin",
"D:/Program Files/Mozilla Firefox/firefox.exe");
FirefoxProfile profile = new FirefoxProfile();
//設置代理參數(shù)
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", proxyIp);
profile.setPreference("network.proxy.http_port", proxyPort);
//設置默認下載路徑
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "D:\\");
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://www.baidu.com");
System.out.println("start firefox browser succeed...");
}
5缕坎、啟動本機器的firefox配置:
每次啟動如果都像上面那樣在代碼里面配置profile比較麻煩怖侦,可以使用下面的方法啟動本機器的firefox的配置,換句話說就是我們可以事先配置本機的firefox然后用webdriver啟動它谜叹,這樣本機上的firefox安裝了什么插件都可以直接使用了匾寝,不需要在配置profile:
public static void StartLocalFirefox(){
System.out.println("start firefox browser...");
System.setProperty("webdriver.firefox.bin",
"D:/Program Files/Mozilla Firefox/firefox.exe");
ProfilesIni pi = new ProfilesIni();
FirefoxProfile profile = pi.getProfile("default");
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://www.baidu.com/");
System.out.println("start firefox browser succeed...");
}
6、如果在機器B上要啟動機器A上的firefox配置荷腊,可以先導出A的配置艳悔,然后加載:
1、將A機器上的Profiles文件夾”C:\Users\cloudchen\AppData\Local\Mozilla\Firefox\Profiles”給拷貝出來到某個目錄
2停局、代碼:
public static void StartFireFoxByOtherConfig(){
System.out.println("start firefox browser...");
System.setProperty("webdriver.firefox.bin",
"D:/Program Files/Mozilla Firefox/firefox.exe");
File file = new File("files\\lg6mie1i.default"); //profiles文件目錄很钓,這里我是放在工程目錄下的files文件夾下
FirefoxProfile profile = new FirefoxProfile(file);
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://www.baidu.com");
System.out.println("start firefox browser succeed...");
}
PS:如果插件或其它東東未加載成功,可以檢查下profile文件夾下是否包含插件信息董栽。
三码倦、啟動chrome瀏覽器
1、啟動chrome需要chromedriver的驅(qū)動:
public static void StartChrome(){
System.out.println("start firefox browser...");
System.setProperty("webdriver.chrome.driver", "files\\chromedriver.exe"); //指定驅(qū)動路徑
WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com/");
System.out.println("start firefox browser succeed...");
}
另锭碳,如果不想用setProperty的方式袁稽,可以將chromedriver.exe 放在”C:\Windows\System32”路徑下或者path可以找到的路徑下并重啟電腦即可。
2擒抛、加載插件:
public static void StartChromeLoadPlugin(){
System.out.println("start firefox browser...");
System.setProperty("webdriver.chrome.driver", "files\\chromedriver.exe");
File file = new File ("files\\youtube.crx");
ChromeOptions options = new ChromeOptions();
options.addExtensions(file);
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.baidu.com/");
System.out.println("start firefox browser succeed...");
}
3推汽、設置profile: 未完待續(xù) ...
四、啟動IE瀏覽器
1歧沪、IE啟動和chrome類似也需要下載相應的驅(qū)動:
public static void StartIE(){
System.out.println("start firefox browser...");
System.setProperty("webdriver.ie.driver", "files\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("http://www.baidu.com/");
System.out.println("start firefox browser succeed...");
}
2歹撒、IE下沒有插件加載
3、IE的放大比例為要設置100%
4诊胞、啟動IE時暖夭,需關閉如下圖中4個區(qū)域的保護模式:
5、對于第4點提到的關閉保護模式,還可以使用代碼關閉:
//啟動IE瀏覽器并關閉保護模式
public static void StartIEAndCloseProtectedMode(){
System.out.println("start firefox browser...");
System.setProperty("webdriver.ie.driver", "files\\IEDriverServer.exe");
DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
//IE默認啟動保護模式迈着,要么手動在瀏覽器的設置中關閉保護模式竭望,要么在代碼中加上這一句,即可
dc.setCapability("ignoreProtectedModeSettings", true);
WebDriver driver = new InternetExplorerDriver(dc);
driver.get("http://www.baidu.com/");
System.out.println("start firefox browser succeed...");
}