简体   繁体   English

在运行时将数据从Jenkins参数化的版本传递到testng.xml

[英]Passing data from Jenkins parameterized build to testng.xml at runtime

I have a browser automation framework which uses Java, Maven and TestNG. 我有一个使用Java,Maven和TestNG的浏览器自动化框架。 I am using testng.xml files to pass different parameters to tests in the following way: 我正在使用testng.xml文件通过以下方式将不同的参数传递给测试:

<parameter name="firstName" value="John"/> 

I want to be able to expose the data inputs to non-technical people and propose to do this by using a parameterized Jenkins job. 我希望能够向非技术人员公开数据输入,并建议通过使用参数化的詹金斯工作来做到这一点。

All of the test data are Strings. 所有测试数据都是字符串。

I can see how I can create a parameterized project in Jenkins and can create String parameters within the project. 我可以看到如何在Jenkins中创建参数化项目以及如何在项目中创建String参数。

I am unclear as to the best way of passing the data at runtime. 我不清楚在运行时传递数据的最佳方法。

I understand an Ant script can be used, but I am using Maven already and don't want to introduce another conflicting technology. 我知道可以使用Ant脚本,但是我已经在使用Maven,并且不想引入另一种相互冲突的技术。

I have heard this can be done using a Beanshell script in the testng.xml but, from the example on the testng docs , I'm still unclear how best to implement this. 我听说可以使用testng.xml中的Beanshell脚本来完成此操作,但是从testng docs上的示例中 ,我仍然不清楚如何最好地实现此目的。

Here's how you can solve this problem. 解决问题的方法如下。

  • Add all the parameters that you want to pass via Jenkins into your suite xml file and define some default values for it. 将您要通过Jenkins传递的所有参数添加到套件xml文件中,并为其定义一些默认值。
  • In your Jenkins job you now create the parameters but ensure that the name of the parameter in your job configuration matches with the name that you have added in your suite xml file. 现在,您在Jenkins作业中创建参数,但要确保作业配置中的参数名称与在套件xml文件中添加的名称匹配。
  • You then make use of a TestNG listener as shown below and then wire it in using one of the methods that are detailed in this blog post of mine 然后,您可以使用如下所示的TestNG侦听器,然后使用我的这篇博客文章中详细介绍的方法之一将其连接

After that, you should be able to easily do what you need. 之后,您应该可以轻松地完成所需的工作。 If you are having parameters that are defined at <test> level, then you can enhance the listener I have shown below and add similar code after implementing org.testng.ITestListener listener. 如果您具有在<test>级别定义的参数,则可以增强下面显示的侦听器,并在实现org.testng.ITestListener侦听器之后添加类似的代码。

Listener 倾听者

public class EnvReaderListener implements ISuiteListener {

    @Override
    public void onStart(ISuite suite) {
        Map<String, String> parameters = suite.getXmlSuite().getParameters();
        for (Map.Entry<String, String> parameter : parameters.entrySet()) {
            String env = System.getenv(parameter.getKey());
            if (env != null && ! env.trim().isEmpty()) {
                parameter.setValue(env);
            }
        }
    }

    @Override
    public void onFinish(ISuite suite) {

    }
}

TestNG suite xml TestNG套件XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="My Suite" parallel="methods" verbose="2">
    <listeners>
        <listener class-name="org.rationale.emotions.EnvReaderListener"/>
    </listeners>
    <parameter name="name" value="Sample name"/>
    <test name="My Test" verbose="2">
        <classes>
            <class name="org.rationale.emotions.SampleTest"/>
        </classes>
    </test>
</suite>

As per this example, you would need to ensure that your job would have a parameter named name . 按照此示例,您需要确保您的作业将具有一个名为name的参数。

Sample test class 样品测试课

package org.rationale.emotions;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class SampleTest {
    @Test
    @Parameters ("name")
    public void testMethod(String name) {
        System.err.println("Name that was passed in via Jenkins job " + name);
    }
}

you may use Maven Resources Plugin goal copy-resources with enabled filters in your project to replace variables in a template file. 您可以在项目中启用filters情况下使用Maven Resources Plugin目标copy-resources来替换模板文件中的变量。 Example: 例:

         <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.1</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>path_to_output_folder/output</outputDirectory>
                        <resources>          
                            <resource>
                                <directory>path_to_resource_dir/res</directory> <!--source to your testng.xml-->
                                <filtering>true</filtering>
                            </resource>
                        </resources>              
                    </configuration>            
                </execution>
            </executions>
        </plugin>

in your case for using this with jenkins : 在您的情况下与jenkins一起使用:

  • change your testng.xml --> <parameter name="firstName" value="${firstname}"/> 更改您的testng.xml-> <parameter name="firstName" value="${firstname}"/>

  • call maven goal mvn resources:resources -DfirstName=$FIRSTNAME where ($FIRSTNAME) is the Jenkins variable. 调用maven目标mvn resources:resources -DfirstName=$FIRSTNAME ,其中($ FIRSTNAME)是Jenkins变量。

hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM