You might be knowing everyone is suggesting to avoid hardcoding strings inside code like browser url, username, password, environment, which browser etc.
So to avoid hard coding, we use external file sources like json, xml or properties file to have all these values in key value pair.
so any one who want to run the tests, they can just update the values in these external files and no need to visit and change the actual code implementation.
This above works well when we want to run our tests manually from the code editor itself.
Problem –
But with increasing no. of tests, we tend to use nightly run using Azure devops or jenkins etc., where each run we can’t customise the values as we need to manually change the values in the project based on the need and push the code back to git or some other tool, which is not a good practice.
Solution –
So we need to provide a way where user can have the privilege to send the values from the command line itself [which the CI CD pipelines support to run the tests], and no need to change the values in side the project / framework.
Let’s understand how to achieve this using Maven.
Prerequisites –
- Maven project > pom.xml > sure-fire plugin
- TestNG
Use maven-surefire-plugin in pom.xml as prerequisite.
<plugins> [...] <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M7</version> <!--configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration--> </plugin> [...] </plugins>
Run all @Test from maven command line –
Now we can run mvn test
along with other parameters to guide the test run in different ways.
Open terminal or in any CICD tool, we can use below command
mvn test
This will run all the @Test methods in your project.
Run all @Test from a specific class –
mvn test -Dtest=[packageName].[classname]
will run all the @Test methods from the above mentioned classname
Run specific @Test methods –
mvn test -Dtest=[packageName].[classname]#[test1]+[test2]
Will run only tests test1 & test2 from the above mentioned classname
Run specific testNG categories –
mvn test -Dgroups=regression,api
will run all the @Test methods with group= “regression” or “api” or both
Run testNG.xml using maven command line –
mvn test -Dsurefire.suiteXmlFiles=testng.xml
This above will run the tests from testng.xml
Note – make sure the testng.xml file is located on the parent directory of project, else provide the complete path of testng.xml
You can even run multiple testng.xml files as below
mvn test -Dsurefire.suiteXmlFiles=testng_ui.xml, testng_api.xml
Other ways to run testng.xml – using pom.xml properties
Add the configuration section under surefire plugin and the properties section as below
<!--...--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <XmlFile>${myTestNGFile}</XmlFile> <skipTests>false</skipTests> </properties> <dependencies> </dependencies> <plugins> <!--...--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M7</version> <configuration> <suiteXmlFiles> <suiteXmlFile>${XmlFile}</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> <!--...--> </plugins>
Run below command to run any testng files from your project
mvn clean test -DmyTestNGFile=testng_functional.xml
You can replace testng_functional.xml to any desired testng xml file.
Reference – Running single test using surefire
Customised parameters into maven command –
To fit the above concept in our framework and accept any of our own parameters from maven command to code base / framework, refer below –
We will see what changes we need to do at code level to accept these parameter values from command line, so our tests will run accordingly.
We can use String browser = System.getProperty("browser", "chrome");
From the above line, “chrome” is the default if not sending any value from command line.
Let’s see usage of this above code line in selenium –
package testNgLearning; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class MavenParams { //mvn test -Dtest=testNgLearning.MavenParams -Dbrowser=firefox @Test public void MavenParamTest() throws InterruptedException { WebDriver driver = null; String browser = System.getProperty("browser", "chrome"); if(browser.contains("chrome")){ System.setProperty("webdriver.chrome.driver","/Users/skpatro/sel/chromedriver"); driver = new ChromeDriver(); }else if(browser.contains("firefox")){ System.setProperty("webdriver.gecko.driver","/Users/skpatro/sel/geckodriver"); driver = new FirefoxDriver(); } if(driver != null){ driver.get("https://qavbox.github.io/demo/links/"); Thread.sleep(2000); driver.quit(); } } }
If you run the above code from command line or using any CICD pipeline, it will open the url in chrome browser as that’s the default browser we set.
command –
mvn test
To run a different browser name (Let’s say firefox), we can use below command in command prompt or terminal
mvn test -Dtest=testNgLearning.MavenParams -Dbrowser=firefox
This above command will run MavenParams class in firefox browser.
For any values you pass from command line for any parameters, we have to handle them from the framework side.
As this is demo test, we have kept the variables in the same test class. but in real time we can have a separate class only holding all the variables we need to send from maven command line and we can access these variables from any test methods.
Watch below to understand more on the implementation –
Hope this helps!