Firefox Profile是用來指定firefox的設定檔,透過profile我們可以用來停用部分browser功能來實現自動化測試的功能,一般selenium會自動建立一個新的Firefox Profile,這個profile預設是不能用來下載檔案的,如果你用webdriver來點擊下載檔案的功能,那麼頁面就會跳出一個確認視窗,然後頁面就會卡在那裏,這時我們就可以透過profile的設定,讓下載檔案這件事不需要點擊確認。
如何建立一個profile呢,首先你可以去看Mozilla官方文件
這裏我建立好一個profile叫“test_profile”,你可以在地址欄輸入about:config
, 就能打開Firefox的所有設定,因爲我想停用檔案下載的確認視窗,那個功能設定值爲“browser.helperApps.neverAsk.saveToDisk”,找到這個值,然後設定允許下載的檔案類型,例如Excel檔爲“application/vnd.ms-excel”。
接着程序要設定“webdriver.firefox.profile”的值,指定我們要用的Profile名稱即可
$caps = DesiredCapabilities::firefox();
$caps->setCapability("webdriver.firefox.profile", "test_profile");
$driver = RemoteWebDriver::create("selenium url xxx", $caps, 5000);
另外Facebook webdriver也提供了另一個方式,可以即時設定Firefox Profile。
$profile = new FirefoxProfile();
$profile->setPreference(
'browser.helperApps.neverAsk.saveToDisk',
'application/vnd.ms-excel'
);
$profile->addExtension('firebug-2.0.1.xpi');
$caps = DesiredCapabilities::firefox();
$caps->setCapability(FirefoxDriver::PROFILE, $profile);
seleniumUrl, $caps, 5000);