在Maven中针对硒/黄瓜的不同环境使用配置文件属性

时间:2018-10-26 13:17:50

标签: java maven selenium cucumber-jvm maven-profiles

我使用基于Maven的Selenium WebDriver和Cucumber-jvm创建测试。 我要实现下一个目标:

我想拥有带有属性的配置文件,并根据环境在我的步骤中使用此属性。

我在src/test/resources中创建了一个文件夹,并在其中添加了2个子文件夹:StagingDev

在每个文件夹中,我都有一个文件config.properties,其中保存了username

我的POM看起来像:

<profiles>
    <profile>
        <id>staging</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>

        </properties>
    </profile>

    <profile>
        <id>dev</id>
        <properties>

        </properties>
    </profile>
</profiles>

现在,我想将配置文件的属性更改为以下内容:

<properties> test/resources/dev/config.properties</properties>
<properties> test/resources/staging/config.properties</properties>

我想在调用时在步骤def中使用活动登台配置文件运行测试时

system.getProperty("username")

我希望它返回登台属性中提供的username

dev个人资料处于活动状态时运行此文件时,我想获取dev的属性。

2 个答案:

答案 0 :(得分:2)

  • 向个人资料添加属性,例如:

    <propertiesFile>staging.properties</propertiesFile>
    


    <propertiesFile>dev.properties</propertiesFile>
    
  • 相应命名不同的属性文件,然后将其直接放置在src/test/resources中。
  • 使用config.properties使用Best practices for copying files with Maven中描述的选项之一将相应的属性文件复制到${propertiesFile}。我更喜欢Wagon Maven插件。

更新

这意味着:忘记包含两个属性文件的两个额外目录。将它们都放在src/test/resources/中,并根据以下条件进行复制:

staging

src/test/resources/staging.properties已复制到:

  • src/test/resources/config.properties
  • target/config.properties

取决于将复制过程绑定到的阶段。

dev

src/test/resources/dev.properties已复制到:

  • src/test/resources/config.properties
  • target/config.properties

取决于将复制过程绑定到的阶段。

答案 1 :(得分:0)

我的解决方案是这样的:

  1. 使用Spring Boot external configuration。我们可以根据您的喜好设置application-{profile}.propertiesapplication-{profile}.yaml。将环境变量(例如主机名,uri,凭据等)放在此处。而且,如果我们需要所有环境的变量,只需将其放在application.propertiesapplication.yaml中即可。

  2. 使用maven参数spring.profiles.active相应地激活配置文件。以下命令是CI / CD中的一种用例:

    mvn clean verify -Dspring.profiles.active={profile} ..........