Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a testng .xml file from batch for a maven project

I have created a maven project and within I have several testng.xml files for my different suites.

If I run my .xml files from within my IDE (Intelij) everything goes fine

But what I want to do is create a batch file that run these .xml for me so I can run it without my IDE.

I've seen a solution when you have a lib folder etc like this : https://www.seleniumeasy.com/testng-tutorials/how-to-run-testng-xml-via-batch-file-example

But it doesn't works for me since I have a maven project so no lib or bin folder.

I've also tried this

SET SUITE_LOCATION=C:\Users\%USERNAME%\IdeaProjects\Test_Automation\src\main\java\MyApp\Scenarios\CustomStuff
SET CLASSPATH=C:\Users\%USERNAME%\.m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar;
java org.testng.TestNG %SUITE_LOCATION%\testng.xml

or

java -cp org.testng.TestNG %SUITE_LOCATION%\testng.xml

But It doesn't work. I also tried to add the .class files in the classpath but didn't work either.

Any idea on how to run it ?

Thank you!

UPDATE

I'm getting close to something I think this is the last command I tried :

java -classpath C:\Users\%USERNAME%\.m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar;C:\Users\%USERNAME%\.m2\repository\com\beust\jcommander\1.48\jcommander-1.48.jar;%CLASSPATH% org.testng.TestNG -d test-outputs mytestsuite.xml

Error was :

[TestNG] [ERROR] Cannot find class in classpath: "myApp.Scenarios.Scenario1"

this is my xml file :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="AndroidSuite" parallel="tests" thread-count="1">
<test name="SamsungS6-Scenario1">
    <parameter name="deviceUDID"  value="04157df40862d02f"/>
    <classes>
        <class name="myApp.Scenarios.Scenario1"/>
    </classes>
</test>

like image 527
user3718160 Avatar asked Dec 01 '25 09:12

user3718160


2 Answers

If you are using maven, run a testsuite like this :

mvn clean test -DsuiteXmlFile=src/test/resources/api/TestngSuite.xml

Add this command to a batch file and execute it.

like image 157
S.K. Venkat Avatar answered Dec 04 '25 01:12

S.K. Venkat


If you are using maven project and java 8, then you need to do several steps before you run your testng xml file from batch file:

  1. In your pom xml file, you need add a code that will force maven to use 1.8 for the compiler because the default one is 1.5

    <properties>
    
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    
    </properties>
    
  2. In your pom xml file, you need to add the code for the maven-surefire-plugin:

    <plugins>
    
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
                </suiteXmlFiles>
                <testFailureIgnore>true</testFailureIgnore>
            </configuration>
        </plugin>
    
    </plugins>
    

  3. Save your pom.xml file and right click on your maven project and select maven->update project and check the check box for "force update of snapshots/releases" and click ok

  4. Now create a batch file with an extension bat and put this sample code in it and change it according to your project.

    set projectLocation=C:\locationOfYourProject\locationOfYourTestNGXmlFile

    cd %projectLocation%

    mvn clean test -DsuiteXmlFile=yourXMLFileCanBeAnyName.xml

like image 45
SOAlgorithm Avatar answered Dec 03 '25 23:12

SOAlgorithm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!