NullpointerException while invoking SendKeys through Selenium using PageFactory with Page Object

大憨熊 提交于 2019-11-27 08:41:27

问题


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.

Elements Class:

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

Actions Class:

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

    }

Test script:

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

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


回答1:


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

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




回答2:


You need to declare the WebDriver instance and add the constructor in LoginPageElements & LoginPageActions class as:

  1. LoginPageElements class:

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

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

Let me know if this Answers your Question.



来源:https://stackoverflow.com/questions/44968344/nullpointerexception-while-invoking-sendkeys-through-selenium-using-pagefactory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!