繁体   English   中英

使用PageFactory和Page Object通过Selenium调用SendKeys时发生NullpointerException

[英]NullpointerException while invoking SendKeys through Selenium using PageFactory with Page Object

我有三节课。 一种用于从网页中获取所有元素,一种用于对这些元素执行操作,另一种用于测试脚本。 从测试脚本调用函数时,出现空指针异常。 我发现这是因为我使用@FindBy批注,但是我不知道如何解决此问题。

元素类:

public class LoginPageElements {

    @FindBy(id="loginId")
    private static WebElement userNameTextBox;

    @FindBy(id="password")
    private static WebElement userPasswordTextBox;

    @FindBy(id="QTP_LoginButton")
    private static WebElement loginButton;

    public static WebElement getUserNameTextBox(WebDriver driver){
        WebElement a=driver.findElement(By.id("loginId"));
        return a;
    }

    public static WebElement getUserPasswordTextBox(){
        return userPasswordTextBox;
    }

    public static WebElement getLoginButton(){
        return loginButton;
    }
}

动作类:

public class LoginPageActions {

        public static void login(WebDriver driver,String username,String password){
            WebElement a=LoginPageElements.getUserNameTextBox(driver);
            a.sendKeys(username);
            LoginPageElements.getUserPasswordTextBox().sendKeys(password);
            LoginPageElements.getLoginButton().click();
        }

    }

测试脚本:

public class Sample {
     public static String driverPath = "D:/Selenium/Chrome Driver latest/chromedriver.exe";
     public static WebDriver driver;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", driverPath);

    ChromeOptions options = new ChromeOptions();
    options.addArguments("test-type");
    options.addArguments("start-maximized");
    options.addArguments("--js-flags=--expose-gc");
    options.addArguments("--enable-precise-memory-info");
    options.addArguments("--disable-popup-blocking");
    options.addArguments("--disable-default-apps");
    options.addArguments("--enable-automation");
    options.addArguments("test-type=browser");
    options.addArguments("disable-infobars");
    options.addArguments("--disable-extensions");
    options.setExperimentalOption("useAutomationExtension", false);

    driver = new ChromeDriver(options);

    driver.get("http://10.235.80.106:8080");

    LoginPageActions.login(driver,"acb", "adasd");
}

当我将WebDriver对象从测试脚本传递到元素类时,也不例外。 由于没有WebDriver实例化,当我使用通过FindBy注释初始化的元素时,会发生问题。 我该如何解决? 谢谢

您可以继续使用@FindBy批注,只需要确保对WebElements进行初始化即可。 为此,您应该使用PageFactory初始化LoginPageElements:

LoginPageElements loginPageElements = PageFactory.initElements(webDriver, LoginPageElements.class);

其中webDriver是用于运行硒测试的WebDriver的实例。

您需要声明WebDriver实例,并在LoginPageElementsLoginPageActions类中添加构造函数,如下所示:

  1. LoginPageElements类:

     WebDriver driver; //constructor public LoginPageElements(WebDriver loginDriver) { this.driver=loginDriver; } 
  2. LoginPageActions类:

     WebDriver driver; //constructor public LoginPageActions(WebDriver loginDriver) { this.driver=loginDriver; } 

让我知道这是否回答了您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM