PHPUnit + Selenium:如何设置 Firefox about:config 选项? [英] PHPUnit + Selenium: How to set Firefox about:config options?

查看:34
本文介绍了PHPUnit + Selenium:如何设置 Firefox about:config 选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 PHPUnit 和 Firefox 远程运行 Selenium 测试时,onChange 事件 不会像用户一样被触发正在操作浏览器.

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

对此的解决方案似乎是在Firefox的首选项中将focusmanager.testmode选项设置为true(即about:config),正如 Selenium 错误报告中的建议.

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.

但是,所有示例都直接使用 Selenium,而我使用的是 PHPUnit,它有自己的 API,隐藏了 Selenium 内部.我不知道如何使用 PHPUnit 设置这个 Firefox 选项,所以我希望其他人能告诉我如何做到这一点!

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!

(不,我不能进入 about:config 并自己手动设置它,因为每次运行测试时测试都会创建一个新的干净浏览器配置文件,因此任何手动配置更改都会丢失.)

(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.)

推荐答案

感谢 Selenium 开发人员,我有一个解决方案!

Thanks to the Selenium developers I have a solution!

将其放入您的测试中,以便在 setUp() 函数中调用它:

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));
}

这会将 focusmanager.testmode 设置为 true.

您需要使用您想要设置的首选项创建自己的迷你 Firefox 配置文件,并在测试开始时传递它.操作方法如下:

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. 创建一个新文件夹并将所需的文件放入 Firefox 配置文件中.这可以是任何东西(书签、扩展、您自己的个人资料的副本等),但我们需要的是一个名为 prefs.js 的文件,它存储我们的 about:config 设置.

  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.

在此文件夹中创建 prefs.js,内容如下:

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

user_pref("focusmanager.testmode", true);

  • 压缩文件夹(prefs.js 应该在存档的根目录中),并对其进行 base64 编码.

  • Zip up the folder (prefs.js should be in the root of the archive), and base64 encode it.

    如果你使用的是 Linux,你可以这样做:

    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
    

    然后按照上面的简短版本,取 base64 值并将其设置为firefox_profile"功能.

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

    这篇关于PHPUnit + Selenium:如何设置 Firefox about:config 选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆