1. 程式人生 > >啟動帶有用戶配置信息的FireFox瀏覽器

啟動帶有用戶配置信息的FireFox瀏覽器

調用 ons () utf8 啟動 oca get 定義 blog

今天自己寫了一個簡單的登錄HTML網頁,在用Selenium+FireFox調用時發現瀏覽器出現了亂碼,之後百度發現是因為瀏覽器設置的編碼格式不是“UTF-8”,之後修改瀏覽器編碼為“UTF-8”之後,再次運行程序發現還是亂碼,但是直接打開瀏覽器瀏覽網頁,字符編碼已經正常了,之後查詢了資料才知道是由於WebDriver啟動FireFox瀏覽器時會啟動全新的FireFox瀏覽器窗口,導致當前機器用戶的瀏覽器配置信息均在測試中無法被使用,要解決這一問題,就需要為使用指定的配置文件來啟動FireFox瀏覽器窗口,下面我們就來碩碩如何配置屬於自己的瀏覽器。

1)生成自定義的Firefox瀏覽器配置文件

1、打開CMD窗口

2、使用CD命令進入firefox.exe的所在目錄,並輸入命令firefox.exe -ProfileManager -no -remote

技術分享

3、輸入命令之後會顯示FireFox的“選擇用戶配置文件”對話框,單擊“Create Profile”按鈕,創建屬於自己的配置文件:

技術分享 技術分享

4、創建OK之後,選擇自己創建的配置文件名,我這裏配置的是Default User,所以我選擇Default User之後,點擊"Start Firefox"

5、編寫Selenium代碼,加載創建的配置文件

package com.testng.webdriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class CreateFireProfile {
	WebDriver driver;
	String baseUrl;
	
	@BeforeMethod
	public void beforeMethod()
	{
		baseUrl = "http://localhost:8080/Example1.html";
	}
	
	@Test
	public void testFirefoxProfile()
	{
		  
		ProfilesIni allProfiles = new ProfilesIni();
		FirefoxProfile profile = allProfiles.getProfile("WebDriver");
		profile.setPreference("browser.fixup.use-utf8", true);
		profile.setPreference("intl.charsetmenu.browser.cache", "UTF-8");
		profile.setPreference("intl.charsetmenu.browser.static", "UTF-8");
		profile.setPreference("intl.charsetmenu.browser.unicode", "UTF-8");
		profile.setPreference("intl.charsetmenu.mailedit", "UTF-8");
		profile.setPreference("prefs.converted-to-utf8", "UTF-8");
		
		driver = new FirefoxDriver(profile);
		
		driver.manage().window().maximize();
		driver.get(baseUrl);
		 
	}

}

修改瀏覽器的默認字符編碼,未修改成功,後續待補充。

啟動帶有用戶配置信息的FireFox瀏覽器