问题
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:
LoginPageElements
class:WebDriver driver; //constructor public LoginPageElements(WebDriver loginDriver) { this.driver=loginDriver; }
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