How to Execute TestNG based Selenium Tests from the Command line?

We have discussed why do we use WebDriver driver = new ChromeDriver in our last tutorial. I believe it helped many learners to understand the core concept behind the declaration of WebDriver’s instance. Today we are going to discuss the execution of TestNG based Selenium test cases from the command line.

Why do we need to execute testng based Selenium tests from Command line?

You may ask why do we need to execute selenium tests from the command line when we have user-friendly IDE (Eclipse or IntelliJ or any 3rd party IDE). The immediate answer would be ease in test execution. Whenever you are working on test case building then each day you will add new tests and at the end of the development process, your test case number will increase, hence, in such case you would plan to have single-click execution, instead of waiting for the IDE to load the entire tests. Therefore, in such circumstances, test execution will be easier through the command line.

Test execution through command line technique also helps in running tests remotely on different servers.

Actions class (Selenium): A quick look on keyboard & mouse events

What is the significance of creating TestNG based Selenium tests for Command line execution?

TestNG provides Test-driven development environment for extensive testing and the most important things here is the testng.xml file from where we trigger tests. So when we talk about running Selenium tests from the command line then we actually trigger testng.xml file from the command line and the definition written inside the testng.xml file, which is generally our Selenium-based classes and methods are called from it. Hence, we ultimately call it as TestNG based Selenium tests execution from the command line.

Steps to Execute Selenium tests from Command line

Followings are some of the steps to execute Selenium tests from the command line.

Step# 1: Create a Sample TestNG project to get testng.xml file

Hope at this stage of learning, you all are aware of the creation of maven project with TestNG in your IDE. Once you create your project then make sure you have testng.xml file properly designed.

Project hierarchy

Step# 2: Store all JARs in a folder

You resolve dependencies through the pom.xml file when you are using Maven. All the dependencies’ Jars are stored in .m2 folder so create a new folder and name it as lib and store all the JAR files here. Please make sure that the lib folder is stored inside the project folder. See the project hierarchy image above.

lib

Step# 3: Design your test cases with Selenium WebDriver

Now you can design your test cases with Selenium WebDriver and Java. Just, for example, I have created a sample test case below.

package MyPackage;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;

public class DemoTest {
	WebDriver driver = null;
	
  @Test
  public void testMethod() {
	  driver.get("http://www.inviul.com/");
	  
	  System.out.println("Title is: "+driver.getTitle());
	  
  }
  @BeforeMethod
  public void beforeMethod() {
	  System.setProperty("webdriver.chrome.driver", "C:\\HP Laptop Data\\HP C drive\\Selenium\\chromedriver.exe");
	  driver = new ChromeDriver();
	  
  }

  @AfterMethod
  public void afterMethod() {
	  driver.close();
	  driver.quit();
	  
  }

}

Step# 4: Navigate to project folder from the command prompt

Open the command prompt and navigate to the project folder. Here are the few basic DOS commands.

cd..- It is used to go back

cd <folder_name> – It is used to open any folder

c: – It is used to change to c drive

d: – It is used to change to d drive

cls- Clear screen

Navigation command prompt

Step# 5: Commands to execute TestNG based Selenium Tests from Command Line

Here I am providing two techniques. You can use any one of them to execute testng based Selenium tests from the command line. Each one is an alternative to each other.

Technique# 1

Open the command prompt and use the following command:

java -cp C:\Users\blogg\wsoace\selenium\lib\*;C:\Users\blogg\wsoace\selenium\bin org.testng.TestNG testng.xml

Execute TestNG Selenium Tests command line 1

Technique# 2

This is an alternative technique.

set classpath=C:\Users\blogg\wsoace\selenium\bin;

set classpath=C:\Users\blogg\wsoace\selenium\lib\*;

java org.testng.TestNG testng.xml

Execute TestNG Selenium Tests command line 2

This was all about running TestNG based Selenium tests from the command line. You can post your queries below by using our comment form. If you haven’t joined our Facebook group then you can join by clicking on the below button for quick updates.

Join Inviul fb group

Leave a Reply