简体   繁体   English

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

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

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. 我发现这是因为我使用@FindBy批注,但是我不知道如何解决此问题。

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. 当我将WebDriver对象从测试脚本传递到元素类时,也不例外。 The problem occurs when I use the elements initialized with FindBy annotations because of no WebDriver instantiation. 由于没有WebDriver实例化,当我使用通过FindBy注释初始化的元素时,会发生问题。 How do I fix this? 我该如何解决? Thanks 谢谢

You can continue to use the @FindBy annotations you just need to make sure you initialise the WebElements. 您可以继续使用@FindBy批注,只需要确保对WebElements进行初始化即可。 To do this you should initialise your LoginPageElements using PageFactory: 为此,您应该使用PageFactory初始化LoginPageElements:

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

where webDriver is an instance of the WebDriver you are using to run the selenium tests. 其中webDriver是用于运行硒测试的WebDriver的实例。

You need to declare the WebDriver instance and add the constructor in LoginPageElements & LoginPageActions class as: 您需要声明WebDriver实例,并在LoginPageElementsLoginPageActions类中添加构造函数,如下所示:

  1. LoginPageElements class: LoginPageElements类:

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

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

Let me know if this Answers your Question. 让我知道这是否回答了您的问题。

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

相关问题 NelePointerException在硒中使用页面对象模型 - NullPointerException using Page Object Model in Selenium 如何使用 Selenium 和 Java 通过 PageFactory 等待元素不可见 - How to wait for invisibility of an element through PageFactory using Selenium and Java Selenium java.lang.NullPointerException与PageFactory - Selenium java.lang.NullPointerException with PageFactory 在通过 Java 使用 Selenium 在 VS Code 中工作时,方法 sendKeys(String) 对于类型 ByJava(67108964) 和 sendKeys 方法未定义 - The method sendKeys(String) is undefined for the type ByJava(67108964) with sendKeys method while working in VS Code using Selenium through Java 在 PageFactory (Selenium+Java) 中获取 NPE NullPointerException - Getting NPE NullPointerException in PageFactory (Selenium+Java) 使用selenium pagefactory时接收空指针异常 - Recieving null pointer exception while using selenium pagefactory 在 selenium 的 PageFactory 上选择对象 - Select object on selenium's PageFactory 使用PageFactory的Page类 - Page Class using PageFactory 使用Mockito调用模拟方法时发生NullPointerException - NullPointerException while invoking mocked method using Mockito 在没有PageFactory的Selenium中使用@FindBy - Using @FindBy in Selenium without PageFactory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM