我是自动化测试的初学者。目前,我尝试使用Actions类编写测试。 ChromeDriver 2.42版本 硒3.14版 Chrome 9.0.3497.100
下面我放我的代码:
public class KlasaDoCwiczen {
WebDriver driver;
Actions action;
ChromeOptions options;
@Before
public void UruchamianieAplikacji () {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\smartyn\\Desktop\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
Actions action = new Actions(driver);
driver.get("http://localhost:4200/dashboard");
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(3000, TimeUnit.MILLISECONDS);
//driver.quit();
}
@Test
public void TestCwiczeniowyZKlasaActions () {
action.clickAndHold(driver.findElement(By.id("client-individualPerson-bigFamilyCard-details-bttn")))
.moveToElement(driver.findElement(By.id("client-individualPerson-bigFamilyCard-details-bttn")))
.build()
.perform();
}
}
所以当我执行此命令时,我有一条语句:
wrz 25,2018 10:42:16 org.openqa.selenium.remote.ProtocolHandshake createSession INFO:检测到的方言:OSS
java.lang.NullPointerException
at KlasaDoCwiczen.TestCwiczeniowyZKlasaActions(KlasaDoCwiczen.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
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)
答案 0 :(得分:0)
此错误消息...
java.lang.NullPointerException
...表示 NullPointerException 在(KlasaDoCwiczen.java:31)处引发。
您的主要问题是,您已在UruchamianieAplikacji()
方法中全局且一次地初始化了对象 action 。在UruchamianieAplikacji()
方法中声明的 action 对象仅在该方法中具有范围/可见性。
向前移动,您正在尝试通过TestCwiczeniowyZKlasaActions()
方法访问对象动作,其中对象 action 不可见。
因此您会看到 java.lang.NullPointerException 。
将 action 的声明保留为全局变量,就像 driver , options 等一样,并从中删除其他声明UruchamianieAplikacji()
方法:
Actions action = new Actions(driver); //remove this line
或在Actions action
方法中初始化TestCwiczeniowyZKlasaActions()
,仅将其从 global 范围和TestCwiczeniowyZKlasaActions()
方法中删除,如下所示:
new Actions(driver).clickAndHold(driver.findElement(By.id("client-individualPerson-bigFamilyCard-details-bttn")))
.moveToElement(driver.findElement(By.id("client-individualPerson-bigFamilyCard-details-bttn")))
.build()
.perform();