Jenkins如何参数化执行TestNG,这个Maven插件告诉你!

一、背景介绍

    工作中在使用Java+Selenium+TestNG+Maven+Jenkins做WebUI自动化测试的过程中,想要配置两个参数化构建。第一个就是执行Testng的XML文件参数;另一个参数就是环境参数,该参数对应WebUI自动化测试的环境,分别为UAT(测试环境)和PROD(生产环境),实际Selenium相关代码会由于环境参数的不同而不同(由于xpath定位或其他存在的差异)。
    实际效果:Jenkins给定不同的XML文件名称、环境参数后,会执行指定XML文件,指定环境的测试。

二、实现逻辑

在这里插入图片描述
在这里插入图片描述

三、Jenkins配置

  • 项目->配置->General->参数化构建

    在参数化构建中,添加两个对应的字符参数。注意这里的字符参数的名称后续需要对应Maven的pom.xml文件的配置。
在这里插入图片描述

  • 构建项目
    在这里插入图片描述

四、Maven插件配置

4.1 properties配置

<properties>
	<!--
      这里是将后面plugins插件中的配置设置到properties前面
      1 env.后面的字符串对应Jenkins中设置的字符名称,
      2 <xml.file>和<environment>中的xml.file和environment对应plugins中的配置
     -->
    <xml.file>${env.xmlFileName}</xml.file>
    <environment>${env.environment}</environment>
</properties>

4.2 build->plugins配置

<build>
    <plugins>
      <plugin>
      	
        <!--1.配置maven-surefire-plugin插件,指定版本-->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20.1</version>
        
        <!--2.使用configuration标签对插件进行配置-->
        <configuration>
        	<!--
          2.1使用suiteXmlFiles、suiteXmlFile标签设置运行的xml
          ${xml.file}对应properties设置的值
        	-->
          <suiteXmlFiles>
            <suiteXmlFile>${xml.file}</suiteXmlFile>
          </suiteXmlFiles>
          
          <!--
          2.2使用systemPropertyVariables标签设置运行的环境
          ${environment}对应properties设置的值
        	-->
          <systemPropertyVariables>
            <environment>${environment}</environment>
          </systemPropertyVariables>
        </configuration>
      </plugin>
    </plugins>
  </build>

五、TestNG参数化

关于TestNG的参数化,可以参考我前面的博客:https://blog.csdn.net/qq_37688023/article/details/105609002

5.1 XML参数化配置

<suite name="PC All Test Suite"  parallel="tests" thread-count="1" >
    <parameter name="environment" value="${environment}"/>
</suite>

5.2 Test注解方法参数化

@Test(priority = 1, description = "YFF  现金事务分类 进入页面")
@Parameters({"environment"})
public void test1(@Optional("uat") String environment) throws Exception {
  /* 初始化页面类 */
  cashAffairClassPage = new CashAffairClassPage(driver, environment, language);
  cashAffairClassPage.jumpToPageModule();
}

猜你喜欢

转载自blog.csdn.net/qq_37688023/article/details/105898327
今日推荐