October 15, 2024

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 must create a number of testng.xml information for operating totally different exams. 

These a number of information had been created to segregate the exams and place all of the exams 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 the way to execute exams. Since Maven is the construct device that’s getting used, a single testng.xml file is required to run all of the exams within the challenge. Additionally, there are circumstances that needed to be debugged for the take a look at failures by operating a single testng.xml file.

testng files

On this challenge, I’ve created 9 totally different testng.xml information which have a number of exams, and I’m operating all exams 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 the way to execute a number of testng.xml information utilizing a single testng.xml file. I can even be shedding some gentle on executing a single testng.xml file out of the 9 obtainable ones and operating it from the command line utilizing Maven.

Working A number of testng.xml Information Utilizing a Single testng.xml File

Let’s first concentrate on operating all of the exams with all of the 9 totally different testng.xml information. The answer to that is to make use of the <suite-files> </suitefiles> 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:

<?xml model="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite identify="Selenium 4 POC Exams ">
<suite-files>
    <suite-file path="testng-saucedemo.xml"/>
    <suite-file path="testng-automationpractice.xml"/>
    <suite-file path="testng-theinternet.xml"/>
    <suite-file path="testng-juice-shop.xml"/>
    <suite-file path="testng-lambdatestecommerce.xml"/>
    <suite-file path="testng-seleniumgrid-theinternet.xml"/>
    <suite-file path="testng-lambdatest-selenium-playground.xml"/>
    <!--        <suite-file path="testng-seleniumgrid-juiceshop.xml"/>-->
</suite-files>
</suite> 

As soon as we execute this file, it would execute the respective testng.xmls within the order as up to date between the <suite-files> tag. So, “testng-saucedemo.xml” can be executed first, after which, “testng-automationpractice.xml” can be executed, and so forth.

All of the testng.xml information offered within the above instance have a number of exams in them. So, all of the exams inside the respective testng.xml will get executed and after completion, the subsequent XML file can be picked for execution.

The next are the contents of the testng-saucedemo.xml file:

<?xml model="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite identify="Sauce Demo Web site Exams" parallel="exams" thread-count="4" verbose="2">
<take a look at identify="selenium 4 Exams with Chrome Browser">
    <parameter identify="browser" worth="chrome"/>
    <courses>
        <class identify="io.github.mfaisalkhatri.exams.saucedemo.SauceDemoTests">
            <strategies>
                <embrace identify="loginSauceDemoTest"/>
                <embrace identify="logOutSauceDemoTest"/>
            </strategies>
        </class>
    </courses>
</take a look at> <!-- Check -->
<take a look at identify="selenium 4 Exams with Firefox Browser">
    <parameter identify="browser" worth="firefox"/>
    <courses>
        <class identify="io.github.mfaisalkhatri.exams.saucedemo.SauceDemoTests">
            <strategies>
                <embrace identify="loginSauceDemoTest"/>
                <embrace identify="logOutSauceDemoTest"/>
            </strategies>
        </class>
    </courses>
</take a look at> <!-- Check -->
<take a look at identify="selenium 4 Exams with Edge Browser" enabled="false">
    <parameter identify="browser" worth="edge"/>
    <courses>
        <class identify="io.github.mfaisalkhatri.exams.saucedemo.SauceDemoTests">
            <strategies>
                <embrace identify="loginSauceDemoTest"/>
                <embrace identify="logOutSauceDemoTest"/>
            </strategies>
        </class>
    </courses>
</take a look at> <!-- Check -->
<take a look at identify="selenium 4 Exams with Opera Browser" enabled="false">
    <parameter identify="browser" worth="opera"/>
    <courses>
        <class identify="io.github.mfaisalkhatri.exams.saucedemo.SauceDemoTests">
            <strategies>
                <embrace identify="loginSauceDemoTest"/>
                <embrace identify="logOutSauceDemoTest"/>
            </strategies>
        </class>
    </courses>
</take a look at> <!-- Check -->
</suite> <!-- Suite -->

As soon as all of the exams as up to date on this XML file get executed (doesn’t matter move or fail), and the execution is completed, the subsequent file can be picked for operating one other set of exams.

Working the suite information in parallel just isn’t supported by testng.

Working a Single testng.xml File Utilizing Maven

You could have an possibility in IDE to run your exams utilizing testng.xml file by right-clicking on it and deciding on the choice to run the exams. Nevertheless, in relation to executing the exams within the CI/CD pipeline, that possibility doesn’t maintain properly, as it is advisable execute the exams utilizing instructions within the automated pipeline.

Configuring Your Mission 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:

Update the Maven Surefire plugin in your pom.xml

Discover the <SuiteXmlFile> tag within the above screenshot. The worth for suiteXmlFile is about as $suite-xml. We can be setting the default worth for this declaration within the properties block of the pom.xml file as follows:
Set the default value for this declaration in the properties block of the pom.xml file

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.

The default path to testng.xml is set for the file

So, now if we run the command mvn clear set up or mvn clear take a look at, Maven will decide up the testng.xml default file based mostly on the file path that’s up to date within the properties block and execute all of the exams.

Now, the query that involves thoughts is: “What ought to I do if I need to execute some other testng.xml file, is it doable to take action?”

The reply is “Sure”: we will run any testng.xml file in our challenge by including the -Dsuite-xml=<testng.xml file path> in our mvn command.

Bear in mind, we had earlier set this configuration in our Maven Surefire plugin block in pom.xml.

Configuration in our Maven Surefire plugin block in pom.xml

We simply must move the values for the suite-xml property variable within the command line, which might be executed utilizing the -D possibility in mvn command:

mvn clear take a look at -Dsuite-xml=<testng.xml file path>

Let’s now attempt our palms with the command line and run totally different testng.xml information utilizing Maven from the command line, as we simply realized.

We’ll run the testng-internet.xml file, verify that it ought to override the prevailing default testng.xml, and run solely the one which we move within the command. We have to move the complete path the place the testng.xml is saved, and in our case, it’s obtainable in test-suite folder, so the complete path is test-suitestestng-theinternet.xml. Right here is the command that we’ll run (be sure to are on the challenge’s root folder path within the command line window earlier than you execute the Maven command):

mvn clear take a look at -Dsuite-xml=test-suitetestng-theinternet.xml

Test results on console - 1

Test results on console - 2

“-Dsuite-xml” possibility can be utilized with different maven instructions like as follows : mvn clear set up/ mvn clear confirm, and so forth.

The exams had been efficiently run and the outcomes had been printed on the console. It says 32 exams ran and handed efficiently.

To verify that the right XML file was picked and exams had been executed, let’s execute the exams for testng-theinternet.xmlfile utilizing IDE and verify the variety of exams executed.
Execute the tests for testng-theinternet.xmlfile using IDE

Check the number of tests executed

We are able to see that 32 exams had been executed and handed, which confirms that the exams we executed utilizing the mvn command had been appropriately executed for the testng.xml file we handed.

Conclusion

We are able to have a number of testng.xml information to segregate the exams based mostly on the totally different modules/web sites in our challenge and these a number of testng.xml information might 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.