问题描述
使用 Java 在 Selenium WebDriver 中运行测试时出现空指针异常.出于某种原因,测试返回 null,甚至所有内容都被正确声明(我认为?).
Getting Null Pointer exception when running test in Selenium WebDriver with Java. For some reason the test is retunrning null, even everything is being declared correctly (I think?).
我在这里遗漏了什么/做错了什么?
What am I missing/doing wrong here?
//给定这段代码:
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());
}
}```
这是上面代码执行的堆栈跟踪:
This is the stack trace of the above code execution:
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
推荐答案
U are getting NPE(Null pointer exception) on line
U are getting NPE(Null pointer exception) on line
loginButton.click();
这意味着您正在对获得空值的对象进行一些操作,在这种情况下,它就是您的 loginButton
it means you are doing some operation on object which got a null value and in this case it is none other than your loginButton
现在问题来了,为什么登录按钮在初始化时为空
now the question comes up, Why login button is null when you are initialising it with
@FindBy(className = "ico fa fa-lock")
static WebElement loginButton;
这是可能的,因为网络驱动程序无法找到这个元素,因此它为空
it is possible because of web driver is not able to find this element and hence it is null
尝试以下建议
public HomePage(WebDriver driver) {
super(driver);
PageFactory.initElements(driver, this);
}
如果这不起作用,请尝试使用这样的单个类名
and if this does not work try with single class name like this
@FindBy(className = "ico")
private WebElement loginButton;
@FindBy(className = "login")
List<WebElement> buttons;
然后像这样点击
buttons.get(0).click();
否则尝试使用javascript直接点击,它肯定可以工作
else try with javascript click direct, it should surely work
((JavascriptExecutor)driver).executeScript("document.getElementsByClassName('login')[0].click()");
这篇关于返回空指针异常 - Java Selenium Webdriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!