Search code examples
javamavenseleniumcucumber-jvmmaven-profiles

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.


Solution

    • 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.