Execute Selenium script(testng.xml) through terminal/command prompt

We can execute testng.xml file in different ways. It could be Maven, Ant, IDE or directly via command line/terminal. Executing testng.xml file via command line allows user to run multiple testng xml files simultaneously. Before running a testng.xml suite through the command prompt, we need to compile our project code. We can find the compiled code by eclipse under a folder named “bin” inside the corresponding java project.

Pre-requisite:

All the dependencies should be in a central location i.e. All the jar files should be in single folder.

Directory structure:

/bin – All the test packages are under bin
/src – All the source files are under src
/lib – All the libraries required for the execution of tests are under lib. Also all needed jar files are kept here.

Running test xml via terminal

Now lets see how to run testng.xml via terminal 
  1. Open CMD/terminal and navigate to project folder
  2. Now we have two ways to execute the testng xml.
    a) Firstly, we need to set classpath from terminal. To set classpath we need to mention the bin(containing the packages) folder and required jar files folder(in this case it is lib).
    set classpath=[Project Folder]\bin;[Project Folder]\libs\*
    Secondly to execute script we have,
    java org.testng.TestNG testng.xml
    b)Instead of  giving 2 different commands, we can combine both as a single command to execute the testng.xml file.
    java -cp “.\bin;.\libs\*” org.testng.TestNG testng.xml
    Here, we are adding the TestNG JAR and the project compiled code to the Java classpath by using the -cp option of Java.
  3. We can find the result of test which are passed/failed or skipped.

Execute multiple testng.xml files

If we need to execute multiple xml files, we can use the previous command by passing the other XML files as arguments to the terminal.

The following is a sample command:

java -cp “.\bin;.\JarFiles\*” org.testng.TestNG testng.xml testng1.xml

Executing a particular test from testng.xml file

To execute a particular test from the testng XML file, we can use option -testnames at the terminal with comma-separated names of tests that need to be executed.

The following is a sample command:

java -cp “.\bin;.\JarFiles\*” org.testng.TestNG -testnames “Regression” testng.xml

. . .

Ask a Feature

Index