1

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

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

页面对象类

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);
    }


}

主班

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)

这是我收到的错误消息。

4

1 回答 1

0

有两个问题要解决,

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

LoginPage lp = new LoginPage(driver);

LoginPage 类的第二个构造函数。构造函数的名称应与类名相同。

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();
    }
于 2019-07-25T13:56:47.870 回答