【问题标题】:Selenium Webdriver : How to run testNG.xml file in Terminal (in MAC)?Selenium Webdriver:如何在终端(在 MAC 中)运行 testNG.xml 文件?
【发布时间】:2020-03-08 21:43:39
【问题描述】:

我在 MAC 系统中创建了一个 Maven-testNG 项目。我创建了一个 testSuite,现在我想使用 Terminal 运行 testNG.xml 文件。有什么方法可以运行这个xml文件吗?

testNG.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" verbose="2">
    <test name="Test">
        <classes>
            <class name="com.Live.testcase.TC0001_Signup" />
            <class name="com.Live.testcase.TC0002_SoleProprietorship" />
            <class name="com.Live.testcase.TC0003_Login" />
            <class name="com.Live.testcase.TC0004_ForgotPassword" />
            <class name="com.Live.testcase.TC0005_LLC" />
        </classes>
    </test> <!-- Test -->
</suite> <!-- Suite -->

Pom.xml

<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.learnautomation</groupId>
    <artifactId>com.learnautomation.selenium</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <suitXmlFile>src/main/resources/testng.xml</suitXmlFile>
        <skipTests> false </skipTests>
    </properties>

    <build>
        <plugins>
            <plugin>

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <compilerVersion>1.8</compilerVersion>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>

                    <suiteXmlFiles>
                        <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
                    </suiteXmlFiles>


                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.14.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.13</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/jexcelapi/jxl -->
        <dependency>
            <groupId>jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.4.2</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/com.aventstack/extentreports -->
        <dependency>
            <groupId>com.aventstack</groupId>
            <artifactId>extentreports</artifactId>
            <version>3.1.5</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.10-FINAL</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

    </dependencies>

</project>

注意:我在互联网上看到了很多选项。主要是关于通过命令行(windows机器)运行代码和一些不是maven项目的建议意味着简单的项目,转到库部分并运行命令。

【问题讨论】:

    标签: java xml maven selenium testng


    【解决方案1】:

    正如我所说,我的 testng 文件在 build 文件夹中,所以我必须在 &lt;suiteXmlFile&gt;build/${suiteFile}&lt;/suiteXmlFile&gt; 中提供路径

    Pom.xml

    中进行了一些更改
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M4</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>build/${suiteFile}</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

    运行命令:

    $ cd path/to/your/project

    $mvn test -DsuiteFile=testng.xml

    $mvn clean test -DsuiteFile=testng.xml

    参考 youtube 链接: https://www.youtube.com/watch?v=lQCyVy-g1e8&t=436s


    注意:

    如果您的testng.xml 文件不属于任何文件夹,而您只想每次都调用testng.xml,则项目中没有其他xml 文件。

    简单地说&lt;suiteXmlFile&gt;testng.xml&lt;/suiteXmlFile&gt; 运行命令

    $ cd path/to/your/project

    $mvn test

    【讨论】:

      【解决方案2】:

      是的,您可以从终端运行 testng.xml 文件。你应该在你的机器上安装了maven。以下命令会有所帮助。

      $ brew install maven
      

      之后,您可以简单地使用mvn 命令在您的项目中执行 maven 目标。如果您已相应配置 Pom.xml 文件,则以下命令将执行您的 testng.xml 文件

      $ cd path/to/your/project
      $ mvn test
      

      【讨论】:

      • mvn test -DsuiteXmlFile=YourTestSuiteFileName 将是适当的命令。
      • 是的。但是 Pom.xml 已经有&lt;suitXmlFile&gt; 标签。所以不需要在命令中再次提及。
      • 1. mvn clean install 2. mvn test 任何事情都应该有效。但是应该安装 maven 并设置 .m2 路径
      • 如果我使用 mvn clean install 会显示错误。 prnt.sc/pw8894 显示错误,如果我使用 mvn 测试 prnt.sc/pw88qh
      • 您已将 &lt;suiteXmlFile&gt; 设置为 src/main/resources/testng.xml。确保您的 xml 文件在此处,然后重试。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 1970-01-01
      • 2016-05-29
      • 2013-09-20
      • 2013-11-22
      • 2015-11-26
      相关资源
      最近更新 更多