2012-02-13 79 views
8

我使用Ubuntu 11.04和硒2.9.0下面是它是如何配置我的根POM:如何配置硒webdriver使用自定义Firefox设置进行测试?

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

当attemting运行测试,我得到一个异常:

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

由于就像我用google搜索它一样,问题在于硒使用的firefox驱动与浏览器版本不兼容。考虑到firefox发布的频繁更新,维护我的本地测试环境将会很困难。

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

所以,我不知道如何设置我的硒配置,以使其与静态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。

回答

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); 
+0

是有办法做到这一点使用RemoteWebDriver API?我没有提到,但我们使用spring来实例化网络驱动程序,因此上述情况很难发生。另一种方法是对每个驱动程序使用带有弹簧的工厂和工厂方法,而对于二进制路径属性的名称和值则使用2个属性,因此每个特定的驱动程序将由工厂进行适当配置。不过,如果可能的话,我会优先采用更少的代码来做到这一点。 – 2012-02-13 14:24:10

+0

RemoteWebDriver对我来说相当新颖。你能编辑这个问题并告诉我你如何初始化RemoteWebDriver给我?我希望我能找到一些东西。但是反正 - FirefoxDriver是RemoteWebDriver的子类... – 2012-02-13 14:56:15

+0

我重温了这个问题 - 现在是基本硒测试类的代码。我打算基于浏览器名称删除基于重新初始化的一些服务定位器逻辑,因此pom.xml属性将成为浏览器名称,如果需要,还会使用二进制路径。工厂方法将检查是否提供了二进制路径,并将适当地创建和配置具体的驱动程序对象。 – 2012-02-13 15:28:43

相关问题