Bilal
Bilal

Reputation: 588

Set config.properties values through maven command

I am new to automation and created a sample project. I have a config.properties file and class to read and write to this file. config.properties contains browser and url variables. I want to run the test using mvn test command and I want to pass the browser value on runtime.

something like

mvn -Dbrowser=firefox test

I am not sure how to do that as these things are totally alien to me.

Upvotes: 0

Views: 1699

Answers (2)

pdrersin
pdrersin

Reputation: 311

With your current code you are setting a temporary variable in the env variables. For this to work you have to go to your class where you set the browser string and put a boolean there. The boolean should check if the browser value from the environment variable is null or not. If not null, it should take that value as your browser value. And if it is null it should do whatever you are doing now. So, instead of just directly reading the browser value from the config file, you should make it conditional. Something like this:

String browserParamFromEnv = System.getProperty("browser");
String browser = browserParamFromEnv == null ? ConfigurationReader.getProperty("browser") : browserParamFromEnv;

Upvotes: 2

Muzzamil
Muzzamil

Reputation: 2881

You can pass browser name dynamically with -D as prefix with system variable. To set up it with maven project, Maven Surefire plugin provides the configuration parameter  forsystemPropertyVariables to set system variables. The properties defined here will be available in the maven tests.

<build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.0</version>
                    <configuration>
                        <systemPropertyVariables>
                            <browser>${browserName}</browser>
                        </systemPropertyVariables>              
                    </configuration>
                </plugin>
            </plugins>
        </build>

To set dynamic system variables you can use ${browserName} so you are free to pass any value in maven command as

mvn -Dbrowser=firefox test

OR

mvn -Dbrowser=chrome test

Upvotes: 1

Related Questions