I have tried to find the answer to this but none of those helped me. I'm sure I'm missing one detail, but I can't figure out what exactly.
I get NullPointerException when run a test case. I think it happens when I try to pass driver to my page object (WikiMain), which is inherited from WebPage (superclass), because when I debugged, I noticed driver = null. Should I pass the driver to WebPage, because other pages are inherited from it?
P.S. Sorry I'm kind of new to this and it's my first questions on this website.
Here is my Test Case class:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
public class TestCase{
public WebDriver driver;
@BeforeTest
public void setUp(){
driver = new ChromeDriver();
}
@AfterTest
public void tearDown(){
driver.quit();
}
public WebPage getWikiMain(){
driver.get("https://www.wikipedia.org/");
return new WikiMain(driver);
}
}
Here is my WebPage class:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
public class WebPage{
private WebDriver driver;
public WebPage(WebDriver driver){
this.driver = driver;
initWebElements();
}
public void initWebElements(){
PageFactory.initElements(driver, this);
}
}
Here is WikiMain class:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class WikiMain extends WebPage{
private WebDriver driver;
public WikiMain(WebDriver driver){
super(driver);
}
public WebPage getWikiEn(){
String linkToWikiEn = driver.findElement(By.xpath(".//*[@class= 'central-featured-lang lang1']/a")).getAttribute("href");
driver.get(linkToWikiEn);
return new WikiMainEn(driver);
}
}
And WikiMainEn class:
package com.company.pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class WikiMainEn extends WebPage{
private WebDriver driver;
public WikiMainEn(WebDriver driver){
super(driver);
}
@FindBy(xpath = ".//*[@id='mp-topbanner']/tbody/tr/td[1]/table/tbody/tr/td/div[1]")
private WebElement title;
public WebElement getTitle(){
return title;
}
public String getTitleText(){
return getTitle().getText();
}
}
And actual Test:
import com.company.pages.*;
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestWikiMain extends TestCase{
@Test
public void verifyEnWiki() {
WikiMain wikiMain = (WikiMain) getWikiMain();
WikiMainEn wikiMainEn = (WikiMainEn) wikiMain.getWikiEn();
Assert.assertTrue(wikiMainEn.getTitleText().contains("Welcome to"));
}
testCase()class, should the annotation be@BeforeClassinstead of@BeforeTest(as per the TestNG homepage example SimpleTest.java)?