1

I'm learning to use selenium WebDriver with TestNG plugging for Eclipse and I ran into this issue where I can't execute the test case (it is unfinished but it has no compile errors nor warnings).

This is my testNG class:

package com.selenium.testng.practice;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;

public class NewTest {

    public static final String URL = "https://www.sodimac.cl/sodimac-cl/";
    private WebDriver driver;

    WebElement construccionMenu = driver.findElement(By.xpath("//li[@data-url='/Construccion_reparacion']"));
    WebElement unfoldedConsMenu = driver.findElement(By.xpath("//div[@class='navBar-display __activated']"));
    WebElement seguridadInfantil = driver.findElement(By.xpath("//div[@class='backDisplay __backOn']"
            + "//a[@href='/sodimac-cl/category/cat400014/seguridad-infantil']"));
    WebElement alzadores = driver.findElement(By.xpath("//a[@href='/sodimac-cl/category/cat4850037/Alzadores-y-Sillas-para-autos']"));


    @BeforeMethod
    public void beforeMethod() {        

        System.out.println("Initiating before Method.class..");

        System.setProperty("webdriver.chrome.driver", "./src/test/resources/drivers/chromedriver.exe");
        driver = new ChromeDriver();

        System.out.println("Ending before method...");
    }


    @Test
    public void f() {

        try {

            driver.manage().window().maximize();
            driver.get(URL);

            Actions action = new Actions(driver);
            action.moveToElement(construccionMenu);
            Assert.assertTrue(unfoldedConsMenu.isDisplayed());
            Assert.assertTrue(seguridadInfantil.isDisplayed());
            seguridadInfantil.click();

            Assert.assertTrue(alzadores.isDisplayed()); 
            alzadores.click();



        } catch (Exception e) {         
            System.out.println("Script failed");
        }


    }


    @AfterMethod
    public void afterMethod() {
        //driver.quit();
    }

}

And this is what the console says:

> [RemoteTestNG] detected TestNG version 7.0.0
> org.testng.TestNGException:  Cannot instantiate class
> com.selenium.testng.practice.NewTest  at
> org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:30)
>   at
> org.testng.internal.InstanceCreator.instantiateUsingDefaultConstructor(InstanceCreator.java:193)
>   at
> org.testng.internal.InstanceCreator.createInstanceUsingObjectFactory(InstanceCreator.java:113)
>   at
> org.testng.internal.InstanceCreator.createInstance(InstanceCreator.java:79)
>   at
> org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:114)
>   at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:172)   at
> org.testng.TestClass.getInstances(TestClass.java:102)     at
> org.testng.TestClass.initTestClassesAndInstances(TestClass.java:82)
>   at org.testng.TestClass.init(TestClass.java:74)     at
> org.testng.TestClass.<init>(TestClass.java:39)    at
> org.testng.TestRunner.initMethods(TestRunner.java:463)    at
> org.testng.TestRunner.init(TestRunner.java:342)   at
> org.testng.TestRunner.init(TestRunner.java:295)   at
> org.testng.TestRunner.<init>(TestRunner.java:226)     at
> org.testng.remote.support.RemoteTestNG6_12$1.newTestRunner(RemoteTestNG6_12.java:33)
>   at
> org.testng.remote.support.RemoteTestNG6_12$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_12.java:66)
>   at
> org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:679)
>   at org.testng.SuiteRunner.init(SuiteRunner.java:196)    at
> org.testng.SuiteRunner.<init>(SuiteRunner.java:127)   at
> org.testng.TestNG.createSuiteRunner(TestNG.java:1265)     at
> org.testng.TestNG.createSuiteRunners(TestNG.java:1244)    at
> org.testng.TestNG.runSuitesLocally(TestNG.java:1093)  at
> org.testng.TestNG.runSuites(TestNG.java:1032)     at
> org.testng.TestNG.run(TestNG.java:1000)   at
> org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
>   at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
>   at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77) Caused
> by: java.lang.reflect.InvocationTargetException   at
> sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
>   at
> sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
>   at
> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
>   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
>   at
> org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:23)
>   ... 26 more Caused by: java.lang.NullPointerException   at
> com.selenium.testng.practice.NewTest.<init>(NewTest.java:21)  ... 31
> more

I'm added the libraries using Maven dependencies. Any ideas why I can't execute?

0

You define a class property which tries to initialize with a driver that does not yet exist. So the driver is NULL and you get a NullPointerException. You could move the WebElements to the test method or assign the value after the driver has been set in the beforeMethod

construccionMenu = driver.findElement(By.xpath("//li[@data-url='/Construccion_reparacion']"));
0

You are using the driver object before actually creating its instance. So when you say driver.FindElement(), the driver instance is null, so the execution is throwing a Null Pointer Exception

You can move the below lines into your f() method to fix the error. So your f() will look like the below.

public void f() {

            try {

                driver.manage().window().maximize();
                driver.get(URL);

                WebElement construccionMenu = driver.findElement(By.xpath("//li[@data-url='/Construccion_reparacion']"));
                WebElement unfoldedConsMenu = driver.findElement(By.xpath("//div[@class='navBar-display __activated']"));
                WebElement seguridadInfantil = driver.findElement(By.xpath("//div[@class='backDisplay __backOn']"
                        + "//a[@href='/sodimac-cl/category/cat400014/seguridad-infantil']"));
                WebElement alzadores = driver.findElement(By.xpath("//a[@href='/sodimac-cl/category/cat4850037/Alzadores-y-Sillas-para-autos']"));

                Actions action = new Actions(driver);
                action.moveToElement(construccionMenu);
                Assert.assertTrue(unfoldedConsMenu.isDisplayed());
                Assert.assertTrue(seguridadInfantil.isDisplayed());
                seguridadInfantil.click();

                Assert.assertTrue(alzadores.isDisplayed()); 
                alzadores.click();



            } catch (Exception e) {         
                System.out.println("Script failed");
            }


        }

On a best practice, you can start using Page Object Models to separate your identifiers from tests so that this will give you more re usability. You can refer this link for PageObjectModel design

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.