如何使用无头浏览器通过 Jenkins 在虚拟服务器 space 中运行 Selenium Webdriver 测试用例(Maven 项目)?

How can I run Selenium Webdiver test cases(Maven Project) in virtual server space through Jenkins with Headless browser?

我有一个基于 maven.It 的项目在本地机器上使用 mvn clean install exec:java 命令正确运行。

这是我的 Pom.xml 运行 maven 项目。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>AutomationMaven</groupId>
    <artifactId>AutomationMaven</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>AutomationMaven</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.43.1</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.8</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.4</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>2.43.1</version>
        </dependency>
    </dependencies>
    <build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                     <configuration>
                             <source>1.7</source>
                             <target>1.7</target>
                             <encoding>${project.build.sourceEncoding}</encoding>
                             <showDeprecation>true</showDeprecation>
                             <showWarnings>true</showWarnings>
                             <executable>${env.JAVA_HOME}/bin/javac</executable>
                             <fork>true</fork>
                     </configuration>
                </plugin>
                <plugin>  
                       <groupId>org.codehaus.mojo</groupId>  
                       <artifactId>exec-maven-plugin</artifactId>  
                       <version>1.2.1</version>  
                       <configuration>  
                          <mainClass>com.mainClass</mainClass>  
                       </configuration>  
                 </plugin>  
         </plugins>
    </pluginManagement>
    </build>
</project>

我已经为它设置了"GIT repository",与"Jenkins Job"一致。

我在 "Jenkins" 的工作中使用了这个参数 - JDK 7 - Xvfb 设置(需要有关配置的更多信息) - maven 命令 = mvn clean install exec:java

我有不同的服务器来运行 Maven 项目(Selenium 测试)。

我在服务器上运行项目时遇到跟踪跟踪(错误):

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"mcHeaderLink"}
Command duration or timeout: 30.03 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.43.1', revision: '5163bceef1bc36d43f3dc0b83c88998168a363a0', time: '2014-09-10 09:43:55'
System info: host: 'ip-10-120-0-202', ip: '10.120.0.202', os.name: 'Linux', os.arch: 'amd64', os.version: '3.2.0-58-virtual', java.version: '1.7.0_72'
Session ID: 6c37455f-1dbe-4a8f-acd4-a2b21ae6ec00
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=LINUX, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=false, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=26.0}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:393)
    at org.openqa.selenium.By$ById.findElement(By.java:214)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:59)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:37)
    at com.sun.proxy.$Proxy22.click(Unknown Source) 

很难说为什么它不起作用,因为您对您的应用程序只字不提。但是 "NoSuchElementException" 表明测试是 运行 并且找不到所需的元素。

查看您的应用程序日志文件并检查它是否真的 运行。

如果出现错误,您可以转储屏幕截图以查看您的应用程序状态。 见 Take a screenshot with Selenium WebDriver

还有另一种方法可以帮助您。

但是您需要专用服务器 space 和 运行 显示您的 selenium 脚本。 javascript 操作需要显示。

您必须先使用您的存储库配置 jenkins 作业。

获得存储库后,您必须在 mvn exec:java 的帮助下 运行 该项目。

请使用 maven-compiler-plugin 确认您在 jenkins 中的 JRE 版本。应该是一样的。

一旦您能够成功 运行 jenkins 作业,您可以在您的 jenkins 控制台日志中查看它。