SornoDavid
SornoDavid

Reputation: 47

Use profiles properties for different enviroments in Maven for Selenium/Cucumber

I create tests using Selenium WebDriver and Cucumber-jvm, built on Maven. I want to achieve next:

I want to have profiles with properties and use this properties in my steps depended on enviroments.

I've created a folder in src/test/resources and added 2 subfolder in it: Staging and Dev.

In each folder I have a file config.properties where I have saved username.

My POM looks like :

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

        </properties>
    </profile>

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

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

Now I want to change properties of profiles to something like this:

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

And I want when I run my test with an active staging profile in my step defs when I call:

system.getProperty("username")

I want this to return username which is provided in staging's properties.

When I run this when dev profile is active, I want to get dev's property.

Upvotes: 3

Views: 2313

Answers (2)

Eric Tan
Eric Tan

Reputation: 1463

My solution is like this:

  1. Use Spring Boot external configuration. We could set up either application-{profile}.properties or application-{profile}.yaml according to your preference. Put the environment variables (e.g. hostname, uri, credentials etc) over there. And if we need variables for all the environments, just put them in application.properties or application.yaml.

  2. Use the maven argument spring.profiles.active to activate the profile accordingly. The following command is one use case in CI/CD:

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

Upvotes: 0

Gerold Broser
Gerold Broser

Reputation: 14782

  • Add properties to your profiles, e.g:

    <propertiesFile>staging.properties</propertiesFile>
    


    <propertiesFile>dev.properties</propertiesFile>
    
  • Name the different properties files accordingly and place them in src/test/resources directly.
  • Copy the according properties file to config.properties with one of the options described in Best practices for copying files with Maven by using ${propertiesFile}. I prefer the Wagon Maven Plugin.

UPDATE

That means: Forget about the two extra directories containing the two properties files. Put them both in src/test/resources/ and copy them according to:

staging

src/test/resources/staging.properties copied to:

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

depending on the phase you bind the copy process to.

dev

src/test/resources/dev.properties copied to:

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

depending on the phase you bind the copy process to.

Upvotes: 2

Related Questions