Reputation: 27
I cant pass the driver/object to next class/page and have same NullPointerException in first/beginning class.
PageObject class - SearchResultsPage:
public class SearchResultsPage extends BasePage{
@FindBy(xpath = "//*[@data-original-title=\"Compare this Product\"]")
List <WebElement> compareButton;
@FindBy(partialLinkText = "Product Compare")
WebElement urlComparePage;
public SearchResultsPage(WebDriver driver) {
super(driver);
PageFactory.initElements(driver, this);
}
public void compareItems(){
for(WebElement compareButtons: compareButton){
compareButtons.click();
}
}
public void goToComparePage(){
urlComparePage.click();
}
}
PageObject class HomePage:
public class HomePage extends BasePage{
public HomePage(WebDriver driver) {
super(driver);
PageFactory.initElements(driver, this);
}
public String PAGE_TITLE = "Your Store";
WebDriver driver;
@FindBy(className = "input-lg")
WebElement inputSearch;
@FindBy(className = "btn-lg")
WebElement searchButton;
public void isHomePage(){
String pageTitle = driver.getTitle();
Assert.assertEquals(pageTitle, PAGE_TITLE);
}
public void inputIntoSearch(){
String itemName = "ipod";
inputSearch.sendKeys(itemName);
}
public SearchResultsPage clickSearchButton(){
searchButton.click();
return PageFactory.initElements(driver, SearchResultsPage.class);
}
}
Test class:
public class MainPage {
HomePage hp;
TopNavigation topNav;
ComparePage cp;
SearchResultsPage srp;
@BeforeTest
public void setUp(){
WebDriver driver = new FirefoxDriver();
driver.get("http://demo.opencart.com/");
driver.manage().window().maximize();
hp = PageFactory.initElements(driver, HomePage.class);
topNav = PageFactory.initElements(driver, TopNavigation.class);
cp = PageFactory.initElements(driver, ComparePage.class);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test(priority = 0)
public void checkIsHomePage(){
hp.isHomePage();
}
@Test
public void changeCurrency(){
topNav.clickButtonChangeCurrency();
topNav.setCurrency();
}
@Test
public void searchProducts(){
hp.inputIntoSearch();
hp.clickSearchButton();
}
@Test
public void addToCompare(){
srp.compareItems();
}
}
And I have 2 problem:
1.When I run test checkIsHomePage() - FAILS (NullPointerException) and changeCurrency() PASS. I dont Know why first test is FAIL if thist 2 methods are in the same PageObiect class - HomePage. What is wrong?
2.When searchProduct method Pass I want to compare product using addToCompare(), but I have no idea how use PageFactory.initelements to make test on - page with search results. How should I do this?
---------------------------------------UPDATED---------------------------- Ok, i foud the reason why it doesnt work. Its becouse of base class (BasePage). I made it and extended all classes with PageObjest, and with constructor and super(driver). When I deleted "extends" and "super" in PageObject classes and used (this.driver = driver) it is working now. But what i made with this base class that it didint work??
---------------------------------------------UPDATED------------------------------- My BasePage is poor now:
public class BasePage {
WebDriver driver;
public BasePage(WebDriver driver){
this.driver=driver;
}
}
Upvotes: 0
Views: 2873
Reputation: 5113
The fundamental problem is that HomePage
is clearly intended to defer to BasePage
(though you also call it MainPage
in the snippet): its constructor passes the WebDriver
instance to super(), and yet it has its own WebDriver driver
member variable that never gets set, and that will be null when isHomePage()
is called. You should instead have used the WebDriver
instance from the parent class and dropped the 'masking' declaration from the child class.
Upvotes: 1