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.
Related
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.
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 .
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
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.
iam learning selenium inorder to reproduce it on my application.so,please help me by answering my questions.1)how to execute bulk of testcases at a time by using automation tool (selenium 2)?)how to start my application to test with automation tool selenium rc?
To use selenium API, you need to download the needed .jar files from here
Once you add the needed .jar files to your projects classpath, you are ready to start doing testing.
Here a very simple hello world application example that can help you understand selenium tests. (As you see there is no call to main or anithing similar, the tests will run automatically when the application is launched)
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import junit.framework.TestCase;
public class HelloSeleniumTest extends TestCase {
private Selenium browser;
public void setUp() {
browser = new DefaultSelenium("localhost",
4444, "*firefox", "http://www.google.com");
browser.start();
}
public void testGoogle() {
browser.open("http://www.google.com/webhp?hl=en");
browser.type("q", "hello world");
browser.click("btnG");
browser.waitForPageToLoad("5000");
assertEquals("hello world - Google Search", browser.getTitle());
}
public void tearDown() {
browser.stop();
}
}
Before you run the app, you should start the RC server from the console. It is very simple, just:
1- Go to the Selenium-Server folder using the console (The place where are the files that you downloaded)
2- execute java -jar selenium-server.jar
Once is running, go back to your programming IDE and run the application
Also you have the possibility of downloading the Selenium plugin for firefox, that will create for you the java code when you navigate the pages so your testing will go faster.
This is how it looks like:
If something still unclear, visit this link, it is very well explained.
Download demo with sample test and try with it