使用webDriver.getWindowHandle的String变量的NullPointerException错误

时间:2016-05-04 23:41:17

标签: java selenium junit nullpointerexception

当我尝试获取当前的webdriver Window Handle并将其传递给String变量并且我不明白为什么时,我在JUnit测试中得到NullPointerException?

@Test

public void citationLinkerTest() {

//BeforeClass creates and instance of the browser and goes to the target URL.


//ERROR AT THIS LINE: 

String firstWindowHandle = webDriver.getWindowHandle();


assertEquals("Expecting only one window", 1, webDriver.getWindowHandles().size());

}

************错误:

java.lang.NullPointerException
    at com.sersol.alejp.tests.EJournalPortalNewUIIT.citationLinkerTest(EJournalPortalNewUIIT.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    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.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    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:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

2 个答案:

答案 0 :(得分:0)

很抱歉,但由于我的声誉不高,我无法回答这个问题,而是作为一个新的回复。

正如@Kiril S在评论中提到的,问题可能是您定义WebDriver的方式和位置。你没有给出该部分的代码,所以假设存在问题。如果您使用多个注释,则必须在类级别提及驱动程序,以便可以跨类和所有方法访问它。

答案 1 :(得分:0)

您的测试似乎没有初始化webDriver所以getWindowHandle()返回null导致此问题