在带有Java的Selenium WebDriver中运行测试时,获取Null Pointer异常。出于某种原因,该测试会重新调整为null,即使一切都已正确声明(我认为呢?)。
我在这里想念什么/做错什么了吗?
//给出以下代码:
public class HomePage extends DriverSetup{
@FindBy(className = "ico fa fa-lock")
static WebElement loginButton;
public HomePage (WebDriver driver){
super(driver);
}
public static void logInBut(){
loginButton.click();
}```
//When running this test:
```public class test1 extends DriverSetup{
public test1() {
super(driver);
}
@Test
public void signIn(){
getDriver().get(URL);
HomePage.logInBut();
logInPage.inEmail(" ");
logInPage.inPassword(" ");
}```
//Driver Set up:
```public class DriverSetup {
public static WebDriver driver;
public DriverSetup(WebDriver driver) {
}
public WebDriver getDriver() {
if(this.driver == null) {
this.setUp();
}
return this.driver;
}
public void FindByInitialization (WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver, this);
}
//Set up Driver
@BeforeClass //Before executing any test in the class do this
public static void setUp(){
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Peter.Redondeiro\\Documents" +
"\\IDEA projects\\ChromeDriver\\chromedriver_win32\\chromedriver.EXE");
driver = new ChromeDriver();
driver.manage().window().maximize(); //maximise window when open
}
//clear all the cookies after executing each test
@After
public void clearCookies(){
driver.manage().deleteAllCookies(); //delete all the cookies
}
//Close browser after executing all the tests in the class
@AfterClass
public static void closeBrowser(){
driver.close();
}
}```
//Log in page object:
public class logInPage extends DriverSetup{
//Find the input email box in Login page
@FindBy(id = "inputEmail")
private static WebElement inputEmail;
//Find input password box in Login page
@FindBy(id = "inputPassword")
private static WebElement inputPassword;
//Find LogIn button in Login page
@FindBy(id = "login")
private static WebElement logInButton;
public logInPage(WebDriver driver) {
super(driver);
}
//Confirm that you are on the Login page
public static String confirmationHeader(){
return header.getText();
}
//Method to enter email in email box
public static void inEmail(String inEmail){
inputEmail.sendKeys(inEmail + emailGenerator.randomEmailGenerator());
}
//Method to enter password in password box
public static void inPassword(String PassInput){
inputPassword.sendKeys(PassInput + passwordGenerator.randomPasswordGenerator());
}
}```
这是上述代码执行的堆栈跟踪:
java.lang.NullPointerException
at logInPage.inEmail(logInPage.java:46)
at test1.signIn(test1.java:15)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Process finished with exit code -1
答案 0 :(得分:3)
U在行上获取NPE(空指针异常)
loginButton.click();
这意味着您正在对具有空值的对象进行某些操作,在这种情况下,它就是您的loginButton
现在出现了问题,为什么用
初始化登录按钮时为空?@FindBy(className = "ico fa fa-lock")
static WebElement loginButton;
由于网络驱动程序无法找到此元素,因此它为空
尝试遵循建议
public HomePage(WebDriver driver) {
super(driver);
PageFactory.initElements(driver, this);
}
如果这不起作用,请尝试使用这样的单个类名
@FindBy(className = "ico")
private WebElement loginButton;
@FindBy(className = "login")
List<WebElement> buttons;
并在这样单击时
buttons.get(0).click();
否则请尝试使用javascript单击直接操作,它肯定可以正常工作
((JavascriptExecutor)driver).executeScript("document.getElementsByClassName('login')[0].click()");
答案 1 :(得分:1)
请检查HomePage.java中的第55行
它说
loginButton.click();
空指针是因为loginButton
为空,可能是因为它没有初始化。
最可能是因为它无法通过您提到的类来查找元素。
您可能要使用'。'在班级名称前面
答案 2 :(得分:1)
loginButton
是null
,因为您指定的定位符不匹配任何内容。 className
使用一类作为参数,而您使用了三类。要使用多个类,您需要使用cssSelector
@FindBy(css = ".ico.fa.fa-lock")
static WebElement loginButton;
或xpath
@FindBy(xpath = "//*[@class='ico fa fa-lock']")
static WebElement loginButton;
别忘了初始化PageFactory.initElements(driver, HomePage)
答案 3 :(得分:1)
尝试以下代码。
@FindBy(className = "login")
static WebElement loginButton;
或
@FindBy(xpath ="//a[@class='login']")
static WebElement loginButton;
答案 4 :(得分:1)
致谢@Ravi,以下是我的查询的答案:
public class HomePage extends DriverSetup{
@FindBy(className = "login")
private static List<WebElement> buttons;
public HomePage (WebDriver driver){
super(driver);
}
public static void logInBut(){
((JavascriptExecutor)driver).executeScript("document.getElementsByClassName('login')[0].click()");
}
}
//Trying with javascript click because of environment issue driver was not able to find elements with @FindBy, hence the web element was null and it was throwing NullPointerException
//If you need to read through to understand please see the comments and read @Ravi's comments in particular!