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

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.

Related

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.

Cannot find class in classpath: (ClassName)

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 .

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.

How to run java file exported from selenium IDE through command prompt in windows

Can some one tell me how can i run the java file which was exported from selenium IDE through command prompt.
I have used the following command: "java -jar selenium-server.jar -htmlSuite "*firefox" "http://www.google.com" "C:\mytestsuite\mytestsuite.html" "C:\mytestsuite\results.html"
Able to launch selenium functional test runner but nothing is executed there.
The converted tests are JUnit tests. So you should have two processes:
Your selenium server process:
java -jar lib/selenium-server-standalone-2.28.0.jar
Your JUnit test runner
"java -cp /usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name] "
If you have several test classes, it might be better to create a TestSuite with the Suite annotation:
#RunWith(Suite.class)
#SuiteClasses({
MyTestClass1.class,
MyTestClass2.class})
public class TestSuite {
...
If you are using Spring, you can setup config containing selenium server address, browser, ...
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "classpath:my/package/seleniumConfig.xml"})
public abstract class SeleniumTestSuite {
The java test cases exported don't compile because they require at least the Selenium library to compile and may need junit or TestNG as well to actually run. I really suggest that you do this from within eclipse.
You can get eclipse 32 or 64 bit here. Then create a new Java project by going File-> New -> Java Project. You can get the Selenium client zip here. You need to get the client jar (called selenium-java-2.31.0.jar) out of this zip and put it in the lib directory of your new Java project in eclipse. You may have to create the lib directory and then right-click the jar file in the lib directory and "add to build path".
Put the java code that was generated by the Selenium IDE into the src directory of the new Java project in eclipse. (you may need to create the appropriate packages under src etc). Then right-click the test case java file that you want and select "Run As ... Junit". This should run it for you. If you get compile or run errors you can update your question above.
This is the solution:
Test is your Class mande by selenium
import org.junit.runner.JUnitCore;
import com.example.tests;
public static void main(String[] args) {
Result result = JUnitCore.runClasses(Test.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
}

JUnit4 in Eclipse

I'm trying to run some JUnit test units in Eclipse 3.5, but with no luck at all.
JUnit3 works fine.
When I create the JUnit4 Test unit, Eclipse offers to add the JUnit library to the class-path. I accept, but when I check to see if it was added in the project's properties panel, I can see JUnit4 was added, but no JARS where included.
If I choose Edit, the combo says "JUnit 4", and just below "Current location: Not Found".
When I launch a JUnit4 test, I get an error saying and internal error occurred, NullPointerException.
I've read for about two days now, and all references say eclipse INCLUDES JUnit4, but somehow, there seems to be something I'm missing.
I've tried re-creating my projects and creating a test in brand new ones with no luck.
package test;
import static org.junit.Assert.*;
import org.junit.Test;
public class AuthServiceTest {
#Test
public final void testValidateCredentials() {
fail("Not yet implemented"); // TODO
}
}
[Edit]
I've added junit-4.8.1.jar to the project's classpath, and eclipse's classpath, but still the same problem.
[Edit2]
I also added junit-dep-4.8.1.jar, since I'm not sure if these dependencies are necessary, but no change.
Right click on the project name .
Built Path--> Add Libraries
Select the appropriate library from the list (JUnit 4 in this instance) .
In my Eclipse installation JUnit 4 is provided, it's in plugins\org.junit4_4.3.1\junit.jar
If you can't find it, then I guess that you may need to download it.
You can associate your JUnit with the Eclipse JUnit settings in
Windows->Preferences->Java->Build Path->User Libraries
Select JUnit there, and you can add and edit JARs.
It appears that the Eclipse 3.5 from Fedora's repository doesn't include JUnit, and installing the appropriate package didn't include it either.
Another Debian PC presented the same issue. A clean download from eclipse.org solved the problem.
I will report this bug at some point :)

Categories