一、SELENIUM2啟動瀏覽器
注意: SELENIUM2在啟動瀏覽器時,都是啟動一個干凈的沒有任務 插件及cookies信息的瀏覽器,即使是你之前的瀏覽器有設置過代理,到自動化啟動時,也是沒有代理的模式。
1.啟動firefox瀏覽器:建議使用32.x版本的火狐瀏覽器,要不有可能會出問題。
啟動不在默認安裝路徑的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 stub
launchChrome();
}
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;
}
}
最后打個廣告,不要介意哦~
最近我在Dataguru學了《軟件自動化測試Selenium2》網絡課程,挺不錯的,你可以來看看!要是想報名,可以用我的優惠碼 G863,立減你50%的固定學費!
鏈接:http://www.dataguru.cn/invite.php?invitecode=G863













