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

我使用基于 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的属性。


眼眸繁星
浏览 230回答 2
2回答

撒科打诨

向您的个人资料添加属性,例如:<propertiesFile>staging.properties</propertiesFile><propertiesFile>dev.properties</propertiesFile>相应地命名不同的属性文件并src/test/resources直接放置它们。使用 Maven 复制文件的最佳实践中config.properties描述的选项之一将相应的属性文件复制到${propertiesFile}。我更喜欢 Wagon Maven 插件。更新这意味着:忘记包含两个属性文件的两个额外目录。将它们都放入src/test/resources/并根据以下内容复制它们:stagingsrc/test/resources/staging.properties 复制到:src/test/resources/config.properties或者 target/config.properties取决于您将复制过程绑定到的阶段。devsrc/test/resources/dev.properties 复制到:src/test/resources/config.properties或者 target/config.properties取决于您将复制过程绑定到的阶段。

拉莫斯之舞

我的解决方案是这样的:使用 Spring Boot外部配置。我们可以设置application-{profile}.properties或application-{profile}.yaml根据您的喜好设置。将环境变量(例如主机名、uri、凭据等)放在那里。如果我们需要所有环境的变量,只需将它们放入application.propertiesor 中application.yaml。使用 maven 参数相应spring.profiles.active地激活配置文件。以下命令是 CI/CD 中的一个用例:mvn clean verify -Dspring.profiles.active={profile} ..........
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java