为了账号安全,请及时绑定邮箱和手机立即绑定

在 Maven 中为 Selenium/Cucumber 使用不同环境的配置文件属性

在 Maven 中为 Selenium/Cucumber 使用不同环境的配置文件属性

眼眸繁星 2021-12-10 10:46:07
我使用基于 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>我希望当我在我的步骤 defs 中使用活动暂存配置文件运行我的测试时:system.getProperty("username")我希望它返回usernamestaging 的属性中提供的。当我在devprofile 处于活动状态时运行它时,我想获取dev的属性。
查看完整描述

2 回答

?
撒科打诨

TA贡献1934条经验 获得超2个赞

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


<propertiesFile>staging.properties</propertiesFile>



<propertiesFile>dev.properties</propertiesFile>

相应地命名不同的属性文件并src/test/resources直接放置它们。

使用 Maven 复制文件的最佳实践中config.properties描述的选项之一将相应的属性文件复制到${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

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


查看完整回答
反对 回复 2021-12-10
?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

我的解决方案是这样的:

  1. 使用 Spring Boot外部配置。我们可以设置application-{profile}.propertiesapplication-{profile}.yaml根据您的喜好设置。将环境变量(例如主机名、uri、凭据等)放在那里。如果我们需要所有环境的变量,只需将它们放入application.propertiesor 中application.yaml

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

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


查看完整回答
反对 回复 2021-12-10
  • 2 回答
  • 0 关注
  • 229 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号