配置Jenkins使用Chrome - Selenium 3.0.1 Maven TestNG

时间:2017-01-09 15:34:01

标签: java maven google-chrome selenium jenkins

我一直试图让Jenkins使用Chrome进行自动化测试。当我在本地运行测试时,我能够在TestNG的JDK VM选项中使用Chrome-by -Dbrowser = chrome。

enter image description here

出于某种原因,无论我尝试什么,它似乎都停留在使用Firefox上。

这是我之前的方法

@BeforeMethod(
    alwaysRun = true
)
public static void configureBrowserBeforeTest(Method testMethod) {
    try {
        ((Driver)driver.get()).resetBrowser();
        wait.set(newDefaultWait());
        userAgent = determineUserAgent();
        initialiseNewScreenshotCapture(testMethod);
    } catch (Exception var2) {
        logger.error("Failed to configure browser.", var2);
        throw new RuntimeException("Failed to configure browser.", var2);
    }
}

和POM文件

<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>com.frameworkium</groupId>
    <artifactId>Frameworkium</artifactId>
    <packaging>jar</packaging>
    <version>2.0.5</version>

    <name>Frameworkium</name>
    <description>
        A template designed to get up and running quickly with Selenium and Appium.
    </description>
    <url/>
    <inceptionYear/>
    <organization/>
    <licenses/>

    <developers/>
    <contributors/>

    <prerequisites>
        <maven>3.1.1</maven>
    </prerequisites>

    <modules/>

    <scm/>
    <issueManagement/>
    <ciManagement/>
    <distributionManagement/>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <env.config>local</env.config>
        <threads>1</threads>
        <groups/>
        <aspectj.version>1.8.9</aspectj.version>
    </properties>

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.8</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>com.github.Frameworkium</groupId>
            <artifactId>frameworkium-core</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-remote-driver</artifactId>
            <version>3.0.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.0.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
        <testSourceDirectory>src/test/java</testSourceDirectory>
        <resources>
            <resource>
                <directory>
                    src/main/resources
                </directory>
            </resource>
        </resources>
    </build>
    <profiles>
        <profile>
            <id>tests</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>

                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.5.1</version>
                        <configuration>
                            <source>1.8</source>
                            <target>1.8</target>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.19.1</version>
                        <configuration>
                            <parallel>methods</parallel>
                            <threadCount>${threads}</threadCount>
                            <systemProperties>
                                <screenshotDirectory>
                                    ${project.build.directory}/screenshots
                                </screenshotDirectory>
                                <allure.issues.tracker.pattern>
                                    /browse/%s
                                </allure.issues.tracker.pattern>
                            </systemProperties>
                            <suiteXmlFiles>
                                <suiteXmlFile>testng.xml</suiteXmlFile>
                            </suiteXmlFiles>
                            <includes>
                                <include>**/Test*.java</include>
                                <include>**/*Tests*.java</include>
                                <include>**/*Tests.java</include>
                                <include>**/*Test.java</include>
                                <include>**/*TestCase.java</include>
                            </includes>
                            <groups>${groups}</groups>
                            <testFailureIgnore>false</testFailureIgnore>
                            <argLine>
                                -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                            </argLine>
                        </configuration>
                    </plugin>
                </plugins>

            </build>
        </profile>
    </profiles>
</project>

enter image description here

我确定解决方案很简单,我让Jenkins作为服务运行并使用本地管理员帐户。我已经为jenkins安装了chromedriver插件以及Selenium插件。测试被发现没问题,但是他们使用Firefox和FF版本50似乎不能与Selenium 3.0.1上的gecko驱动程序一起工作,所以我宁愿使用Chrome,我知道工作得很好,但对于我的生活,我不能计算如何告诉詹金斯使用Chrome而不是FF。在此先感谢您的帮助!如果我错过任何事情,我会道歉。

操作系统 - Windows Server 2012 Maven的 TestNG的 硒3.0.1

1 个答案:

答案 0 :(得分:1)

我猜测定义使用的默认浏览器是firefox,你的测试代码无法找到要使用的浏览器风格(通过命令行(或)JVM args(或)通过参数)所以它默认为什么它知道即{。{1}}。

我注意到在IntelliJ运行配置屏幕截图中,您已使用JVM参数firefox指定了浏览器风格,但出于某种原因,我在Jenkins配置页面屏幕截图中看不到相同内容。

你似乎有这样的事情-Dbrowser=chrome

是否可以将其更改为clean install package -DBrowserName="$BROWSERNAME" -e(我认为您不需要在引号中附上clean install package -Dbrowser=$BROWSERNAME -e并假设$BROWSERNAME引用Jenkins配置页中的有效参数)< / p>

这应该基本上解决你的问题。