0

I'm new to JAVA and Selenium and I really want to understand why my code doesn't work and the NullPointerException is thrown.

Basically, what I want to do is call a method that has WebDriver implementations from a different class to "Master Test" class that will be executed as a JUnit test.

But every time I execute my Master Test then NullPointerException is thrown.

Here's my Master Test that will be executed:

package common_methods;

import org.junit.*;

public class Master_Test extends Configurations {

    @Before
    public void setUp(){
        try{
        testConfiguration();
        driver.get("http://only-testing-blog.blogspot.ro/");
        } catch (Exception e){
            System.out.println("setUp Exception: "+e);
        }
    }

    @After
    public void tearDown(){
        driver.close();
    }

    @Test
    public void masterTest(){
        try{
        TestCase1 testy1 = new TestCase1();
        testy1.test1();
        }catch (Exception master_e){
            System.out.println("Test Exception: "+master_e);
        }
    }
}

Now for better understanding here is the Configurations Class that is being extended:

package common_methods;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Configurations {

    public WebDriver driver;

    public void testConfiguration() throws Exception{
        System.setProperty("webdriver.chrome.driver", "D:\\Browser_drivers\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
    }
}

And here is the TestCase1 Class from which I get my method:

package common_methods;

import org.openqa.selenium.*;

public class TestCase1 extends Configurations{

    public void test1() throws Exception{
        driver.findElement(By.id("tooltip-1")).sendKeys("Test Case 1 action");
        Thread.sleep(5000);
    }
}

Why do I get the NullPointerException?

1 Answers1

0

In Master_Test, where you are calling

TestCase1 testy1 = new TestCase1();

you need to pass the driver reference, so that it doesn't give the NullPointerException. Also, you would need to add a constructor in TestCase1 class to handle the driver.

Check the code below. I have used it with FirefoxDriver & Google -

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Configurations {

    public WebDriver driver;

    public void testConfiguration() {
        driver = new FirefoxDriver();
    }
}

Master_Test

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;



public class Master_Test extends Configurations {

    @Before
    public void setUP() {
        testConfiguration();
        driver.get("http://www.google.com");
    }

    @Test
    public void masterTest() {
        TestCase1 test = new TestCase1(driver);
        test.test1();
    }
}

TestCase1

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class TestCase1 extends Configurations {

    public TestCase1(WebDriver driver) {
        this.driver = driver;
    }

    public void test1() {
        driver.findElement(By.id("lst-ib")).sendKeys("test");
    }
}
Anish Pillai
  • 1,023
  • 7
  • 8