Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

While executing the code below , null pointer exception is occuring, as driver of class Pom_MainHerokuapp is always null

testcases:-

package testcases;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Test;

import poms.Pom_MainHerokuapp;

import testbase.TestBase;

public class MainHerokuapp extends TestBase {

    Pom_MainHerokuapp mainHerokuappObject;

    public MainHerokuapp() {

        mainHerokuappObject = new Pom_MainHerokuapp(driver);

    }

    @Test(priority = 0)

    public void TestMainpagetitle() {

        mainHerokuappObject.VerifyTitles();

    }

    @Test(priority = 1)

    public void TestABTestingText() {

        mainHerokuappObject.VerifyTextOfABTesting();

    }

}

TestBase class:-

package testbase;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.BeforeTest;

public class TestBase {

    public WebDriver driver;

    @BeforeTest

    public void setup() {

        driver = new FirefoxDriver();

        driver.get("https://the-internet.herokuapp.com/");

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

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    }

}

Also:

package poms;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.support.FindBy;

import org.openqa.selenium.support.PageFactory;

import org.testng.Assert;

public class Pom_MainHerokuapp {

    public WebDriver driver;

    String text;

    String StringMaintext;

    String Stringsubtitle;

    @FindBy(xpath = "//html//body//div[2]//div//h1")

    WebElement Maintitle;

    @FindBy(xpath = "//html//body//div[2]//div//h2")

    WebElement Subtitle;

    @FindBy(linkText = "A/B Testing")

    WebElement ABTesting;

    @FindBy(xpath = "//html//body//div[2]//div//div//h3")

    WebElement ABTestingText;

    create constructor of this class

    public Pom_MainHerokuapp(WebDriver driver) {

        this.driver = driver;

        PageFactory.initElements(driver, this); // Initialization all webelements  

    }

    public void VerifyTitles() {

        StringMaintext = Maintitle.getText();

        Stringsubtitle = Subtitle.getText();

        System.out.println(StringMaintext);

        System.out.println(Stringsubtitle);

        Assert.assertEquals(StringMaintext, "Welcome to the Internet");

        Assert.assertEquals(Stringsubtitle, "Available Examples");

    }

    public void VerifyTextOfABTesting() {

        ABTesting.click();

        text = ABTestingText.getText();

        System.out.println(text);

        Assert.assertEquals(text, "A/B Test Variation 1");

    }

}

the error is :-

FAILED: TestMainpagetitle java.lang.NullPointerException at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)

1 Answer

0 votes
by (62.9k points)

This isn't hard to explain. The default constructor of your test class MainHerokuapp will be called as soon as it is run, i.e. when the driver is still null - before your @BeforeTest method, where the driver gets set.

 

MainHerokuapp and TestBase seem to be mixed-up rather than having a clear separation, so you'd be better off merging them back into one.

 

Another way is to restore control to the child class by dropping the constructor, moving the @BeforeTest there, and calling up to the parent. This definitely works:

 public class MainHerokuapp extends TestBase {

    Pom_MainHerokuapp mainHerokuappObject;

    @BeforeTest

    public void setup() {

        super.setup();

        mainHerokuappObject = new Pom_MainHerokuapp(driver);

    }

    @Test(priority = 0)

    public void TestMainpagetitle() {

        mainHerokuappObject.VerifyTitles();

    }

    @Test(priority = 1)

    public void TestABTestingText() {

        mainHerokuappObject.VerifyTextOfABTesting();

    }

}

public class TestBase {

    public WebDriver driver;

    // @BeforeTest

    public void setup() {

        driver = new FirefoxDriver();

        driver.get("https://google.com/");

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

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    }

}

Browse Categories

...