-1

I am getting null pointer exception. Would you be able to help and what should be fixed ?

java.lang.NullPointerException

at pages.LandingPage.acceptConsent(LandingPage.java:14)

at stepDefs.LandingPageStepDefs.customerAcceptsCookieConsent(LandingPageStepDefs.java:22)

at ✽.Customer accepts cookie consent(file:///C:/Users/Bartek/IdeaProjects/Cucumber/src/test/java/features/interia.feature:5)

Feature: test of a webpage

  @desktop
  Scenario: Test landing page
    Given Customer accepts cookie consent
    And I click "OK" button at the bottom of the page

====================================================


package stepDefs;

import io.cucumber.java.en.And;
import lombok.extern.slf4j.Slf4j;
import pages.LandingPage;


import static utils.BasePage.driver;

@Slf4j
public class LandingPageStepDefs {

    LandingPage landingPage = new LandingPage(driver);

    @And("Customer accepts cookie consent")
    public void customerAcceptsCookieConsent() {
        landingPage.acceptConsent();
    }

    @And("I click {string} button at the bottom of the page")
    public void iClickButtonAtTheBottomOfThePage(String text) {
        landingPage.clickButtonAtTheBottom(text);
    }


}


====================================================

package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import utils.BasePage;

public class LandingPage extends BasePage {

    public LandingPage(WebDriver driver) {
        super(driver);
    }

    public void acceptConsent() {
        driver.findElement(By.xpath("//p[contains(text(), 'Consent')]")).click();
    }

    public void clickButtonAtTheBottom(String text) {
        String xpath = String.format("//div[@id='cNag']//a[contains(text(), '%s')]", text);
        driver.findElement(By.xpath(xpath)).click();
    }
}


===================================================
package utils;

import lombok.Getter;
import lombok.Setter;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;


public class BasePage {

    @Getter @Setter
    public static WebDriver driver;

    public BasePage() {
    }

    public BasePage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

}
===============================================

package utils;

import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.BeforeAll;
import io.cucumber.java.Scenario;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class Hook extends BasePage {



    @BeforeAll
    public static void beforeAll() {
        // Runs before all scenarios
    }

    @Before("not @mobile")
    public void beforeDesktop(Scenario scenario) {

        System.setProperty("webdriver.chrome.driver", "C://Users//Bartek//eclipse-workspace/chromedriver.exe");
        driver = new ChromeDriver();

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();

        driver.get("https://www.netcarshow.com/");

    }

}

I am adding pom

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
    <artifactId>Cucumber</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.4.0</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.testng/testng -->

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>7.8.1</version>
        </dependency>



        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>7.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>compile</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.23.1</version>
            <scope>test</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.reflections</groupId>
            <artifactId>reflections</artifactId>
            <version>0.9.11</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.1-jre</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.8.3</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.26</version>
            <scope>compile</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>2.0.7</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>2.0.7</version>
        </dependency>



    </dependencies>

</project>
1

1 Answer 1

0

All the hooks, inheritance, and attribute initialization makes reasoning how the initialization code run very difficult. Probably it would require lots of debugging to understand how it works ATM.

I would suggest simply re-writing it to be cleaner.

E.g.

On the Hooks, you can configure one static class that will hold the driver.

@Before("not @mobile")
    public void beforeDesktop(Scenario scenario) {
        
        System.setProperty("webdriver.chrome.driver", "C://Users//Bartek//eclipse-workspace/chromedriver.exe");
        val driver = new ChromeDriver();

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();

        // driver.get("https://www.netcarshow.com/"); -> Removing it on purpose
        DriverWrapper.setDriver(driver);

    }

This class would be something like:

public static class DriverWrapper {
  @Setter @Getter
  private Driver driver;
}

Then on your steps, simply use it.

@Slf4j
public class LandingPageStepDefs {

   LandingPage landingPage;

   @Given("The user accesses the home page")
   public void customerAcceptsCookieConsent() {
      landingPage = new LandingPage(DriverWrapper.getDriver());
   }

   @And("Customer accepts cookie consent")
   public void customerAcceptsCookieConsent() {
       landingPage.acceptConsent();
   }

   @And("I click {string} button at the bottom of the page")
   public void iClickButtonAtTheBottomOfThePage(String text) {
       landingPage.clickButtonAtTheBottom(text);
   }


}
3
  • I tried your solution, but it doesn't work. Apr 26, 2023 at 20:36
  • Can you detail the error? Are you sure beforeDesktop runs before customerAcceptsCookieConsent? Apr 27, 2023 at 14:13
  • The problem was solved, it was not in the code but in intellij runner configuration. Apr 30, 2023 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.