如何在页面对象模型class-Java中使用鼠标悬停动作?

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

我正在使用页面对象模型和 testNG 练习 slenium-java。我试图在 spicejet 网站上做注册场景。我可以轻松转到 url,但要注册,我们必须悬停在 Login/Signup 按钮上,然后我们必须单击注册。 我试图在我的一个页面对象 class 中执行鼠标悬停操作,但这是一个错误。

我已经尝试在主页而不是页面对象 class 中初始化操作 class,但也没有产生任何结果。

页面对象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);
    }


}

主要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();

    }

}

错误信息

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)

这是我收到的错误信息。

有两个问题需要解决,

1 st - 页面对象创建。你必须像这样传递驱动程序对象,

LoginPage lp = new LoginPage(driver);

LoginPage 上的第 2 个构造函数 class。构造函数的名称应与 class 名称相同。

public LoginPage(WebDriver driver) {

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

将操作移至登录方法内

public void login() {

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