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)