Search code examples
javaselenium-webdriverpageobjectspage-factory

How to use mouse hover action in page object model class-Java?


I am practicing slenium-java with page object model and testNG. I tried to do signup scenario in spicejet website. I can easily go to url, but to signup, we've to hove over Login/Signup button then we have to click signup. I tried to do mouse hover action in one of my page object class, but it's an giving error.

I already tried to initialize action class in main page instead of page object class, but that also not yielded any result.

Page Object Class

package ObjectRepository;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

public class LoginPage {

    WebDriver driver;

    public LoginPage(WebDriver driver) {

        this.driver=driver;
        PageFactory.initElements(driver, this);
    }

    Actions action = new Actions(driver);

    By xpath = By.id("ctl00_HyperLinkLogin");
    By xpath1 = By.xpath("//*[contains(text(), 'Sign up')]");
    By Password = By.id("pass");
    By Login = By.xpath("//*[@type='submit']");

    public void login() {
         action.moveToElement(driver.findElement(xpath)).moveToElement(driver.findElement(xpath1)).click().build().perform();
    }

    public WebElement signUp() {
        return driver.findElement(xpath1);
    }

    public WebElement pwd() {
        return driver.findElement(Password);
    }

    public DashBoard submit() {
         driver.findElement(Login).click();
        return new DashBoard(driver);
    }


}

Main Class

package TestCases;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;

import ObjectRepository.DashBoard;
import ObjectRepository.Data;
import ObjectRepository.LoginPage;

public class MainClass {

    @Test
    public void A() {

        System.setProperty("webdriver.chrome.driver", "D:\\drivers_selenium\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();

        driver.get("https://www.spicejet.com/");

        LoginPage lp = new LoginPage(driver);
        lp.login();
        lp.signUp().click();

    }

}

Error Message

java.lang.NullPointerException
    at java.util.Objects.requireNonNull(Unknown Source)
    at org.openqa.selenium.interactions.Actions.<init>(Actions.java:65)
    at ObjectRepository.LoginPage.<init>(LoginPage.java:17)
    at TestCases.MainClass.A(MainClass.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1137)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:753)
    at org.testng.TestRunner.run(TestRunner.java:607)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:368)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:363)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:321)
    at org.testng.SuiteRunner.run(SuiteRunner.java:270)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1284)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1209)
    at org.testng.TestNG.runSuites(TestNG.java:1124)
    at org.testng.TestNG.run(TestNG.java:1096)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

This is the error message i got.


Solution

  • There are two issues to fix,

    1 st - page object creation. you have to pass the driver object like this,

    LoginPage lp = new LoginPage(driver);
    

    2nd constructor on the LoginPage class. The constructor should have the same name as class name.

    public LoginPage(WebDriver driver) {
    
            this.driver=driver;
            PageFactory.initElements(this.driver, this);
        }
    

    Move Actions to within Login Method

    public void login() {
    
    Actions action = new Actions(driver);
        action.moveToElement(driver.findElement(xpath)).moveToElement(driver.findElement(xpath1)).click().build().perform();
        }