4.8.15

Pass and read Runtime Property Values via Maven command

Many a times when you implement CI with Maven & Jenkins or TeamCity, you might want to pass parameters to the test like the Browser and Env in which you want to run your tests.
This can be done in various ways, but a very simple and straightforward method is as follows -

Define the variables in your tests that you want to store the values of these run-time parameters, like below

static final String BROWSER_TYPE = System.getProperty("browserType");
static final String RUN_ENV = System.getProperty("runEnv");

These can be defined in the base Abstract class that you use to initialize global methods/fields, etc. And its a good idea to declare them static and final so that we can use them directly in our tests, and we dont overwrite these values.

System.getProperty - this is the method that actually gets you the value that you have passed as part of your maven command. You can get any property you want via this.

Your maven command can look like this -

    mvn integration-test -DbrowserType=chrome -DrunEnv=test3

You can have these parameters defined in any order, and can give any name to it, but the names passed as maven command, and those referred in your tests should be the same. What I refer here as parameters is technically called a property of the plugin.

In order to pass multiple values for the same parameters, use the following syntax -

mvn integration-test -DbrowserType=FireFox-DrunEnv=Test1 -DmetaFilters=+Regression +OnBoarding -skip -Manual -wip

Multiple values have been specified for the metaFilters property, to be used with JBehave Test Story-s and Scenarios.
This will only execute scenarios with Regression and OnBoarding tags, and not with skip, Manual or wip tags. Refer this post to learn more about Meta Filtering with JBehave

Another example of maven parameters -

mvn clean integration-test 
-Duse.sso.login=true 
-Dcontext.view.show=false 
-Dusers.dir="c:\jenkins\jbehave" 
-Dbrowser=firefox -Denv=test1 
-Dmeta.filters="${PARM_META_FILTERS} -broken -wip -skip -manual"


We can declare parameter names like ${suiteXmlLFile} in the POM and then specify that in the mvn command to pass the value at run time.
<suiteXmlFile>${suiteXmlFile}</suiteXmlFile>

mvn test -DsuiteXmlFile=./Config/parallelTestNG.xml

Maven commands are case sensitive hence, use exact names while specifying and reading values.


One cool trick is that you can validate the value that was passed via maven correctly or not, by looking up that property [browserType] in the 'System Properties' pane of JVisualVM for your IDE - intelliJ or Eclipse.

No comments:

Post a Comment