DEV Community

loading...
Cover image for How to run a Selenium Project from command line using JAR files

How to run a Selenium Project from command line using JAR files

Manul wickrmanayaka
QA Engineer👨‍💻
・5 min read

What is JAR ?

JAR(Java ARchive) files are archive files that include a Java-specific manifest file. They are built on the ZIP format and typically have a .jar file extension.

Alt Text

In simple words JAR file is a compressed format of a compiled Java project. This file contains with .class files, meta data and some other resources. Also these files are a way to distribute our source code and can be used by external programs.

In here we are focusing on how to run a .jar file from command line without using any IDE.

First step should be we are going to need a java project to create a .jar file and I’ll use one of my previous program which I used to run with selenium and TestNG.

You can refer to my article on “Use of .properties file in selenium project with TestNG” and I’ll take the same code and continue it further.

For your reference find my GitHub project here

PropertiesFile Class:

package test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
public class PropertiesFile {
//Create a object for class properties 
 static Properties prop = new Properties();
 //defining the project path
 static String projectPath = System.getProperty("user.dir");
//setting expected title from the ebay.com/Daraz.lk
 static String expectedTitleebay ="Electronics, Cars, Fashion, Collectibles & More | eBay";
 static String expectedTitledaraz ="Online Shopping Sri Lanka: Clothes, Electronics & Phones | Daraz.lk";
//create the htmlReporter object
static ExtentSparkReporter htmlReporter;
 static ExtentReports extent;
 static ExtentTest test1;
public static String browserName = null;
public static void main(String[] args) throws InterruptedException {
PropertiesFileTestNG.setup();
  PropertiesFileTestNG.test1();
  PropertiesFileTestNG.tearDown();
 }
public static void getProperties() {
try {
//create a object for class InputStream
   InputStream input = new FileInputStream(projectPath + "/src/test/java/test/config.properties");
   //Load properties file
   prop.load(input);
   //get values from properties file
   String browser = prop.getProperty("browser");
   System.out.println(browser);
   //Setting browsername in the PropertiesFileTestNG class
   PropertiesFileTestNG.browserName = browser;
} catch (Exception exp) {
System.out.println(exp.getMessage());
   System.out.println(exp.getCause());
   exp.printStackTrace();
}
}
public static void setProperties() {
try {
//create a object for class OuputStream
   OutputStream output = new FileOutputStream(projectPath + "/src/test/java/test/config.properties");
   //Load properties file and set firefox
   prop.setProperty("result", "test passed");
   //store values i properties file
   prop.store(output, "setting firefox");
} catch (Exception exp) {
System.out.println(exp.getMessage());
   System.out.println(exp.getCause());
   exp.printStackTrace();
  }
}
}
Enter fullscreen mode Exit fullscreen mode

PropertiesFileTestNG Class:

package test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
import io.github.bonigarcia.wdm.WebDriverManager;
public class PropertiesFileTestNG {
//creating driver object
 private static WebDriver driver = null;
//setting expected title from the ebay.com/Daraz.lk
 static String expectedTitleebay ="Electronics, Cars, Fashion, Collectibles & More | eBay";
 static String expectedTitledaraz ="Online Shopping Sri Lanka: Clothes, Electronics & Phones | Daraz.lk";
//create the htmlReporter object
static ExtentSparkReporter htmlReporter;
 static ExtentReports extent;
 static ExtentTest test1;

 public static String browserName = null;

 @BeforeSuite
 public static void setup() {

  PropertiesFile.getProperties();
if(browserName.equalsIgnoreCase("chrome")) {
   //initializing and starting the chrome browser
   WebDriverManager.chromedriver().setup();
   driver = new ChromeDriver();

  }else if(browserName.equalsIgnoreCase("firefox")) {
   //initializing and starting the firefox browser
   WebDriverManager.firefoxdriver().setup();
   driver = new FirefoxDriver();

  }
htmlReporter = new ExtentSparkReporter("extentReport.html");
  //create ExtentReports and attach reporter(s)
  extent = new ExtentReports();
  extent.attachReporter(htmlReporter);

 }
@Test  
 public static void test1() throws InterruptedException {

  //creates a toggle for the given test, add all log events under it
  test1 = extent.createTest("ebay Search Test", "test to validate search box ");
test1.log(Status.INFO, "Starting test case");

  if (browserName.equals("firefox")) {
   System.out.println("Starting test on Firefox");
   test1.pass("Starting test on Firefox");
  }else if(browserName.equals("chrome")) {
   System.out.println("Starting test on chrome");
   test1.pass("Starting test on chrome");
  }

  //maximize the window 
  driver.manage().window().maximize();
  test1.pass("maximize has done");
//Navigate to Ebay.com
  driver.get("https://www.ebay.com");
  Thread.sleep(1000);
  test1.pass("Navigate to Ebay.com");
//compare whether the title id matching
  String actualTitle = driver.getTitle();
  Assert.assertEquals(actualTitle, expectedTitleebay);
  test1.pass("title is correct");
//enter in the TextBox
  driver.findElement(By.xpath("//*[@id=\"gh-ac\"]")).sendKeys("Mobile");
  test1.pass("Entered text in the text box");
  Thread.sleep(1000);
  //hit enter
  driver.findElement(By.xpath("//*[@id=\"gh-btn\"]")).sendKeys(Keys.RETURN); 
  test1.pass("Press keybopard enter key");
  Thread.sleep(1000);

 }

 @AfterSuite
 public static void tearDown() {
driver.quit();
  test1.pass("closed the browser");
test1.info("test completed"); 

  //write results into the file
  extent.flush();
PropertiesFile.setProperties();
 }
}
Enter fullscreen mode Exit fullscreen mode

config.properties:

browser=chrome
Enter fullscreen mode Exit fullscreen mode

Since we were calling main() from the PropertiesFileTestNG class we won’t be able to run the actual test in main() method class. For that I have pasted this code in the class level in PropertiesFile Class.

//setting expected title from the ebay.com/Daraz.lk
 static String expectedTitleebay ="Electronics, Cars, Fashion, Collectibles & More | eBay";
 static String expectedTitledaraz ="Online Shopping Sri Lanka: Clothes, Electronics & Phones | Daraz.lk";
//create the htmlReporter object
static ExtentSparkReporter htmlReporter;
 static ExtentReports extent;
 static ExtentTest test1;
public static String browserName = null;
Enter fullscreen mode Exit fullscreen mode

Copy all the above codes to your project and run it.

This will go to the site www.ebay.com and then it will validate site by comparing Title. After will enter "mobile" in the search box and hit enter.

I have used @test annotation to execute the test case which is test1 and used extent Reports as the reporting library.

After a successful test it will call the PropertiesFile and write the status of our test to the .properties file.

So in above code we had to change the browser name as "chrome" or "firefox" by opening the project. So the same thing can be done with CMD without using Eclipse.

So First lets create a .jar file by right clicking on the project and going to export option.

You will see this window and click Runnable JAR file.

Alt Text

Under Launch Configuration select the class which you included the main() method. In our case it is PropertiesFile.class

Give a name to the JAR file (in my case I named it as selenium.jar ) and define the export Destination and Finish.

Alt Text

Go to the physical location that you have saved the JAR file and you will see it created there as selenium.jar

Now we have created our JAR file and lets run it from the command line.

Open the cmd and navigate to the selenium.jar file location.

Enter this command to execute the selenium.jar file and hit Enter.

java -jar selenium.jar
Enter fullscreen mode Exit fullscreen mode

You will see the progress in the cmd and passing all tests.

Alt Text

You will see the SOP() outputs we print in the console and its passing. Also we can go to extent reports and see the test results.

Alt Text

In this article we created a JAR file and ran a selenium TestNG framework using command line without using any IDE.

𝑻𝑯𝑰𝑵𝑲 𝑸𝑼𝑨𝑳𝑰𝑻𝒀!

Discussion (0)