Quality Testing

Quality is delighting customers

java.lang.NullPointerException on clicking button in webdriver test

Hi All,

I am getting Null pointer exception on button click in web driver test.

(Code line is in bold)

below is the button imaage and html for application

And below is the written code with exception           

    Start(); 
    String winHandleBefore = NavigateToCOSstart();
    TestLogin("abc","password");
    int count;
    count = CartsCurrentCount();
    System.out.println(count);
    if (count != 0){
    System.out.println("before.......");
    driver.findElement(By.xpath("//button[@class='button btn-continue']")).click();
    }
    else{
        driver.findElement(By.linkText("here")).click();
    }
    driver.findElement(By.xpath("//ul[@class='mm-carousel-ul']/li")).click();



and exception is  :


java.lang.NullPointerException
    at Test.Maven.MyCartTest.MyCart(MyCartTest.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Views: 6949

Reply to This

Replies to This Discussion

This is the problem of Chrome Browser and this is persistent error in Chrome Browser so I would suggest to use another browser like FF or IE for the same or use javascriptExecutor for the same.

Code for that is 

JavascriptExecutor jse = (JavascriptExecutor)driver; 

jse
.executeScript("document.getElementByClassName('button btn-continue').click();")Hope this would help you in hitting the button.


NullPointerException is not from WebDriver side. It is java exception.

I think you might not initialized any variable properly. Just give a try with initializing all variables to some default value.

Use an alternative way

instead of extend base class, Create an object of your base class and call your start() method with object of your base class

Base_class obj = new Base_class();

obj.start();

tried..still not working :(

Thanks guyes..

Got the solution..problem was with driver instantiation.

I Modified :

In Base class:

public WebDriver Start(){
        System.setProperty("webdriver.ie.driver", "C:/IEDriverServer_x64_2.31.0/IEDriverServer.exe");
        String URL = "http://abc/home/default.aspx";
        driver = new InternetExplorerDriver();
        driver.get(URL);
        return driver;
        }

And in test :

driver = obj.Start();

Great ! 

I told you :D

RSS

TTWT Magazine


Advertisement

Advertisement

Advertisement

Advertisement

© 2021   Created by Quality Testing.   Powered by

Badges  |  Report an Issue  |  Terms of Service