使用带页面对象的 PageFactory 通过 Selenium 调用 SendKeys 时出现 NullpointerException
NullpointerException while invoking SendKeys through Selenium using PageFactory with Page Object
我有三个 class。一种用于从网页获取所有元素,一种用于对这些元素执行操作,另一种用于测试脚本。当我从测试脚本调用一个函数时,我得到一个空指针异常。我发现这是因为我使用了@FindBy 注释,但我不知道如何解决这个问题。
元素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;
}
}
操作 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();
}
}
测试脚本:
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 对象从测试脚本传递给元素 class 时没有异常。由于没有 WebDriver 实例化,当我使用使用 FindBy 注释初始化的元素时会出现问题。我该如何解决?谢谢
您需要声明 WebDriver
实例并在 LoginPageElements
& LoginPageActions
class 中添加构造函数为:
LoginPageElements
class:
WebDriver driver;
//constructor
public LoginPageElements(WebDriver loginDriver)
{
this.driver=loginDriver;
}
LoginPageActions
class:
WebDriver driver;
//constructor
public LoginPageActions(WebDriver loginDriver)
{
this.driver=loginDriver;
}
如果这回答了您的问题,请告诉我。
您可以继续使用 @FindBy 注释,您只需要确保初始化 WebElements。为此,您应该使用 PageFactory:
初始化您的 LoginPageElements
LoginPageElements loginPageElements = PageFactory.initElements(webDriver, LoginPageElements.class);
其中 webDriver 是您用于 运行 selenium 测试的 WebDriver 实例。
我有三个 class。一种用于从网页获取所有元素,一种用于对这些元素执行操作,另一种用于测试脚本。当我从测试脚本调用一个函数时,我得到一个空指针异常。我发现这是因为我使用了@FindBy 注释,但我不知道如何解决这个问题。
元素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;
}
}
操作 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();
}
}
测试脚本:
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 对象从测试脚本传递给元素 class 时没有异常。由于没有 WebDriver 实例化,当我使用使用 FindBy 注释初始化的元素时会出现问题。我该如何解决?谢谢
您需要声明 WebDriver
实例并在 LoginPageElements
& LoginPageActions
class 中添加构造函数为:
LoginPageElements
class:WebDriver driver; //constructor public LoginPageElements(WebDriver loginDriver) { this.driver=loginDriver; }
LoginPageActions
class:WebDriver driver; //constructor public LoginPageActions(WebDriver loginDriver) { this.driver=loginDriver; }
如果这回答了您的问题,请告诉我。
您可以继续使用 @FindBy 注释,您只需要确保初始化 WebElements。为此,您应该使用 PageFactory:
初始化您的 LoginPageElementsLoginPageElements loginPageElements = PageFactory.initElements(webDriver, LoginPageElements.class);
其中 webDriver 是您用于 运行 selenium 测试的 WebDriver 实例。