我使用基于Maven的Selenium WebDriver和Cucumber-jvm创建测试。 我要实现下一个目标:
我想拥有带有属性的配置文件,并根据环境在我的步骤中使用此属性。
我在src/test/resources
中创建了一个文件夹,并在其中添加了2个子文件夹:Staging
和Dev
。
在每个文件夹中,我都有一个文件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
的属性。
答案 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)
我的解决方案是这样的:
使用Spring Boot external configuration。我们可以根据您的喜好设置application-{profile}.properties
或application-{profile}.yaml
。将环境变量(例如主机名,uri,凭据等)放在此处。而且,如果我们需要所有环境的变量,只需将其放在application.properties
或application.yaml
中即可。
使用maven参数spring.profiles.active
相应地激活配置文件。以下命令是CI / CD中的一种用例:
mvn clean verify -Dspring.profiles.active={profile} ..........