Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit + Selenium: How to set Firefox about:config options?

When running Selenium tests remotely with PHPUnit and Firefox, onChange events are not fired as they are when a user is operating the browser.

The solution to this seems to be to set the focusmanager.testmode option to true in Firefox's preferences (i.e. about:config), as suggested in a Selenium bug report.

However all the examples are using Selenium directly, while I am using PHPUnit which has its own API hiding the Selenium internals. I can't figure out how to set this Firefox option using PHPUnit, so I'm hoping someone else can tell me how this can be done!

(No, I can't go into about:config and set it myself manually because the tests create a new clean browser profile each time the tests are run, so any manual config changes are lost.)

like image 909
Malvineous Avatar asked Feb 13 '23 19:02

Malvineous


1 Answers

Thanks to the Selenium developers I have a solution!

Short version

Put this in your test so that it gets called in the setUp() function:

// Firefox mini-profile that sets focusmanager.testmode=true in about:config
define('FIREFOX_PROFILE',
'UEsDBAoAAAAAADqAxkSBK46tKgAAACoAAAAIABwAcHJlZnMuanNVVAkAA1BZkVM6WZFTdXgLAAEE
6AMAAARkAAAAdXNlcl9wcmVmKCJmb2N1c21hbmFnZXIudGVzdG1vZGUiLCB0cnVlKTsKUEsBAh4D
CgAAAAAAOoDGRIErjq0qAAAAKgAAAAgAGAAAAAAAAQAAAKSBAAAAAHByZWZzLmpzVVQFAANQWZFT
dXgLAAEE6AMAAARkAAAAUEsFBgAAAAABAAEATgAAAGwAAAAAAA==');

protected function setUp()
{
    $this->setDesiredCapabilities(Array('firefox_profile' => FIREFOX_PROFILE));
}

This sets focusmanager.testmode to true.

Long version

You need to create your own mini Firefox profile with the preferences you want set, and pass it along at the start of your tests. Here's how to do it:

  1. Create a new folder and put the files you want in the Firefox profile in there. This can be anything (bookmarks, extensions, a copy of your own profile, etc.) but all we need here is a file called prefs.js which stores our about:config settings.

  2. Create prefs.js in this folder with the following content:

    user_pref("focusmanager.testmode", true);
    
  3. Zip up the folder (prefs.js should be in the root of the archive), and base64 encode it.

If you're using Linux, you can do it all like this:

mkdir firefox-profile
cd firefox-profile
echo 'user_pref("focusmanager.testmode", true);' >> prefs.js
zip -r ../firefox-profile.zip *
base64 < ../firefox-profile.zip

Then take the base64 value and set it as the "firefox_profile" capability as per the short version above.

like image 136
Malvineous Avatar answered Mar 24 '23 15:03

Malvineous