I have two function calls as shown below in the @Test
in the test class, one simply clicks on a webElement and other one performs an action to activate the dropdown (By simply hovering over using action.movetoelement
) and then clicking on one of the options in that drop down.
My Program uses a POM with Page elements and the functions to be performed on those webelements in a separate class.
Package testcases;
import pageObjects.VisitHomePage;
---some code--
@Test
public void test1() {
login.eCommerceDemoSite();.// this throws a null pointer exception
login.clickEnrollyourself();// this works as expected
}
-- some code
package pageObjects;
public class VisitHomePage extends Base {
By demoSitesLocator = By.xpath("/html/body/div[1]/div[1]/header/nav/ul/li[8]/a/span[1]/span/span"); //Locate Demo Site button/text on the toolsqa website
By eCommSiteLocator = By.xpath("/html/body/div[1]/div[1]/header/nav/ul/li[8]/ul/li[1]/a/span[2]"); //Locate E-Commerce website link in the demo sites drop down
By enrollYourSelf = By.xpath("/html/body/div[1]/div[4]/div/div/ul/li/div[6]/div/div/div/a");
public VisitHomePage(WebDriver driver) {
super(driver);
visit("some website");
}
public void clickEnrollyourself() {
click(enrollYourSelf);
}
public void eCommerceDemoSite() {
perform(demoSitesLocator);// this throws a null pointer exception
click(eCommSiteLocator);
}
package pageObjects
public class Base{
private Actions action;
private WebDriver driver;
public Base(WebDriver driver) {
this.driver =driver;
}
public void perform(By locator) {
WebElement element = find(locator);
action.moveToElement(element).perform();//this throws a null pointer exception
}
public void click(By locator) {
find(locator).click();// this works a expected
}
}
I have tried and failed to resolve this issue, Kindly advise
1 Answer 1
In your Base
class in the method perform
you're trying to call action.moveToElement(element)
but you haven't instantiate action by the moment (so the reference is pointing to null
). That is why you get NullPointerException
.
Before you call action.moveTo..
you should instantiate your action like:
Actions action = new Actions(driver);