配置Selenium +无头驱动程序+ maven

时间:2016-07-27 14:02:09

标签: java maven selenium selenium-webdriver

我一直在本地运行我的Selenium测试(用Java编写),现在我想无头地运行它们。我得到Xvfb并尝试配置我的pom.xml但是我得到了这个异常

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test (default-test) on project test-suite: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test failed: There was an error in the forked process
[ERROR] org.testng.TestNGException:
[ERROR] An error occurred while instantiating class com.firefox.steps.SharedDriver: null
[ERROR] at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:391)
[ERROR] at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:293)
[ERROR] at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:115)
[ERROR] at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:200)
[ERROR] at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
[ERROR] at org.testng.TestRunner.initMethods(TestRunner.java:409)
[ERROR] at org.testng.TestRunner.init(TestRunner.java:235)
[ERROR] at org.testng.TestRunner.init(TestRunner.java:205)
[ERROR] at org.testng.TestRunner.<init>(TestRunner.java:153)
[ERROR] at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:536)
[ERROR] at org.testng.SuiteRunner.init(SuiteRunner.java:159)
[ERROR] at org.testng.SuiteRunner.<init>(SuiteRunner.java:113)
[ERROR] at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
[ERROR] at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
[ERROR] at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
[ERROR] at org.testng.TestNG.run(TestNG.java:1057)
[ERROR] at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:295)
[ERROR] at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:84)
[ERROR] at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:90)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
[ERROR] Caused by: java.lang.ExceptionInInitializerError
[ERROR] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[ERROR] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
[ERROR] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[ERROR] at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
[ERROR] at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
[ERROR] at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:380)
[ERROR] ... 21 more
[ERROR] Caused by: org.openqa.selenium.WebDriverException: Unable to bind to locking port 7054 within 45000 ms
[ERROR] Build info: version: '2.53.1', revision: 'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09'
[ERROR] System info: host: 'crunchbang', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.16-0.bpo.2-amd64', java.version: '1.8.0_60'
[ERROR] Driver info: driver.version: SharedDriver
[ERROR] at org.openqa.selenium.internal.SocketLock.lock(SocketLock.java:100)
[ERROR] at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:80)
[ERROR] at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:271)
[ERROR] at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:119)
[ERROR] at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:216)
[ERROR] at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:211)
[ERROR] at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:207)
[ERROR] at com.firefox.steps.SharedDriver.<clinit>(SharedDriver.java:23)
[ERROR] ... 27 more
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

我的pom.xml的一部分

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </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>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
        <systemPropertyVariables>
            <homepage>${ADDRESS}</homepage>
            <display.props>${displayProps}</display.props>
        </systemPropertyVariables>
        <suiteXmlFiles>
            <suiteXmlFile>src/test/resources/testng/testng.xml</suiteXmlFile>
        </suiteXmlFiles>
    </configuration>
</plugin>

我用maven clean install运行它...

1 个答案:

答案 0 :(得分:0)

你应该使用PhantomJSDriver进行无头硒测试。我还建议使用install-phantomjs-plugin maven插件以独立于机器的方式运行。