如何配置selenium webdriver以使用自定义firefox设置进行测试?

如何配置selenium webdriver以使用自定义firefox设置进行测试?

问题描述:

我正在使用Ubuntu 11.04和selenium 2.9.0以下是我在root pom中的配置方式:

I am using Ubuntu 11.04 and selenium 2.9.0 Here is how it is configured in my root pom:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.9.0</version>
    <scope>test</scope>
</dependency>

当准备运行测试时,我得到一个例外:

When attemting to run a test, I get an exception:

org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
*** LOG addons.xpi: startup
*** LOG addons.xpi: Ignoring file entry whose name is not a valid add-on ID: > /tmp/anonymous3804893394247066972webdriver-profile/extensions/webdriver-staging
*** LOG addons.xpi: checkForChanges
*** LOG addons.xpi: No changes found

    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:95)
    ....

据我搜索它,问题是selenium使用的firefox驱动程序与浏览器版本不兼容。考虑到firefox发布的频繁更新,很难维护我的本地测试环境。

As far as I have googled it, the issue is that the firefox driver that selenium uses is incompatible with the version of the browser. Having in mind the frequent updates firefox releases, it will be difficult to maintain my local test environment.

因此,我决定使用最新的兼容版本安装静态firefox并使用selenium,同时保留我的默认firefox(我不能删除它)。

Therefore, I have decided to install a static firefox with the latest known to me compatible version and to use selenium with it, while preserving my default firefox (I must not remove it).

所以,我不知道如何设置我的selenium配置,以使其与静态firefox一起使用。可能我必须配置我的应用程序以接收驱动程序使用的firefox二进制文件的路径?我想知道是否还需要其他任何东西。

So, I am not sure how to setup my selenium configuration in order to make it work with the static firefox. Probably I must configure my app to receive the path to the firefox binary for the driver to use? I'd like to know if anything else is also needed.

**编辑

我正在使用配置初始化正确的webdriver的属性:

I am using configuration properties to initialize the proper webdriver:

public abstract class SeleniumTestBase {

    ...

    public final void setUp() throws Exception {
        String driverClass = getConfigurationProperty("selenium.webDriverClass");
        driver = (WebDriver) Class.forName(driverClass).newInstance();
        ...
        doSetUp();
    }

    public void doSetUp() {
    }

    ...
}

selenium.webDriverClass属性可由pom.xml管理,因此不同的配置文件可以有不同的价值。目前它是要实例化的驱动程序类的FQN。

The "selenium.webDriverClass" property is manageable by the pom.xml therefore different profiles can have different value. Currently it is the FQN of the driver class to be instantiated.

只要我知道java命令

As long as I know that the java command

WebDriver driver = new FirefoxDriver();

将在您的计算机上运行已安装的Firefox浏览器。

will run the installed Firefox browser on your computer.

但是在 http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html 我意识到可以有办法覆盖它:

but reading the JavaDoc at http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html I realised that there can be way how to override it:

FirefoxBinary binary = new FirefoxBinary(new File("path/to/binary"));
FirefoxProfile profile = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(binary, profile);