Ami
Ami

Reputation: 111

Having issue with cucumber and selenium as the driver is always null with simple test case

I have 2 Class files excluding the .feature file, the runner class and step definitions class. Without creating the classes separately if I directly find the elements inside the step definitions class it works. But having a base class and having classes with page object model and page factory doesn't work anymore.

It keeps on showing:

java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.SearchContext.findElement(org.openqa.selenium.By)" because "this.searchContext" is null

or driver is null

and not clicking on the link it supposed to

Following are the codes for the classes:

Base class

public class Base {
    public static WebDriver driver;
    public static WebDriverWait wait;
    public static void startBrowser(String browser, String url){

        if (browser.equalsIgnoreCase("chrome")){

            driver = new ChromeDriver();

        } else if (browser.equalsIgnoreCase("firefox")) {

            driver = new FirefoxDriver();

        } else if (browser.equalsIgnoreCase("edge")) {

            driver = new EdgeDriver();

        }

        wait = new WebDriverWait(driver, Duration.ofSeconds(30));

        driver.manage().window().maximize();

        driver.get(url);

    }

}

HomePage class

public class HomePage extends Base {

    @FindBy(xpath = "//li[@id='menu-item-324']//a")
    public WebElement loginLink;

    public HomePage(){

        PageFactory.initElements(driver,this);
    }

    public void clickLoginLink(){

        loginLink.click();
    }

}

Step Definitions class

public class LoginSteps extends Base {

    HomePage home = new HomePage();

    @Given("user opens a a browser")
    public void user_opens_a_a_browser() throws InterruptedException {
        startBrowser("chrome", "domain.com");
    }

    @When("user enters the url")
    public void user_enters_the_url() {
        System.out.println("entered url");;
    }

    @And("clicks on login link")
    public void clicksOnLoginLink() {

        home.clickLoginLink();

    }

    @Then("user navigated to domain.com")
    public void user_navigated_to_domain_com() {
        System.out.println("navigated to");
    }



}

feature file

Feature: feature to test to validate login

  Scenario: Check login with valid credentials

    Given user opens a a browser
    When user enters the url
    And clicks on login link
    Then user navigated to domain.com

What could be the issue with this please?

Upvotes: 0

Views: 676

Answers (1)

Alexey R.
Alexey R.

Reputation: 8676

What could be the issue with this please?

When Cucumber initializes your step definition class, Java executes HomePage home = new HomePage(); straight away.

By that moment your driver has not yet been initialized as your startBrowser has not yet been executed.

So having that line comleted, you now have page object instance initilized with null driver.

P.S. - There are also several agruable points in your architecture. Get rid of static fields and methods. That will make you think your model more carefully and make you avoid issues in future. Also do not derive step defs. Especially from the same class your page object is derived to avoid mess in your logic.

Upvotes: 1

Related Questions