WebDriver is not passing between classes using page factory

2,446 views
Skip to first unread message

David Purvis

unread,
Oct 14, 2023, 1:09:03 PM10/14/23
to Selenium Users
I am getting an error when accessing pages from another class.

java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.SearchContext.findElement(org.openqa.selenium.By)" because "this.searchContext" is null

at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:68)

at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)

at jdk.proxy2/jdk.proxy2.$Proxy17.click(Unknown Source)

at Pages.InventoryListPage.getItemNames(InventoryListPage.java:31)

at StepDefinitions.CheckDataBetweenListAndSingleItemSteps.checkList(CheckDataBetweenListAndSingleItemSteps.java:19)

at ✽.check list item view against single item view(file:///C:/Users/purvisd/SwagLabs/SwagLabsFramework/src/test/resources/Features/checkSingleItem.feature:8)


This is the class I am calling from


package StepDefinitions;


import org.openqa.selenium.WebDriver;

import Pages.InventoryListPage;

import io.cucumber.java.en.Given;

import io.cucumber.java.en.Then;




public class CheckDataBetweenListAndSingleItemSteps {

WebDriver driver;

InventoryListPage inventory;




@Then("check list item view against single item view")

public void checkList() {

inventory = new InventoryListPage(driver);

inventory.getItemNames();

}


@Given("user is on the home page")

public void user_is_on_the_home_page() {


}

}


This is the page object class I am calling


package Pages;


import java.util.List;


import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.support.FindAll;

import org.openqa.selenium.support.FindBy;

import org.openqa.selenium.support.PageFactory;


public class InventoryListPage {

@FindAll(@FindBy(className="inventory_item_name"))

public List<WebElement> Item_Names;

@FindAll(@FindBy(className="inventory_item_price"))

public List<WebElement> Item_Price;

@FindBy(id="react-burger-menu-btn")

public WebElement burgerMenu;

public WebDriver driver;

public InventoryListPage(WebDriver driver) {

this.driver=driver;

PageFactory.initElements(driver, this);

}

public void getItemNames() {

System.out.println("I am in the Names constructor");

burgerMenu.click();

}

}


The site I use is a testing site at

https://www.saucedemo.com/


I think this is a passing WebDriver to another class issue because If I call all objects from the same class everything works if I call first from the login class then the list class i get the error message.

Any help or insight into this error will be gratefully received .


With thanks in advance.

Krishnan Mahadevan

unread,
Oct 14, 2023, 1:13:06 PM10/14/23
to seleniu...@googlegroups.com

Where are you instantiating WebDriver and assigning it to the data member in your test class ?

 

 

 

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribblings @ https://rationaleemotions.com/

 

 

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/25addc5b-1479-48ca-bfcd-87fe07cdc31dn%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages