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

标签 java selenium webdriver ubuntu-11.04

我正在使用 Ubuntu 11.04 和 selenium 2.9.0 以下是它在我的根 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 二进制文件的路径?我想知道是否还需要其他任何东西。

** 编辑

我正在使用配置属性来初始化正确的网络驱动程序:

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。

最佳答案

只要知道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);

关于java - 如何配置 selenium webdriver 以使用自定义 firefox 设置进行测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9261133/

相关文章:

python - Selenium Firefox 驱动程序引发异常 OSError : [Errno 8] Exec format error

java - 如何在 Eclipse 调试下更改变量或表达式 View 中 Java 对象的字符串表示形式

java - 如何概括这个具体类?

python-3.x - cookie sqlite错误1555如何解决? ( python 和 Selenium )

java - 为什么在搜索多个元素时无法在 Selenium 中按 id 找到一个元素

firefox - Firefox 中的 Selenium "Element is not clickable at point"错误

java - 使用javax发送电子邮件?

java - 从 hibernate 检索类会抛出 nullpointerException

java - 如何让 webdriver 在使用 css、xpath、标签、链接或元素 id 之间切换

ruby-on-rails - 在 Windows 10 机器上运行的应用找不到文件