NullPointerExeption in selenium test

66 Views Asked by At

I have this test class that extend 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("[email protected]");
    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();
  }

   */
}

These are the errors. Solution?

"C:\Program Files\Java\jdk-11.0.16.1\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.2.2\lib\idea_rt.jar=62181:C:\Program Files\JetBrains\IntelliJ IDEA 2022.2.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\grazi.m2\repository\org\junit\platform\junit-platform-launcher\1.7.0\junit-platform-launcher-1.7.0.jar;C:\Users\grazi.m2\repository\org\junit\vintage\junit-vintage-engine\5.7.0\junit-vintage-engine-5.7.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2022.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\IntelliJ IDEA 2022.2.2\plugins\junit\lib\junit-rt.jar;C:\Users\grazi\Desktop\CS\CoralloSmart\target\test-classes;C:\Users\grazi\Desktop\CS\CoralloSmart\target\classes;C:\Users\grazi\AppData\Roaming\JetBrains\IntelliJIdea2022.2\javascript\extLibs__maxcdn.bootstrapcdn.com_bootstrap_3.3.0_css_bootstrap.css;C:\Users\grazi\AppData\Roaming\JetBrains\IntelliJIdea2022.2\javascript\extLibs__maxcdn.bootstrapcdn.com_bootstrap_3.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-simple\1.1.1\json-simple-1.1.1.jar;C:\Users\grazi.m2\repository\commons-io\commons-io\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-connector-java\8.0.22\mysql-connector-java-8.0.22.jar;C:\Users\grazi.m2\repository\com\google\protobuf\protobuf-java\3.11.4\protobuf-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\apiguardian-api\1.1.0\apiguardian-api-1.1.0.jar;C:\Users\grazi.m2\repository\org\junit\platform\junit-platform-engine\1.7.0\junit-platform-engine-1.7.0.jar;C:\Users\grazi.m2\repository\org\junit\jupiter\junit-jupiter-api\5.7.0\junit-jupiter-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-1.7.0.jar;C:\Users\grazi.m2\repository\junit\junit\4.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-admin-2-4.0.6.jar;C:\Users\grazi.m2\repository\org\webjars\font-awesome\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-1.14.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-easing\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-buddy-1.10.18.jar;C:\Users\grazi.m2\repository\net\bytebuddy\byte-buddy-agent\1.10.18\byte-buddy-agent-1.10.18.jar;C:\Users\grazi.m2\repository\org\objenesis\objenesis\3.1\objenesis-3.1.jar;C:\Users\grazi.m2\repository\org\seleniumhq\selenium\selenium-chrome-driver\3.141.59\selenium-chrome-driver-3.141.59.jar;C:\Users\grazi.m2\repository\org\seleniumhq\selenium\selenium-api\3.141.59\selenium-api-3.141.59.jar;C:\Users\grazi.m2\repository\org\seleniumhq\selenium\selenium-remote-driver\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\google\guava\guava\25.0-jre\guava-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-qual-2.0.0.jar;C:\Users\grazi.m2\repository\com\google\errorprone\error_prone_annotations\2.1.3\error_prone_annotations-2.1.3.jar;C:\Users\grazi.m2\repository\com\google\j2objc\j2objc-annotations\1.1\j2objc-annotations-1.1.jar;C:\Users\grazi.m2\repository\org\codehaus\mojo\animal-sniffer-annotations\1.14\animal-sniffer-annotations-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\squareup\okio\okio\1.14.0\okio-1.14.0.jar;C:\Users\grazi.m2\repository\io\github\bonigarcia\webdrivermanager\4.2.2\webdrivermanager-4.2.2.jar;C:\Users\grazi.m2\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;C:\Users\grazi.m2\repository\org\apache\commons\commons-lang3\3.11\commons-lang3-3.11.jar;C:\Users\grazi.m2\repository\org\apache\httpcomponents\client5\httpclient5\5.0.1\httpclient5-5.0.1.jar;C:\Users\grazi.m2\repository\org\apache\httpcomponents\core5\httpcore5\5.0.1\httpcore5-5.0.1.jar;C:\Users\grazi.m2\repository\org\apache\httpcomponents\core5\httpcore5-h2\5.0.1\httpcore5-h2-5.0.1.jar;C:\Users\grazi.m2\repository\org\rauschig\jarchivelib\1.1.0\jarchivelib-1.1.0.jar;C:\Users\grazi.m2\repository\org\apache\commons\commons-compress\1.20\commons-compress-1.20.jar;C:\Users\grazi.m2\repository\org\jsoup\jsoup\1.13.1\jsoup-1.13.1.jar;C:\Users\grazi.m2\repository\org\apache\commons\commons-dbcp2\2.8.0\commons-dbcp2-2.8.0.jar;C:\Users\grazi.m2\repository\org\apache\commons\commons-pool2\2.8.1\commons-pool2-2.8.1.jar;C:\Users\grazi.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\grazi.m2\repository\com\h2database\h2\1.4.200\h2-1.4.200.jar;C:\Users\grazi.m2\repository\org\projectlombok\lombok\1.18.22\lombok-1.18.22.jar;C:\Users\grazi.m2\repository\org\apache\commons\commons-email\1.5\commons-email-1.5.jar;C:\Users\grazi.m2\repository\com\sun\mail\javax.mail\1.5.6\javax.mail-1.5.6.jar;C:\Users\grazi.m2\repository\javax\activation\activation\1.1\activation-1.1.jar;C:\Users\grazi.m2\repository\org\webjars\jquery\3.5.1\jquery-3.5.1.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit5 org.corallosmart.e2e.GestioneContributo.TestEffettuaVersamento

java.lang.NullPointerException at org.corallosmart.e2e.GestioneContributo.TestEffettuaVersamento.testEffettuaVersamento(TestEffettuaVersamento.java:27) 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.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688) at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60) at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131) at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149) at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140) at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84) at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115) at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105) at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37) at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104) at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:210) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:206) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:131) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:65) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84) at java.base/java.util.ArrayList.forEach(ArrayList.java:1541) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84) at java.base/java.util.ArrayList.forEach(ArrayList.java:1541) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32) at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67) at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75) at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57) at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38) at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

1

There are 1 best solutions below

0
On

as per your code line 27

driver.get("http://localhost:8080/CoralloSmart/login");

driver is null, which means you don't have driver started yet

private static WebDriver driver;

@Before
public void setup(){
   driver = SystemTestCase.driver;
}

in this code SystemTestCase.driver is null

what you can do is, start a driver and wait for it

@Before
public void setup(){
   if(SystemTestCase.driver == null ) {
       SystemTestCase.driver=new ChromeDriver();
       WebDriverWait wait=new WebDriverWait(driver, 20);
   }
   driver = SystemTestCase.driver;
}