I am trying to run a Selenium Java Test (jar file) build on Gradle using TestNG.xml from the command.
Note: I am able to successfully run the test case from IntelliJ.
But when I package the project in a .jar file and try to run it from the CLI (Windows 11 powershell), its throwing an error.
On running the Below code
java -cp untitled-0.1-all.jar org.testng.TestNG Testng.xml
Getting this error:
[main] ERROR org.testng.TestNG - Cannot find class in classpath:com.xyz.Test1
Project Structure and Code.
Test1 Class Code:
Its a simple test class opens up chrome browser using selenium and verifies title of the page.
package com.xyz;import com.google.common.util.concurrent.Uninterruptibles;import io.github.bonigarcia.wdm.WebDriverManager;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import org.testng.Assert;import org.testng.annotations.Test;import java.time.Duration;public class Test1 { @Test public void test() { System.out.println(">>>>>>>>>>>>> Starting the test"); //Opening the Chrome brower WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); //Opening google page and verifying the title driver.get("https://www.google.com"); Uninterruptibles.sleepUninterruptibly(Duration.ofSeconds(10)); String actualTitle = driver.getTitle(); Assert.assertEquals(actualTitle, "Google"); //Close the chrome browser driver.quit(); System.out.println(">>>>>>>>>>>>> Ending the test"); }}
Testng.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"><suite name="Suite"><test name="Test"><classes><class name="com.xyz.Test1" /></classes></test></suite>
build.gradle
plugins { id 'java' id 'java-library' id 'com.github.johnrengelman.shadow' version '8.1.1'}group = 'xyz'version = '0.1'repositories { mavenCentral()}shadowJar { // Include test resources from 'src/test/resources' // Include test code from 'src/test/java'}dependencies { //Add impementation & testImplementation as was not sure if this was the source of problem. implementation 'org.seleniumhq.selenium:selenium-java:4.26.0' implementation 'org.testng:testng:7.10.2' implementation 'org.slf4j:slf4j-simple:2.0.9' implementation 'io.github.bonigarcia:webdrivermanager:5.8.0' testImplementation 'org.testng:testng:7.10.2' testImplementation 'org.seleniumhq.selenium:selenium-java:4.26.0' testImplementation 'io.github.bonigarcia:webdrivermanager:5.8.0'}jar { manifest { attributes 'Main-Class': 'com.xyz.Test1' }}test { useTestNG()}
From the powershell (Windows 11) ran the following commands:
> gradle clean> gradle shadowJar
Then ran the below command
> java -cp .\build\libs\untitled-0.1-all.jar org.testng.TestNG .\src\test\resources\Testng.xml
Getting this error
[main] ERROR org.testng.TestNG -Cannot find class in classpath: com.xyz.Test1
I have further checked the .Jar file and it contained the com.xyz.Test1 Class (screenshot below for reference)