NullpointerException while invoking SendKeys through Selenium using PageFactory with Page Object

后端 未结 2 2018
醉梦人生
醉梦人生 2020-12-12 05:02

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 except

相关标签:
2条回答
  • 2020-12-12 05:49

    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.

    0 讨论(0)
  • 2020-12-12 05:54

    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.

    0 讨论(0)
提交回复
热议问题