使用SendKeys时Selenium中的NullpointerException [英] NullpointerException in Selenium when using SendKeys

查看:336
本文介绍了使用SendKeys时Selenium中的NullpointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三节课。一个用于从网页获取所有元素,一个用于使用这些元素执行操作,一个用于测试脚本。当我从测试脚本调用一个函数时,我得到一个空指针异常。我想出这是因为我使用@FindBy注释,但我不知道如何解决这个问题。

I have three classes. One for getting all elements from the Webpage, one for performing actions with those elements and one for the test scripts. I get a null pointer exception when I call a function from the test script. I figured out this is because I use @FindBy annotation, but I don't know how to fix this.

元素类:

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对象从测试脚本传递给元素类。当我使用由FindBy注释初始化的元素时,会出现问题,因为没有WebDriver实例化。我该如何解决?谢谢

There is no exception when I pass the WebDriver object from the test script to the element class. The problem occurs when I use the elements initialized with FindBy annotations because of no WebDriver instantiation. How do I fix this? Thanks

推荐答案

您可以继续使用您需要的@FindBy注释来确保初始化WebElements。为此,您应该使用PageFactory初始化您的LoginPageElements:

You can continue to use the @FindBy annotations you just need to make sure you initialise the WebElements. To do this you should initialise your LoginPageElements using PageFactory:

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

其中webDriver是您用来运行selenium测试的WebDriver的一个实例。

where webDriver is an instance of the WebDriver you are using to run the selenium tests.

这篇关于使用SendKeys时Selenium中的NullpointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆