Buse Kaya
Buse Kaya

Reputation: 95

cucumber.runtime.CucumberException: java.lang.NullPointerException

I am using to execute my selenium test with Cucumber, Maven and TestNG and Appium. When i started this automation by using TestNG via TestNG.xml. App launched but There is an error on running feature files selenium cucumber. I am getting the below error. Please help to resolve this error.

cucumber.runtime.CucumberException: java.lang.NullPointerException
    at cucumber.api.testng.TestNGCucumberRunner.runCucumber(TestNGCucumberRunner.java:69)
    at runner.RunnerTest.feature(RunnerTest.java:54)
    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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:584)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.testng.TestRunner.privateRun(TestRunner.java:770)
    at org.testng.TestRunner.run(TestRunner.java:591)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
    at org.testng.SuiteRunner.run(SuiteRunner.java:304)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
    at org.testng.TestNG.runSuites(TestNG.java:1032)
    at org.testng.TestNG.run(TestNG.java:1000)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.NullPointerException
    at com.odealMobile.Login.ClickLogin(Login.java:41)
    at ✽.When click on login button(src/main/java/features/Android/Login.feature:6)

Details of Plugins Versions:

My Test Class;

package com.odealMobile;

import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import runner.RunnerTest;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class Login extends RunnerTest {

 String loginButtonId = "login";
 String registerButtonId = "register";
 String TCKNId = "tckn";
 String passwordId = "password";
 String profileImageId = "profile_image_view";

 @Given("^odeal app is opened$")
 public void CheckOpenedPage() {
  System.out.print(By.id(loginButtonId));
  wait.until(ExpectedConditions.elementToBeClickable(By.id(loginButtonId)));
  //loginButtonId bekleniyor.
  wait.until(ExpectedConditions.elementToBeClickable(By.id(registerButtonId)));
 }
 @When("^click on login button$")
 public void ClickLogin() {
  // loginButtonId ye tıklanıyor.
  driver.findElement(By.id(loginButtonId)).click();
 }

 @Then("^login page will be opened$")
 public void CheckLoginPage() {
  wait.until(ExpectedConditions.elementToBeClickable(By.id(loginButtonId)));
 }
 @When("^user enters valid TCKN$")
 public void EnterTCKN() {
  driver.findElement(By.id(TCKNId)).click();
  driver.findElement(By.id(TCKNId)).setValue("23231487730");
 }
 @And("^user enters valid password$")
 public void EnterPassword() {
  driver.findElement(By.id(passwordId)).click();
  driver.findElement(By.id(passwordId)).setValue("135246");
 }
 @And("^clicks on login button$")
 public void ClickLoginonLoginPage() {
  driver.findElement(By.id(loginButtonId)).click();
 }
 @Then("^home page will be opened$")
 public void CheckHomePage() {
  wait.until(ExpectedConditions.elementToBeClickable(By.id(profileImageId)));
  if (driver.findElement(By.id(profileImageId)).isDisplayed()) {
   System.out.println("Login Success");
  } else {
   System.out.println("Login Failed");
  }
 }
}

My Runner Class;

package runner;

import cucumber.api.CucumberOptions;
import cucumber.api.testng.CucumberFeatureWrapper;
import cucumber.api.testng.TestNGCucumberRunner;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;


@CucumberOptions(
 plugin = {
  "pretty",
  "json:target/report/cucumber2.json"
 },
 features = {
  "src/main/java/features/Android/Login.feature"
 },
 glue = {
  "com/odealMobile"
 }
)
public class RunnerTest {

 public AppiumDriver < MobileElement > driver;
 public WebDriverWait wait;
 private TestNGCucumberRunner testNGCucumberRunner;

 @BeforeClass(alwaysRun = true)
 public void setUpClass() throws Exception {
  DesiredCapabilities cap = new DesiredCapabilities();
  cap.setCapability("deviceName", "TestDevice-1");
  cap.setCapability("automationName", "UiAutomator2");
  cap.setCapability("udid", "emulator-5554");
  cap.setCapability("platformName", "Android");
  cap.setCapability("platformVersion", "10.0");
  //cap.setCapability("autoGrantPermissions", "true");
  cap.setCapability("noReset", "false");
  cap.setCapability("clearSystemFiles", "true");
  cap.setCapability("appPackage", "com.telera.merchant.stage.debug");
  cap.setCapability("appActivity", "com.telera.merchant.splash.SplashActivity");

  driver = new AndroidDriver < MobileElement > (new URL("http://127.0.0.1:4723/wd/hub"), cap);
  wait = new WebDriverWait(driver, 10);
  testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
 }

 @Test(groups = "Cucumber", description = "Runs Cucumber Feature", dataProvider = "features")
 public void feature(CucumberFeatureWrapper cucumberFeature) {
  testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature());
 }

 @DataProvider
 public Object[][] features() {
  return testNGCucumberRunner.provideFeatures();
 }

 @AfterClass(alwaysRun = true)
 public void tearDownClass() throws Exception {
  Thread.sleep(50000);
  driver.quit();
  //testNGCucumberRunner.finish();  
 }
}

My Pom.xml;

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>appium.mobile.test</groupId>
    <artifactId>odealMobile</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>Odeal Mobile</name>
    <description>Appium Testing for Mobile </description>
    <properties>
        <testng.version>6.7</testng.version>
        <cucumber.version>1.2.6</cucumber.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-guice</artifactId>
            <version>1.2.6</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-html</artifactId>
            <version>0.2.3</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>${cucumber.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${cucumber.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-jvm -->
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-jvm</artifactId>
            <version>${cucumber.version}</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>7.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.cucumber/tag-expressions -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>tag-expressions</artifactId>
            <version>2.0.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>
    </dependencies>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.18.1</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

My testng.xml;

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
     <classes>
        <class name="runner.RunnerTest"/>
     </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Upvotes: 1

Views: 5594

Answers (1)

M.P. Korstanje
M.P. Korstanje

Reputation: 12059

The important part of the error is right here:

Caused by: java.lang.NullPointerException
    at com.odealMobile.Login.ClickLogin(Login.java:41)
    at ✽.When click on login button(src/main/java/features/Android/Login.feature:6)

When you get a null pointer exception this means that a variable or field doesn't have a value. Either because it has been set to null or because it has never been set.

So what happens? In a very simplified way this:

RunnerTest runner = new RunnerTest();
runner.setUpClass() // This sets runner.driver

Login login = new Login();
login.ClickLogin() // This tries to use login.driver

First an instance of the RunnerTest class is created and driver is set when setupClass is called. Then a new instance of Login is created. Because this is a different instance, even though it extends the RunnerTest class, the driver variable is never set. So when call ClickLogin is used a null pointer happens.

You can fix this in a few ways. The easiest is probably making driver static, not having Login extend RunnerTest and referencing driver as RunnerTest.driver.

public class RunnerTest{  

    public static AppiumDriver<MobileElement> driver;

}
public class Login {

        @When("^click on login button$")
        public void ClickLogin() {

            // loginButtonId ye tıklanıyor.
            RunnerTest.driver.findElement(By.id(loginButtonId)).click();
        }
}

Upvotes: 1

Related Questions