[英]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
实例,并在LoginPageElements
和LoginPageActions
类中添加构造函数,如下所示:
LoginPageElements
class: LoginPageElements
类:
WebDriver driver; //constructor public LoginPageElements(WebDriver loginDriver) { this.driver=loginDriver; }
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.