0

Written the Test Script in TestNG framework where the Open browser activity is happening in "baseClass" under @BeforeTest annotation and the Test Script is written in Test Packages under as @Test Annotation. IN the test Script I am extending the Base Class so that my @beforetest(opening the browser) will work first and then test case functionality should execute. But at TestScript I am getting NULLPointerException. @BeforeTest methods are returning the webdriver object and I thought it will work at @Test methods but this is failing

Can anyone guide me here how to proceed further?

public class BaseClass {
    public WebDriver driver;
    @BeforeTest
    public WebDriver openBrowser()
    {
        System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"/Drivers/geckodriver.exe");
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(120));
        driver.manage().window().maximize();
        driver.get("https://app.vwo.com/#/analyze/heatmap/129/reports?token=eyJhY2NvdW50X2lkIjo2LCJleHBlc%20mltZW50X2lkIjoxMjksImNyZWF0ZWRfb24iOjE1MDc3ODk0ODcsInR5cGUiOiJjYW1wYWlnbiIsI%20nZlcnNpb24iOjEsImhhc2giOiJiMzlmMTQ4MWE0ZDMyN2Q4MDllNTM1YzVlNWFjOGVlMCJ9");
        return driver;
    }

public class Assignment extends BaseClass {
    WebDriver driver;
    CommonMethods common = new CommonMethods();
    
    @Test
    public void executeTest()
    {
        WebElement heatMap_locator=driver.findElement(By.xpath(common.readPropertyFile("viewheatmap_xpath")));
        common.clickJavaScriptExecutor(driver, heatMap_locator);
}

enter image description here

3
  • 2
    please add stacktrace, test class , etc
    – PDHide
    Commented Jun 10, 2021 at 10:10
  • @PDHide added the Test Script Commented Jun 10, 2021 at 10:40
  • Have you tried to see what NullPointerException stands for? Have you looked at your stacktrace? See this answer to see how to read stacktraces and understand exceptions: sqa.stackexchange.com/a/51649/1933
    – dzieciou
    Commented May 7, 2023 at 18:10

2 Answers 2

1

If you extend a class having driver field where that field is initialized do not add filed of the same name to child class. Doing that you're hiding a field of base class.

Here is the example:

Your base:

public class TestBase {

    public Object object;

    @BeforeTest
    public void doBefore(){
        object = new Object();
    }

}

Your test that WILL fail (a.k.a BAD TEST):

public class TestChild extends TestBase{

    Object object;

    @Test
    public void doTest(){
        Assert.assertNotNull(object);
    }

}

Your test that WON'T fail (a.k.a GOOD TEST):

public class TestChild extends TestBase{

    @Test
    public void doTest(){
        Assert.assertNotNull(object);
    }

}
1
  • 1
    Yes I have fixed in this way only and it's working. Thanks for response Commented Jun 10, 2021 at 16:02
0

Create a base class where you write a method of initializing. then you an extend that class to your main test case, then you can initialize that method of initializing in @beforeTest

public static void initConfiguration(){
    System.setProperty("webdriver.chrome.driver",
                    System.getProperty("user.dir") + "\\src\\test\\resources\\executables\\chromedriver.exe");
 driver = new ChromeDriver(options);
 driver.get(baseurl);
 driver.manage().window().maximize();
}

above code, you can write in base class then extend that class to your testcase class.

2
  • I have done the same way. I missed to add test class. Please review once Commented Jun 10, 2021 at 10:40
  • don't use "@beforetest" annotation in your base class. extent base class in your test class and just call the method which is doing launching and navigation in your "@test" annotation Commented Jun 10, 2021 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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