我有一个扩展SystenTestCase 的测试类
package org.corallosmart.e2e.GestioneContributo;
import org.corallosmart.e2e.SystemTestCase;
import org.junit.Assert;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class TestEffettuaVersamento extends SystemTestCase {
private static WebDriver driver;
@Before
public void setup(){
driver = SystemTestCase.driver;
}
@Test
public void testEffettuaVersamento(){
driver.get("http://localhost:8080/CoralloSmart/login");
WebElement userField = driver.findElement(By.name("username"));
userField.sendKeys("graziano.giu@gmail.com");
WebElement passwordField = driver.findElement(By.name("password"));
passwordField.sendKeys("root");
WebElement form = driver.findElement(By.name("loginForm"));
form.submit();
driver.get("http://localhost:8080/CoralloSmart/index");
WebElement sostieniciBtn=driver.findElement(By.name("sostieniciBtn"));
sostieniciBtn.click();
WebElement importoField = driver.findElement(By.name("importo"));
importoField.sendKeys( "20");
WebElement formSostienici= driver.findElement(By.name("sostieniciform"));
formSostienici.submit();
driver.get("http://localhost:8080/CoralloSmart/checkout");
WebElement nameCard=driver.findElement(By.name("name-on-card"));
nameCard.sendKeys("Graziano Giuseffi");
WebElement cardNumber= driver.findElement(By.name("card-number"));
cardNumber.sendKeys("5333171083897454");
WebElement expMonth= driver.findElement(By.name("expMonth"));
expMonth.sendKeys("July (07)");
WebElement expYear= driver.findElement(By.name("expYear"));
expYear.sendKeys("2024");
WebElement cvv= driver.findElement(By.name("card-cvc"));
cvv.sendKeys("584");
WebElement paymentForm= driver.findElement(By.id("payment-Form"));
paymentForm.submit();
Boolean isOk= driver.getPageSource().contains("Operazione Completata Con Successo");
Assert.assertTrue(isOk);
}
}
import io.github.bonigarcia.wdm.config.DriverManagerType;
import io.github.bonigarcia.wdm.managers.ChromeDriverManager;
import org.junit.BeforeClass;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.io.FileNotFoundException;
import java.sql.SQLException;
public class SystemTestCase {
protected static WebDriver driver;
/**
* Setup della classe di testing
*
* @throws FileNotFoundException
* @throws SQLException
*/
@BeforeClass
public static void setupClass() throws FileNotFoundException, SQLException {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless"); // per non lanciare gui
ChromeDriverManager .getInstance(DriverManagerType.CHROME).setup();
driver = new ChromeDriver(chromeOptions);
}
/* @AfterAll
public static void tearDown() {
driver.close();
}
*/
}
这些都是错误。解决方案
"C: \Program Files\Java\jdk-11.0.16.1\bin\Java.exe"-ea-Didea测试循环缓冲区大小=1048576"-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 202.2.2\lib\IDEA_rt.jar=62181:C:\Program Files\JetBrains\IIntelliJ IDEA 202.2.2\bin"-Dfile.concoding=UTF-8-类路径";C: \Users\grazi.m2\repository\org\junit\platform\junit platform launcher\1.7.0\junit platform-lancher-1.7.0.jar;C: \Users\grazi.m2\repository\org\junit\vintage \junit vintage engine\5.7.0\junit vintageengine-5.7.0.jar;C: \Program Files\JetBrains\IntelliJ IDEA 202.2.2\lib\IDEA_rt.jar;C: \Program Files\JetBrains\IntelliJ IDEA 2022.2.2 \plugins\junit\lib\junit5-rt.jar;C: \Program Files\JetBrains\IIntelliJ IDEA 2022.2.2 \plugins\junit\lib\junit-rt.jar;C: \Users\grazi\Desktop\CS\ColloSmart\target\test-classes;C: \Users\grazi\Desktop\CS\ColloSmart\target\classes;C: \Users\grazi\AppData\Roaming\JetBrains\IntelliJIdea2022.2\javascript\extLibs__maxcdn.bootstrapcdn.com_bootstrap3.3.0_css_bootstrap.css;C: \Users\grazi\AppData\Roaming\JetBrains\IntelliJIdea2022.2\javascript\extLibs__maxcdn.bootstrapcdn.com_bootstrap3.3.0_js_bootstrap.js;C: \Users\grazi\AppData\Roaming\JetBrains\IntelliJIdea2022.2\javascript\extLibs_code.jquery.com_jquery-1.11.1.js;C: \Users\grazi.m2\repository.com\google\code\gson\gson\2.8.9\gson-2.8.9.jar;C: \Users\grazi.m2\repository\com\googlecode\json-simple\json-suple\1.1.1\json-somple-1.1.1.jar;C: \Users\grazi.m2\repository\commons-io\commons-sio\2.8.0\commons-io-2.8.0.jar;C: \Users\grazi.m2\repository\javax\javaee-web-api\8.0.1\javaee-web-api-8.0.1.jar;C: \Users\grazi.m2\repository\jstl\jstl=1.2\jstl-1.2.jar;C: \Users\grazi.m2\repository\commons dbutils\commons dbutils\1.7\commons-dbutils-1.7.jar;C: \Users\grazi.m2\repository\commons codec\commons codec\1.15\commons-codec-1.15.jar;C: \Users\grazi.m2\repository\mysql\mysql连接器java\8.0.22\mysql-connector-java-8.0.22.jar;C: \Users\grazi.m2\repository\com\google\protobuf\protobf java\3.11.4\protobf-java-3.11.4.jar;C: \Users\grazi.m2\repository\org\junit\jupiter\junit jupiter engine\5.7.0\junit jupiter engine-5.7.0.jar;C: \Users\grazi.m2\repository\org.apiguardian\apiguardianapi\1.1.0\apiguardian-api-1.1.0.jar;C: \Users\grazi.m2\repository\org\junit\platform\junit平台引擎\1.7.0\junit-platformengine-1.7.0.jar;C: \Users\grazi.m2\repository\org\junit\jupiter\junit jupiter api\5.7.0\junit-jjupiter-api-5.7.0.jar;C: \Users\grazi.m2\repository\org.opentest4j\opentest4j \1.2.0\opentest4j-1.2.0.jar;C: \Users\grazi.m2\repository\org\junit\platform\junit platform commons\1.7.0\junit-platform-commons-17.7.0.jar;C: \Users\grazi.m2\repository\junit\junit\A.13 \junit-4.13.jar;C: \Users\grazi.m2\repository\org.hamcrest\hamcrest core\1.3\hamcrest-core-1.3.jar;C: \Users\grazi.m2\repository\org\webjars\startbootstrap-sb-admin-2\4.0.6\startbootstrap-sb-aadmin-2-4.0.6.jar;C: \Users\grazi.m2\repository\org\webjars\font真棒\5.7.2\font-awesome-5.7.2.jar;C: \Users\grazi.m2\repository\org\webjars\bootstrap\4.3.1 \bootstrap-4.3.1.jar;C: \Users\grazi.m2\repository\org\webjars\popper.js\1.14.3\popper.js-114.3.jar;C: \Users\grazi.m2\repository\org\webjars\chartjs\2.7.3\chartjs-2.7.3.jar;C: \Users\grazi.m2\repository\org\webjars\datatables\1.10.19\datatables-1.10.19.jar;C: \Users\grazi.m2\repository\org\webjars\jquery-leasing\1.4.1\jquery-easing-1.4.1.jar;C: \Users\grazi.m2\repository\org\mockito\mockito core\3.6.28\mockito-core-3.6.28.jar;C: \Users\grazi.m2\repository\net\bytebuddy\byte buddy\1.10.18\byte-bddy-1.10.18.jar;C: \Users\grazi.m2\repository\net\bytebuddy\byte buddy-agent\1.10.18\byte-bddy-agent-1.10.18.jar;C: \Users\grazi.m2\repository\org\obgenesis\obgenesis \3.1\obgenecis-3.1.jar;C: \Users\grazi.m2\repository\org.seleniumhq\硒\硒铬驱动程序\3.141.59\硒-铬驱动程序-3.141.59.jar;C: \Users\grazi.m2\repository\org\seleniumhq\seleniumApi\3.141.59\seleniu-api-3.141.59.jar;C: \Users\grazi.m2\repository\org\seleniumhq\selenium\selenium远程驱动程序\3.141.59\selenium-remote-driver-3.141.59.jar;C: \Users\grazi.m2\repository\org\apache\commons\commons-exec\1.3\commons-exec-1.3.jar;C: \Users\grazi.m2\repository.com\谷歌\番石榴\番石榴\25.0-jre\番石榴-25.0-jre.jar;C: \Users\grazi.m2\repository.com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar;C: \Users\grazi.m2\repository\org\checkerframework\checker-compat-qual\2.0.0\checker-compat-quali-2.0.0.jar;C: \Users\grazi.m2\repository\com\google\errorproden\error_prone_annotations\2.1.3\error_prone _annotations-2.1.3.jar;C: \Users\grazi.m2\repository\com\google\j2objc\j2objk annotations\1.1\j2obj-annotations-1.1.jar;C: \Users\grazi.m2\repository\org\codehaus\mojo\animal-sniffer-annotations\1.14\animal-snover-anotations-1.14.jar;C: \Users\grazi.m2\repository\com\squareup\okhttp3\okhttp\3.11.0\okhttp-3.11.0.jar;C: \Users\grazi.m2\repository\com\squareeup\okio\okio\ 1.14.0\okio-1.140.jar;C: \Users\grazi.m2\repository\io\github\bonigarcia\webdrivermanager\4.2.2.jar;C: \Users\grazi.m2\repository\org\slf4j\slf4j api\1.7。
java.lang.NullPointerException网址:org.corallosmart.e2e.GestionControlto.TestEffettuaVersamento.testEffettua Versamento(TestEffettuaVersamento.java:27)位于java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)位于java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)位于java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于java.base/java.lang.reflect.Method.ioke(Method.java:566)网址:org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)网址:org.junit.jupiter.engine.execulation.MethodInvocation.prough(MethodInvocation.java:60)位于org.junit.jupiter.engine.exexecution.InvocationInterceptorChain$ValidatingInvocation.procent(InvocationIntersectorChain.java:131)网址:org.junit.jupiter.engine.exextension.TimeoutExtension.incept(TimeoutExtension.java:149)网址:org.junit.jupiter.engine.exextension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)网址:org.junit.jupiter.engine.exextension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)位于org.junit.jupiter.engine.exexecution.ExecutableInvoker$ReflectiveInterceptorCallLambda$ofVoidMethod$0(ExecutableInvoker.java:115)位于org.junit.jupiter.engine.execulation.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)在org.junit.jupiter.engine.exexecution.InvocationInterceptorChain$InterceptedInvocation.prough(InvocationIntersectorChain.java:106)位于org.junit.jupiter.engine.exexecution.InvocationInterceptorChain.proceed(InvocationIntersectorChain.java:64)位于org.junit.jupiter.engine.execulation.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)位于org.junit.jupiter.engine.execulation.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)网址:org.junit.jjupiter.engine.execulation.ExecutableInvoker.invoke(ExecutableInvoker.java:104)网址:org.junit.jupiter.engine.execulation.ExecutableInvoker.invoke(ExecutableInvoker.java:98)位于org.junit.jupiter.engine.descriptor.TestMethodTestDescriptorLambda$invokeTestMethod$6(TestMethodTestDescriptor.java:210)网址:org.junit.platform.engine.support.hhierarchy.ThrowableCollectioner.execute(ThrowableCollector.java:73)位于org.junit.jjupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:206)位于org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.exexecute(TestMethodTestDescriptor.java:131)位于org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.exexecute(TestMethodTestDescriptor.java:65)位于org.junit.platform.engine.support.histrared.NodeTestTaskLambda$executeSecurivey$5(NodeTestTask.java:139)网址:org.junit.platform.engine.support.hhierarchy.ThrowableCollectioner.execute(ThrowableCollector.java:73)位于org.junit.platform.engine.support.histrared.NodeTestTaskLambda$executeSecurive$7(NodeTestTask.java:129)网址:org.junit.platform.engine.support.histrared.Node.around(Node.java:137)位于org.junit.platform.engine.support.histrared.NodeTestTaskLambda$executeSecurive$8(NodeTestTask.java:127)网址:org.junit.platform.engine.support.hhierarchy.ThrowableCollectioner.execute(ThrowableCollector.java:73)位于org.junit.platform.engine.support.histrared.NodeTestTask.executeSecurively(NodeTestTask.java:126)网址:org.junit.platform.engine.support.histrared.NodeTestTask.exexecute(NodeTestTask.java:84)位于java.base/java.util.ArrayList.forEach(ArrayList.java:1541)位于org.junit.platform.engine.support.hhierarchical.SameThreadHierarchicalTestExecutiorService.invokeAll(SameThreadHierarchialTestExecutiorServices.java:38)位于org.junit.platform.engine.support.histrared.NodeTestTaskLambda$executeSecurivey$5(NodeTestTask.java:143)网址:org.junit.platform.engine.support.hhierarchy.ThrowableCollectioner.execute(ThrowableCollector.java:73)位于org.junit.platform.engine.support.histrared.NodeTestTaskLambda$executeSecurive$7(NodeTestTask.java:129)网址:org.junit.platform.engine.support.histrared.Node.around(Node.java:137)位于org.junit.platform.engine.support.histrared.NodeTestTaskLambda$executeSecurive$8(NodeTestTask.java:127)网址:org.junit.platform.engine.support.hhierarchy.ThrowableCollectioner.execute(ThrowableCollector.java:73)位于org.junit.platform.engine.support.histrared.NodeTestTask.executeSecurively(NodeTestTask.java:126)网址:org.junit.platform.engine.support.histrared.NodeTestTask.exexecute(NodeTestTask.java:84)位于java.base/java.util.ArrayList.forEach(ArrayList.java:1541)位于org.junit.platform.engine.support.hhierarchical.SameThreadHierarchicalTestExecutiorService.invokeAll(SameThreadHierarchialTestExecutiorServices.java:38)位于org.junit.platform.engine.support.histrared.NodeTestTaskLambda$executeSecurivey$5(NodeTestTask.java:143)网址:org.junit.platform.engine.support.hhierarchy.ThrowableCollectioner.execute(ThrowableCollector.java:73)位于org.junit.platform.engine.support.histrared.NodeTestTaskLambda$executeSecurive$7(NodeTestTask.java:129)网址:org.junit.platform.engine.support.histrared.Node.around(Node.java:137)位于org.junit.platform.engine.support.histrared.NodeTestTaskLambda$executeSecurive$8(NodeTestTask.java:127)网址:org.junit.platform.engine.support.hhierarchy.ThrowableCollectioner.execute(ThrowableCollector.java:73)位于org.junit.platform.engine.support.histrared.NodeTestTask.executeSecurively(NodeTestTask.java:126)网址:org.junit.platform.engine.support.histrared.NodeTestTask.exexecute(NodeTestTask.java:84)位于org.junit.platform.engine.support.hhierarchical.SameThreadHierarchicalTestExecutiorService.submit(SameThreadHIerarchicaltestExecutiorService.java:32)位于org.junit.platform.engine.support.hhierarchicalTestExecution.execute(HierarchicalTestExecution.java:57)位于org.junit.platform.engine.support.hhierarchicalTestEngine.exexecute(HierarchicalTestEngine.java:51)位于org.junit.platform.selauncher.core.EngineExecutionOrchestrator.exexecute(EngineExecutionOrhestrator.java:108)位于org.junit.platform.selauncher.core.EngineExecutionOrchestrator.exexecute(EngineExecutionOrhestrator.java:88)位于org.junit.platform.selauncher.core.EngineExecutionOrchestratorLambda$execute$0(EngineExecutionOrhestrator.java:54)位于org.junit.platform.selauncher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrhestrator.java:67)位于org.junit.platform.selauncher.core.EngineExecutionOrchestrator.exexecute(EngineExecutionOrhestrator.java:52)位于org.junit.platform.selauncher.core.DefaultLauncher.execute(DefaultLauncher.java:96)位于org.junit.platform.selauncher.core.DefaultLauncher.execute(DefaultLauncher.java:75)网址:com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs网址:com.intellij.rt.junit.IideaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)网址:com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeatr.java:11)网址:com.intellij.rt.junit.IideaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)网址:com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)网址:com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
根据您的代码行27
driver.get("http://localhost:8080/CoralloSmart/login");
driver
为空,这意味着您还没有启动驱动程序
private static WebDriver driver;
@Before
public void setup(){
driver = SystemTestCase.driver;
}
在该代码中,SystemTestCase.driver
为空
你能做的就是启动一个驱动程序并等待它
@Before
public void setup(){
if(SystemTestCase.driver == null ) {
SystemTestCase.driver=new ChromeDriver();
WebDriverWait wait=new WebDriverWait(driver, 20);
}
driver = SystemTestCase.driver;
}