Nullpointer exception for pico container instantion's method running maven clean test

262 views Asked by At

I am getting nullpointerException while running my cucumber tests by mvn clean test.

Scenario: Run atlas                 # src/test/java/Atlas/Features/Atlas.feature:3
  Given Go to atlas and check title # Atlas.PageObjects.AtlasPage.openAtlas()
    java.lang.NullPointerException
    at Atlas.PageObjects.AtlasPage.openAtlas(AtlasPage.java:12)
    at âś˝.Go to atlas and check title(file:///C:/atlas%20qa/src/test/java/Atlas/Features/Atlas.feature:4)

I created WebDriverConfig.java file and I am using pico container to share this class. Code is working until classes are in the same package but I want to separate them. Please ask me for more information if it's needed.

package Atlas;

import io.cucumber.java.Before;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.Arrays;
import java.util.List;

public class WebDriverConfig {
    public WebDriver driver;

    @Before
    public void setup(){
        ChromeOptions chromeOptions = new ChromeOptions();

        WebDriverManager.chromedriver().setup();
        List<String> browserArguments = Arrays.asList(
                "--window-size=1920,1080",
//                "--start-maximized",
                "--no-sandbox",
                "--ignore-certificate-errors",
                "--disable-popup-blocking",
                //"--incognito",
                "--allow-no-sandbox-job",
                "--proxy-bypass-list=*");
        chromeOptions.addArguments("--headless");
        chromeOptions.addArguments(browserArguments);

        this.driver = new ChromeDriver(chromeOptions);
    }

    public WebDriver getDriver(){
        return driver;
    }
}
package Atlas.PageObjects;

import Atlas.WebDriverConfig;
import io.cucumber.java.en.Given;
import org.openqa.selenium.WebDriver;

public class AtlasPage {
    WebDriver driver;

    @Given("Go to atlas and check title")
    public void openAtlas() {
        driver.get("test env bla bla bla");
    }


    public AtlasPage(WebDriverConfig webDriverConfig) {
        this.driver = webDriverConfig.getDriver();
    }
}

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>atlas-automation-tests</artifactId>
    <version>1.0-SNAPSHOT</version>
        <build>
            <resources>
                <resource>
                    <directory>src</directory>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.2</version>
                    <configuration>
                        <includes>**/common.RunCucumberTest*.java</includes>
                        <forkCount>2</forkCount>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.1.2</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <!-- Jar file entry point -->
                                <mainClass>Main</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </build>

    <dependencies>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>4.1.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.0.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>6.9.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>6.9.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-picocontainer -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <version>6.9.1</version>
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
</project>
1

There are 1 answers

0
Piotrek Kapczyński On

Thanks to Alexey R.

Issue is resolved.

To solve this problem I had to add classes with @Before / @After annotations to glue property in CucumberRuner class:

glue = {"Atlas.PageObjects", "Atlas.common"}