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

时间:2012-02-13 13:11:53

标签: java selenium webdriver ubuntu-11.04

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

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

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

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发布的频繁更新,维护我的本地测试环境将很困难。

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

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

**编辑

我正在使用配置属性来初始化正确的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。

1 个答案:

答案 0 :(得分:15)

只要我知道java命令

WebDriver driver = new FirefoxDriver();

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

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

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