Run TestNG from CMD

On one fine Friday evening you planned to leave the office by 5.30 pm to have dinner with your girlfriend, so you closed all the application in your system just shut-down is left , at 5.29 your manager asks you to run the script and go but you do not have the time at that time you can do two things.

1. Tell your junior to run the script, but you knew that he is never going to obey what you tell,

2. Stay for half an hour and run the script; in this case, your girlfriend going to have dinner with someone else.

So in such a situation, you will be helpless, so to avoid such a situation, we used to go for running the script from CMD.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Reporter;
import org.testng.annotations.Test;

public class TestNGClass
{
	WebDriver driver=new FirefoxDriver();
	@Test
	public void openSeleniumMentor()
	{
		Reporter.log("opening chercher.tech");
		driver.get("https://chercher.tech");
		Reporter.log("selenium-webdriver.coom opened");
	}

	@Test
	public void openGoogle()
	{
		Reporter.log("opening google.com");
		driver.get("https://www.google.com");
		Reporter.log("google opened");
	}
	@Test
	public void openGmail()
	{
		Reporter.log("opening gmail");
		driver.get("https://gmail.com");
		Reporter.log("gmail opened'");
	}

}

1.Gather all the jars to a single folder(jars) under the project folder before writing any code
i.selenium server

ii.testng jar

iii. jcommander jar

With the latest TestNG, it is mandatory to place the Jcommander along with other jars; otherwise, you will face the below error.

Error: Could not find or load main class org.testng.TestNG
Caused by: java.lang.ClassNotFoundException: org.testng.TestNG

jars%20selenium%20webdriver

jar%20folder%20selenium%20webdriver

1.Open CMD

cmd%20selenium%20webdriver

2.Navigate to the folder where you have the project

cmd%20cd%20selenium%20webdriver

cmd%20project%20selenium%20webdriver

3. Write the write below code in CMD and hit the enter button,

java -cp bin; jars/* org.testng.TestNG testng.xml

cmd%20code%20selenium%20webdriver

3.script starts running

cmd%20execution%20selenium%20webdriver

4. After running it prints result on cmd terminal

cmd%20result%20selenium%20webdriver

Printing on the CMD
Whatever you are printing on the console will be printed onto CMD terminal if you run a script from CMD

Create QR Code

Run TestNG From CMD

import org.testng.annotations.Test;

public class Simple
{

	@Test
	public void openSeleniumMentor()
	{
		System.out.println("open Selenium Mentor method");
	}

	@Test
	public void openGoogle()
	{
		System.out.println("open Google method");
	}
	@Test
	public void openGmail()
	{
		System.out.println("open gmail method");
	}
}

print%20on%20cmd%20selenium%20webdriver

Make it Simple :
In the above scenario, you have to run the script after opening the specified path and then type the code to run. It becomes lengthy if you run all the time.

Follow the steps to short it

1.Open notepad

notepad%20selenium%20webdriver

2. Write the below code into notepad(same as we used before); we should give newline between the lines then only it presses enter after the cd command line if there is no line gap means it will be considered as a single command

notepad%20code%20selenium%20webdriver

3. Save the notepad file as "run.bat."

run%20selenium%20webdriver

4.Double click the run.bat or Right-click -> Open

run%20folder%20selenium%20webdriver

open%20run%20selenium%20webdriver

Option Present or Not in Dropdown in selenium

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions