java - 如何通过mvn命令行传递testng.xml参数值

标签 java xml maven selenium-webdriver

下面的testng.xml 文件有三个参数。我不想对参数 customerCode 进行硬编码。我想将不同的/不同的 customerCode 传递给以下 testng.xml 文件,但我也想将 Peter 作为默认 customerCode 如果只是如果我不通过 mvn 命令行提供一个,如下所示:

mvn test -DcustomerCode=customer1 -Ptestng.xml

下面是testng.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestSuite" parallel="false">

    <test name="someTest">
        <parameter name="browserType" value="chrome_win"></parameter>
        <parameter name="Url" value="http://regression.mytest.com/?customerCode=" />
        <parameter name="customerCode" value="Peter"/>

        <groups>
            <run>
                <exclude name="navigateToHomePage" />
            </run>
        </groups>
        <classes>
            <class name="com.automation.customermanagement.createTest.myIntegrationTest" >
            <methods>
                <exclude name="deleteCustomer" />
            </methods>
            </class>
        </classes>
    </test>
</suite>

我该如何实现?有办法吗?

下面是我的 POM 文件。我应该将 customerCode 属性放在配置文件的什么位置?

<profile>
    <id>AddMgmtTest</id>
    <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>
                    <suiteXmlFiles>
                        <suiteXmlFile>admgmttestng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>

最佳答案

这是我实现的解决方案,它运行良好!

1) 将以下属性添加到POM.xml 文件

                    <systemProperties>
                        <property>
                            <name>customerCode</name>
                            <value>Subaru</value>
                        </property>
                    </systemProperties>

2) 将这一行添加到测试方法

String customerCode= System.getProperty("customerCode"); 

3) 上面一行拉取下面mvn命令行中提供的customerCode=customer1

mvn 测试 -DcustomerCode=customer1 -Ptestng.xml

如果您不在命令行中提供 customerCode,它将采用 POM.xml 中上述​​属性中提供的默认值 Subaru > 文件

关于java - 如何通过mvn命令行传递testng.xml参数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43433315/

相关文章:

python - 不同配置格式的优缺点?

java - 在开发模式下运行 spring-GWT 应用程序时出现 XML 错误

xml - SSIS 如何从变量传递 XSD 路径

java - 尝试创建连接时未找到 JDBC 驱动程序

java - 在 Java 中声明一个内联数组会更快(或更好)吗?

java - 如何在 RecordStore 上插入和读取 utf-8 文本

java - lombok项目中的@Data可以识别但无法工作

java - Maven 需要手动更新依赖吗?

java - 广播接收器未在 Android Pie 中注册(工作)

java - 如何调用 onMeasure 方法? - 安卓