0

I have started TestNG and wrote the first program in it but I cannot be able to run because of an error "Cannot find class in classpath: FirstTestNGFile" (here FirstTestNGFile is a class name). Here is my code.

package firsttestngpackage;

import org.openqa.selenium.*;
import org.testng.annotations.*;
import org.testng.Assert;
import org.openqa.selenium.chrome.ChromeDriver;

public class FirstTestNGFile {
	
	public String baseURL = "https://www.google.com/";
	String driverpath = "D:\\Ashish\\Setup\\chrome\\chromedriver.exe";
	public WebDriver driver;
	
  @Test
  public void verifyHomepageTitle() {
	  
	  System.setProperty("webdriver.chrome.driver", driverPath);
	  driver = new ChromeDriver();
	  driver.get(baseURL);
	  String expectedtitle = "Google";
	  String Actualtitle = driver.getTitle();
	  Assert.assertEquals(expectedtitle, Actualtitle);
	  driver.close();  
	  
  }
}

Here is the Exception:

Cannot find class in classpath: FirstTestNGFile
    at org.testng.xml.XmlClass.loadClass(XmlClass.java:77)
    at org.testng.xml.XmlClass.init(XmlClass.java:69)
    at org.testng.xml.XmlClass.<init>(XmlClass.java:55)
    at org.testng.xml.TestNGContentHandler.startElement(TestNGContentHandler.java:575)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)

Note: I already cleaned my project and that did not resolve my problem.

12
  • 2
    Please don't SHOUT! Explanation: All uppercase sentence is interpreted as screaming. Please change it to normal case. Commented Dec 26, 2017 at 13:48
  • 1
    You havent shown us the TestNG xml file that you are working with. The error clearly states that you are referring to your class, without its package name in your testng xml suite file. Commented Dec 26, 2017 at 14:10
  • 1
    @AshishSavaliya - Not sure what do you mean by please write program here with xml file code which you are asking about. You haven't told us how you are running your tests. You need to be adding more information around: 1. How are you running tests (feel free to update your question with detailed information) 2. Include the TestNG suite xml file that you are using. Your stacktrace seems to indicate you are using one. Commented Dec 27, 2017 at 5:46
  • 1
    @AshishSavaliya How are you running your test ? How does your project structure (folder structure) look like ? Please add all this information. Else its difficult to even guess as to what the problem is, apart from just telling you "fix your TestNG suite xml file" Commented Dec 27, 2017 at 5:54
  • 1
    @AshishSavaliya - Please edit your question and add the screenshot to your question instead of sharing a google drive link which requires a login! Commented Dec 27, 2017 at 6:50

5 Answers 5

2

If you are working with Maven and executing testng.xml from pom.xml then below statement will help you solve your problem

"By default, Surefire-plugin runs all tests that matches with filename pattern such as *Test.java in test source directory src/test/java . To use a different naming scheme, we can configure Surefire Plugin which includes parameter and specify the tests that we want to include."

Sign up to request clarification or add additional context in comments.

Comments

2

I have reinstalled TestNG and it works for me now.

Comments

1

The error says it all :

Cannot find class in classpath: FirstTestNGFile
    at org.testng.xml.XmlClass.loadClass(XmlClass.java:77)
    at org.testng.xml.XmlClass.init(XmlClass.java:69)
    at org.testng.xml.XmlClass.<init>(XmlClass.java:55)

The error stack trace clearly indicates TestNG is unable to initialize properly.

You code looks fine to me but you have to take care a couple of points as follows :

  • You need to be specific with the set of imports you are using :

    import org.openqa.selenium.WebDriver;
    import org.testng.annotations.Test;
    import org.testng.Assert;
    import org.openqa.selenium.chrome.ChromeDriver;
    
  • To end your script invoke driver.quit(); to kill the WebDriver and Web Browser instance.

  • Clean the Project Workspace from your IDE to build it afresh.

  • You may consider to close your IDE and take a System Reboot.

  • Execute your Test.

  • If problem still persists you may consider to update the question with the testng.xml which you are using.

Comments

1

So this worked for me :

Got "cannot find class path" even after reinstalling TestNg, Updating Maven , Cleaning projects, changing JRE multiple times.

But this worked:

  1. Go to your .m2 repository. (Mine c:/users/.m2/repository)
  2. Delete entire repository folder.

Rebuild your project, it will reinstall the dependencies again. And now run your files.

This thing worked for me, hope it will work for you guys as well.

Comments

0

Just clear your project space from PROJECT---> CLEAR Then run again .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.