事實證明,「啓動服務器」和「停止服務器」的目標是啓動/停止SeleniumRC服務器。這是不是我想要的,因爲我所有的測試都使用WebDriver API。
顯然,pom DOES中的'xvfb'目標在指定的生命週期階段開始一個Xvfb會話 - 我想我以前沒有看到它。在它的配置中,您可以指定在哪裏編寫一個道具文件,詳細說明Xvfb運行在哪個顯示器上。在Java代碼中,可以讀取該文件,並將值傳遞給創建WebDriver時使用的FirefoxBinary。
相關的pom.xml位如下所示:
<properties>
<displayProps>target/selenium/display.properties</displayProps>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<display.props>${displayProps}</display.props>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>selenium-maven-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>xvfb</id>
<phase>test-compile</phase>
<goals>
<goal>xvfb</goal>
</goals>
<configuration>
<displayPropertiesFile>${displayProps}</displayPropertiesFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
這第一自由顯示器上啓動的Xvfb(:20或以上)並寫入值到道具文件,我讀後來在使用我的Java代碼。
String xvfbPropsFile = System.getProperty("display.props");
FirefoxBinary ffox = new FirefoxBinary();
ffox.setEnvironmentProperty("DISPLAY", /*read value from xvfbPropsFile*/);
WebDriver driver = new FirefoxDriver(ffox);
現在驅動程序將控制Firefox實例在適當的顯示器中旋轉。瞧!
我不能運行這個,我還得到一個「無顯示」錯誤:( –
優秀,得益於它救了我的一天。 –
請出示文件的內容'目標/硒/ display.properties' –