在自動化測試過程中,通過selenium啟動瀏覽器時,可能需要加載插件(如測試用的firebug、或產品中要求必須添加某插件等)、讀取用戶數據(自己瀏覽器的配置文件/別人直接給的瀏覽器配置文件)、設置瀏覽器(不加載圖片等)。
由於我們通過selenium啟動的瀏覽器頁面,是完全干凈的頁面,如果想要讓該頁面帶上我們需要的信息,則需要自己設置。
下面講一下Firefox和Chrome瀏覽器的各種啟動方式:
一:Firefox
1.啟動瀏覽器,使用瀏覽器上保存的所有用戶數據。
用戶數據是從Firefox的配置文件中讀取的,首先看下自己電腦的配置文件,在cmd上進入Firefox的安裝路徑下,運行firefox.exe -ProfileManger命令,會彈出自己電腦火狐瀏覽器的所有配置文件。如下圖。
//創建profileIni對象 ProfilesIni ini = new ProfilesIni(); //通過名字來獲取到相應的配置文件,如上圖所示,也可以寫“profile2” FirefoxProfile profile = ini.getProfile("default"); //創建瀏覽器驅動,並將profile傳入,此時啟動的時候,就會讀取我們default配置文件來調用相應的火狐瀏覽器了 WebDriver driver = new FirefoxDriver(profile); driver.get("https://www.cnblogs.com/");
如果是別人傳過來的一個配置文件,你自己保存在了地址XXX下。(即使是讀取自己本地瀏覽器配置文件,也可使用下面的方法,只要將路徑寫對就可以)
//創建文件對象 File file = new File("C:\\your\\path\\yourProfileName"); FirefoxProfile profile = new FirefoxProfile(file); WebDriver driver = new FirefoxDriver(profile); driver.get("https://www.cnblogs.com/");
2.啟動瀏覽器,設置代理
FirefoxProfile profile = new FirefoxProfile(); String proxyIp="192.168.2.111"; int proxyPort = 6666; //開啟代理模式 profile.setPreference("network.proxy.type", 1); //設置代理服務器IP profile.setPreference("network.proxy.http", proxyIp); //設置代理服務器的端口 profile.setPreference("network.proxy.http_port", proxyPort); WebDriver driver = new FirefoxDriver(profile); driver.get("http://www.baidu.com");
如果還想了解其他的設置,可以在火狐瀏覽器搜索就可以了。
3.啟動瀏覽器,安裝插件
//設置自己所下載的插件的位置 File file = new File("D:\\your\\xxx.xpi\\path"); //創建火狐瀏覽器的profile對象 FirefoxProfile profile = new FirefoxProfile(file); //調用addExtension方法,該方法將file路徑中的插件名字提取出來,並將名字和new FileExtension 的對象添加到extensions中 //private Map<String, Extension> extensions = Maps.newHashMap(); profile.addExtension(file); WebDriver driver = new FirefoxDriver(profile); driver.get("https://www.cnblogs.com/");
二:Chrome
關於Chrome的比較簡單,其API上寫的都非常清楚。可以自行參考下:https://sites.google.com/a/chromium.org/chromedriver/capabilities。里面的例子都寫的很明了。
下面簡單舉幾個例子。
1.添加啟動參數(下面是比較常用的字體設置、加載用戶數據、模擬手機模式)---addArguments
ChromeOptions options = new ChromeOptions(); //讀取用戶參數,XX代表當前用戶 options.addArguments("--user-data-dir=C:\\Users\\XX\\AppData\\Local\\Google\\Chrome\\User Data"); //設置語言 options.addArguments("--lang=zh_CN.UTF-8"); //模擬手機模式 options.addArguments("--user-agent=iphone 7"); WebDriver driver = new ChromeDriver(options); driver.get("http://www.baidu.com");
關於設置手機模式的,--user-agent對應的值應該怎么填寫,在這說明下:
打開瀏覽器,按F12,手動設置成手機模式,之后:
關於Chrome瀏覽器啟動參數,我們測試中常用的大概還有:
--disable-images(控制是否加載圖片)
--start-maximized (啟動時最大化)
download.default_directory": download_dir (設置下載保存路徑)
2.修改設置----setExperimentalOption
ChromeOptions options = new ChromeOptions(); Map<String, Object> prefs = new HashMap<String, Object>(); //禁止加載圖片 prefs.put("profile.default_content_setting_values.images", 2); options.setExperimentalOption("prefs", prefs); WebDriver driver= new ChromeDriver(options); driver.get("http://www.baidu.com");
3.安裝插件---addExtensions
/* * 安裝插件 */ ChromeOptions options = new ChromeOptions(); options.addExtensions(new File("you/path/XXX")); WebDriver driver = new ChromeDriver(options); driver.get("http://www.baidu.com");
4.設置代理---DesiredCapabilities
//設置一個空的能力對象 DesiredCapabilities caps = new DesiredCapabilities(); //設置將要代理的IP和端口號 String proxyIpPort="192.168.2.122:8088"; //設置一個空的協議對象 Proxy proxy=new Proxy(); //設置對象支持http、ftp、ssl協議 proxy.setHttpProxy(proxyIpPort).setFtpProxy(proxyIpPort).setSslProxy(proxyIpPort); //設置能力對象,將proxy對象設置為值 caps.setCapability(CapabilityType.PROXY, proxy); WebDriver driver = new ChromeDriver(caps); driver.get("https://sites.google.com/a/chromium.org/chromedriver/capabilities");
關於Chrome的用戶數據,可以到C:\Users\用戶名\AppData\Local\Google\Chrome\User Data\Default\preferences文件中查看。想要設置的,里面都可以查到。