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

前端 未结 1 1524
情话喂你
情话喂你 2021-01-01 05:35

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 s

1条回答
  •  生来不讨喜
    2021-01-01 05:53

    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.

    0 讨论(0)
提交回复
热议问题