Cannot find class in classpath: (ClassName) - java

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.

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."

I have reinstalled TestNG and it works for me now.

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.

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:
Go to your .m2 repository. (Mine c:/users/.m2/repository)
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.

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

Related

Eclipse : Used testNG 7 without built with maven ? ClassPath issue

I'm writting some testCases on Windows in java using eclipse IDE, version : 2019-06 (4.12.0), selenium WebDriver 4.0.0, testNG 7.0.0-beta7 and PageFactory POM.
I can't build my test case in eclipse or in cmd line, because eclipse doesn't recognized my import of testNg libraries. TestNG is well installed on my project.
I'm supposed that is a class path issue. I would like to know if it's because i'm not using Maven (I don't have to used it for the moment), or if there is some issues with the latest version of testNG lib ?
My java test using testNG.xml worked perfeclty in eclipse with my testNg version: 6.14. I tried to execute it with my cmd line, I had a NullPointerError error. There was a bug with this exception in the previous versions of testNG: https://github.com/cbeust/testng/pull/1811 and they fixed it on the latest version (7.0.0).
I installed the new version of testNG, and test everything I found on the web, but nothing worked. I can't build my test anymore.
CreateSingleOrder_BondsPage.java
import org.openqa.selenium.Keys;
public class CreateSingleOrder_BondsPage {
WebDriver driver;
public CreateSingleOrder_BondsPage(WebDriver driver) {
// TODO Auto-generated constructor stub
this.driver = driver;
}
#FindBy(id="M1_3") WebElement tabOrderManagement;
public void hoverTabOrderManagement() {
Actions action = new Actions(driver);
action.moveToElement(tabOrderManagement).perform(); //move
to the element
}
}
CreateSingleOrder_BondsTest.java
import org.testng.annotations.Test;
public class CreateSingleOrder_BondsTest extends BasicTest { /*BasicTest
contains initialization of driver in #BeforeMethod and #AfterMethod also
*/
#Test
public void buyBonds() throws Exception {
CreateSingleOrder_BondsPage createBonds =
PageFactory.initElements(driver, CreateSingleOrder_BondsPage.class);
Thread.sleep(1000); //loading data
createBonds.hoverTabOrderManagement();
}
}
I have these errors :
for my import in my java class.
The import org.testng cannot be resolved
when I runned my TestNG.xml as "TestNG Suite"
An internal error occurred during: "Launching Test2 TAP_TestNG.xml".
java.lang.NullPointerException
in the tab "Problems" of eclipse
Invalid classpath container: 'TestNG' in project 'Test2 TAP' - Test2
TAP -Build path - Build Path Problem
If anyone can help me with the idea of using maven for the class path, if it's could be a solution or other solutions I can use to run my test again would be very helpful.
Thanks for your help.
This could be due to some trivial issues. A couple of checks:
Have you installed the TestNG plugin for eclipse? If not go to marketplace and do that
Link for TestNG- Eclipse
Make sure the build path for your project has TestNG libraries. If not go to build path and click on Add Library. This will allow you to add TestNG libraries.
This has nothing to do with Maven and should work independently as well.
You can also refer TestNG-Eclipse doc
I believe it is happening due to multiple versions of testng in your .m2 folder. There are 2 solutions I can think of
Delete all the items in TestNG folder and recompile the project.
Delete TestNG versions which you don't require and then recompile the project.

chrome Webdriver can't be resolved to a type error eclipse and java

Im trying to do some automation projects with chrome, java and selenium and having problems importing the chrome driver.
package main;
import org.openqa.selenium.*;
public class SitePoster {
public static void main(String[] args) {
//System.setProperty("webdriver.chrome.driver", "./pathtodriver");
WebDriver driver = new ChromeDriver();
//Getting error saying "ChromeDriver can't be resolved to a type"
}
}
chrome Webdriver can't be resolved to a type error eclipse and java:-
I was getting same problem with selenium -java 3.141.59 on macOs.Check below steps to resolve issue:-
Create a new Java Project >class >Create Class name
Right click on Project >Property > Build path > Libraries
Add external Jars to Classpath download from selenium client & web
driver language binding.
Apply and Close.
Hope you got your Issue fixed. Kindly reply back in case of any issue.
Even after installing the required jar files I was facing the same error message and then I updated the compiler compliance level in the Java compiler to 1.8(depends) and then error disappeared. It solved my issue.
You can follow below steps:
Select the Java project(created one)
Build Path
Configure build path
Java compiler
Change the compiler compliance level(I selected 1.8)
Apply and Close.
The solution for me was the POM specified an older version of ChromeDriverManager which was how chromeDriver was obtained in my build. If you are specifying a location using setProperty this will of course not apply to you. But it helps to check with the version of the dependencies in your POM
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.6.1</version>
<scope>test</scope>
</dependency>
That did it!
Add Selenium Jar Files to ClassPath instead of ModulePath
ChromeDriver is one extra level nested in org.openqa.selenium package. Try with import statement
org.openqa.selenium.chrome.*;
OR
import org.openqa.selenium.chrome.ChromeDriver;
The error says it all :
"ChromeDriver can't be resolved to a type"
While working with Selenium 3.x you have to mention the Key-Value pair through System.setProperty() line mandatory as follows :
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
Additionally, as per best practices instead of import org.openqa.selenium.*; you have to mention the individual packages for import as follows :
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
I was getting the exact same error. It did resolve for me after performing the below steps.
Right click on Project>Buildpath>configure build path
Libraries>Add external Jars.
Add the external Jars from the 'libs' folder first, then add the 'Client component' ones.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class test1 {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\nsukumar\\Documents\\selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://google.co.in");
}
}
Try to edit external jars in the ModulePath & Classpath.
See on the picture link -->
You need to attach jar files. I was getting the exactly the same issue.
you need to install "Selenium 3.4.0" jar files.
Which contains lib folder of .jar files and "client-combined-3.4.0-nodeps" files.
After you attach these jar files refresh the code by giving run command.
Adding Selenium jar files from the build path > add external libraries fix the issue for me.
For Linux user go to terminal check google version with below command
google-chrome --version
OP:- Google Chrome 69.0.3497.100
Now, Upgrade chrome using below command
sudo apt --only-upgrade install google-chrome-stable
After this check the google chrome version using above same command
google-chrome --version
OP:- Google Chrome 78.0.3904.87
Now you need to download the same version google driver. you can find it from the below link.
https://chromedriver.storage.googleapis.com/index.html?path=78.0.3904.70/
Download google driver and extract it and save the path and use it in the code, Now run the code.
if you are not able to add this statement
import org.openqa.selenium.chrome.ChromeDriver;
the probable reason is you are using maven and you have not included the below
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.141.59</version>
</dependency>
Even if we put the below statement, problem still comes
System.setProperty("webdriver.chrome.driver", "C:\\Software\\chromedriver_win32\\chromedriver.exe");
So the solution is goto maven repository site and take the chrome dependency from there to solve this problem
This type of error occurs when you have added the external jars in the ModulePath. To resolve this issue, you can remove the external jars from the node "Modulepath". Select the node "Classpath" then add the external jars. Review that all the jars are under the node "Classpath"
When adding external jars, make sure you add the jars in the lib folder first before adding the ones in the main selenium-java folder. This made the difference for me.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class testdemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","C:\\Users\\victo\\OneDrive\\Documents\\driverchrome\\ChromeDriver.exe");
WebDriver driver=new ChromeDriver();
driver.get(" https:google.com");
I faced this error when using Latest Eclipse 2021-03 and Selenium 3
To resolve this, add your Selenium standalone jar file in Classpath instead of Module path. also do not forgot to delete the existing Jar files in modulepath, apply and then apply&close.
Even after installing the required jar files I was facing the same error message and then I updated the compiler compliance level in the Java compiler to 1.8(depends) and then error disappeared. It solved my issue.
You can follow below steps:
Select the Java project(created one)
Build Path
Configure build path
Java compiler
Change the compiler compliance level(I selected 1.8)
Apply and Close.
This answer helped me as well. I changed my compliance level from 15 to 1.8.
Ran in to this problem today and it turns out I simply forgot to add the .jar files from the 'lib' folder. Hope this helps someone.

"FirefoxDriver cannot be resolved to a type" when running Selenium webdriver

The basic code I am using is as follows:
Package TestSelenium;
import org.openqa.selenium.WebDriver;
public class MyFirstClass {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://www.google.com")
}
}
However, I am getting the error
FirefoxDriver cannot be resolved to a type
I have included all required jars, but still, I am getting this error.
I am using Selenium 3.60:
C:\Users\Ankur>javac -version
javac 1.8.0_144
Screenshot of all required jars:
You need add the below import statement on the top of your page
import org.openqa.selenium.firefox.FirefoxDriver;
it will still throw some exception, because from selenium 3.X on wards you can't directly launch the firefox browser. You need to download browser driver from selenium.hq.org and in your code you need use System.setProperties method or desired capabilities class to specify the location where your browser driver is available.
The error you are seeing FirefoxDriver cannot be resolved to a type says it all. It means the IDE you are using i.e. Eclipse is unable to resolve the keyword FirefoxDriver.
As you can see from the snapshot you shared, the keyword FirefoxDriver is underlined with a red line which indicates the lack of resolution. The reason for that is we havn't added the required import. FirefoxDriver is defined with in org.openqa.selenium.firefox.FirefoxDriver. So we have to import org.openqa.selenium.firefox.FirefoxDriver as well.
Again, if you only add org.openqa.selenium.firefox.FirefoxDriver in your imports still we would face a couple of errors moving a head because we haven't mentioned the location of the geckodriver binary i.e geckodriver.exe in our code block. We need to download the geckodriver.exe from this location and place it in our system and provide the absolute path of the geckodriver.exe through System.setProperty() as follows:
package TestSelenium;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MyFirstClass
{
public static void main(String[] args) throws Exception
{
System.setProperty("webdriver.gecko.driver", "C:\\your_location\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
}
}
Try adding selenium stand alone server(https://docs.seleniumhq.org/download/) to your project (even though you are using maven), as an external jar and try.
For me this resolution worked.
Right click on your Maven project > Properties > Java Buildpath > Libraries tab Click on Add External jar button > Browse to the folder where the jar is saved > upload >Apply and close

Selenium using JUnit

I am getting a null pointer exception when I try to run the following code:
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Mytest {
WebDriver driver = new FirefoxDriver();
#Test
public void test() {
driver.manage().window().maximize();
System.out.print("Window maximized");
driver.get("http://www.google.com");
System.out.print("Site Open");
driver.quit();
System.out.print("End of Test");
}
}
I know that my JUnit.jar is included in the build path and all appropriate Selenium testing jars, and that I can actually create a "new JUnit test" from the Eclipse menu, but the catch might be that I downloaded Eclipse with ADT for android package. I don't know if this is a problem for JUnit as I have never used it before. Can anyone see something I don't? Thanks in advance.
JUnit and Eclipse (Mobile Developer Package) seem to have a different structure than required when trying to use Selenium WebDriver and JUnit. The solution for me was to install Eclipse J2EE edition and the test ran without issue.

How to Create a Compatible JAR File for Selenium Tests?

I have created a script to perform few automated tests in Selenium for a website using Java in Eclipse.
My aim here is to create a JAR file for my automated tests so that when the file is simply executed, the tests will run on any other system configured with a Selenium environment. For that I have created a runnable JAR file from Eclipse by clicking on the Export option from the File menu. The name of the JAR file is Test MyWebsite.jar.
The source code of my Java classes is given below:
Main.java
package testproject.main;
import testproject.testmywebsite.*;
public class Main {
public static void main(String[] args) {
TestMyWebsite tmw = new TestMyWebsite();
tmw.testMyWebsite();
tmw.stopTest();
}
}
TestMyWebsite.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.*;
import testproject.testmywebsite.tools.*;
import testproject.testmywebsite.login.*;
public class TestMyWebsite {
private WebDriver driver;
public TestMyWebsite() {
setUp();
}
private void setUp() {
// Create a new instance of the Chrome driver
driver = new ChromeDriver();
driver.manage().window().maximize();
}
public void testMyWebsite() {
testLogin();
}
public void testLogin() {
TestLogin tl = new TestLogin(driver);
tl.testLogin();
}
public void stopTest() {
//Close the browser
driver.quit();
}
}
TestLogin.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestLogin {
private WebDriver;
public TestLogin(WebDriver driver) {
this.driver = driver;
}
public void testLogin() {
//Perform the login test
}
}
The problem here is that despite setting up and configuring the environment on other computers with the Selenium webdriver, copying the Test MyWebsite.jar in the third-party computer and running the file by double-clicking it, the tests do not run successfully. What happens is the Chrome browser opens momentarily and then closes without opening the 'MyWebsite's' URL. In other words, only the two lines of code in the Main.java class are executed. The JAR file is unable to find the other two associated classes in the Eclipse project. However when I execute the file in my computer the tests are perfectly run, which means that there is a problem with how I have created the JAR file. Please note that these are just three classes of the 12 classes I have created in 5 different packages in my Eclipse project to make understanding easier.
Can anyone please tell me where am I going wrong in creating my JAR file which will run both on my computer as well as on another computer configured with the Selenium environment? As a beginner in Selenium I had followed these instructions while creating my Selenium project in Eclipse. Replies at the earliest will be highly appreciated.
Thank you.
Okay I found out the solution to the problem myself.
The problem lied in the way I was creating the Runnable JAR file in Eclipse. I was selecting the Extract required libraries into generated JAR option (selected by Eclipse by default) under the Library handling label on the first page. By doing so it was not packing the required external Selenium, TestNG and JRE System Library, JAR file libraries in my runnable JAR, hence it was not able to find the required classes. My JAR started working perfectly after selecting the second option, Package required libraries into generated JAR.
For one thing, you should use a test runner, such as JUnit or TestNG, to run your tests. Another thing is to make sure you are using Maven and conforming to its 'standard directory layout'. By using Maven, you will be able to run the 'install' or 'package' goal/task to create the standalone 'executable jar'. Keep in mind that you will need to configure Maven in a unusual way , where you tell it to include all files under 'src/main/java' and 'src/test/java' into the same single .jar file. Also, you will need to configure Maven in such a way as to create a .jar that is actually executable.

Categories