Whereas engaged on an open supply GitHub challenge that was created to showcase the working of Selenium WebDriver framework with Java, because the challenge grew, there was a have to create a number of testng.xml information for working totally different checks.
These a number of information have been created to segregate the checks and place all of the checks associated to a respective web site in a single testng.xml (I’ve used totally different demo web sites to demo totally different actions that may be automated utilizing Selenium WebDriver).
I considered throwing some gentle on the utilization of a number of testng.xml information and methods to execute checks. Since Maven is the construct instrument that’s getting used, a single testng.xml file is required to run all of the checks within the challenge. Additionally, there are circumstances that needed to be debugged for the check failures by working a single testng.xml file.
On this challenge, I’ve created 9 totally different testng.xml information which have a number of checks, and I’m working all checks in these 9 totally different testng.xml information utilizing a single testng.xml file. Sure, that’s doable!
So, be part of my journey the place I’ll display methods to execute a number of testng.xml information utilizing a single testng.xml file. I may also be shedding some gentle on executing a single testng.xml file out of the 9 obtainable ones and working it from the command line utilizing Maven.
Operating A number of testng.xml Recordsdata Utilizing a Single testng.xml File
Let’s first deal with working all of the checks with all of the 9 totally different testng.xml information. The answer to that is to make use of the
tag in your testng.xml file and supply the opposite testng.xml file’s path between this tag. Right here is the instance file to display what I’m speaking about:
As soon as we execute this file, it can execute the respective testng.xmls within the order as up to date between the
tag. So, “testng-saucedemo.xml”
will likely be executed first, after which, “testng-automationpractice.xml”
will likely be executed, and so forth.
All of the testng.xml information offered within the above instance have a number of checks in them. So, all of the checks throughout the respective testng.xml will get executed and after completion, the following XML file will likely be picked for execution.
The next are the contents of the testng-saucedemo.xml file:
As soon as all of the checks as up to date on this XML file get executed (doesn’t matter move or fail), and the execution is completed, the following file will likely be picked for working one other set of checks.
Operating the suite information in parallel isn’t supported by testng.
Operating a Single testng.xml File Utilizing Maven
You might have an possibility in IDE to run your checks utilizing testng.xml file by right-clicking on it and choosing the choice to run the checks. Nevertheless, on the subject of executing the checks within the CI/CD pipeline, that possibility doesn’t maintain effectively, as it’s essential to execute the checks utilizing instructions within the automated pipeline.
Configuring Your Undertaking To Run suite-xml File Utilizing Command Line
We have to set the next configuration to have the ability to run the testng.xml file utilizing Maven.
Replace the Maven Surefire plugin in your pom.xml:
Discover the
tag within the above screenshot. The worth for suiteXmlFile
is about as ${suite-xml}
. We will likely be setting the default worth for this declaration within the properties block of the pom.xml file as follows:
The default path to testng.xml is about for the file that we used within the above part of this weblog and have up to date the suite-files
path in it.
So, now if we run the command mvn clear set up
or mvn clear check
, Maven will choose up the testng.xml default file primarily based on the file path that’s up to date within the properties block and execute all of the checks.
Now, the query that involves thoughts is: “What should I do if I want to execute any other testng.xml file, is it possible to do so?”
The reply is “Yes”: we will run any testng.xml file in our challenge by including the -Dsuite-xml=
in our mvn
command.
Bear in mind, we had earlier set this configuration in our Maven Surefire plugin block in pom.xml.
We simply have to move the values for the suite-xml
property variable within the command line, which will be accomplished utilizing the -D
possibility in mvn
command:
mvn clear check -Dsuite-xml=
Let’s now strive our fingers with the command line and run totally different testng.xml information utilizing Maven from the command line, as we simply discovered.
We are going to run the testng-internet.xml
file, examine that it ought to override the present default testng.xml, and run solely the one which we move within the command. We have to move the total path the place the testng.xml is saved, and in our case, it’s obtainable in test-suite
folder, so the total path is test-suitestestng-theinternet.xml
. Right here is the command that we are going to run (ensure you are on the challenge’s root folder path within the command line window earlier than you execute the Maven command):
mvn clear check -Dsuite-xml=test-suitetestng-theinternet.xml
“-Dsuite-xml” possibility can be utilized with different maven instructions like as follows : mvn clear set up/ mvn clear confirm, and many others.
The checks have been efficiently run and the outcomes have been printed on the console. It says 32 checks ran and handed efficiently.
To verify that the proper XML file was picked and checks have been executed, let’s execute the checks for testng-theinternet.xml
file utilizing IDE and examine the variety of checks executed.
We are able to see that 32 checks have been executed and handed, which confirms that the checks we executed utilizing the mvn
command have been accurately executed for the testng.xml file we handed.
Conclusion
We are able to have a number of testng.xml information to segregate the checks primarily based on the totally different modules/web sites in our challenge and these a number of testng.xml information will be executed utilizing a single testng.xml file.
Likewise, we will execute a testng.xml file from the command line utilizing the Maven Surefire plugin.