-1

I am trying to run a page object methods by cucumber. I am getting error out with driver null NPE errpr. Could you please help. I created Page Object Model file. A feature file describing scenario outline. And then in Test, I am trying to call the method by creating a object of Page Model java class

POM class:

package pages;

import javax.imageio.IIOException;

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

public class Pom {
    
 static WebDriver driver;
    
    By username=By.xpath("//*[@id='user-name']");
    By password=By.xpath("//*[@id='password']");
    By loginbutton=By.xpath("//*[@id='login-button']");
    By logout=By.xpath("//*[contains(text(),'Logout')]");
    By open_menu=By.xpath("//*[contains(text(),'Open Menu')]");
    public Pom(WebDriver driver) 
    {
        this.driver=driver;
        
        //if (!driver.getTitle().equals("Swag Labs")) {
            //throw new IIOException("This is not the login page. The current url is " +driver.getCurrentUrl());
        }
//}
    
    public void enter_username(String username1)
    {
        driver.findElement(username).sendKeys(username1);
        
    }
    public void enter_password(String password1)
    {
        driver.findElement(password).sendKeys(password1);
        
    }
    public void click_login()
    {
        driver.findElement(loginbutton).click();
    }

    public void check_logout()
    {
        driver.findElement(logout).isDisplayed();
    }
    
    public void click_open_menu()
    {
        driver.findElement(open_menu).click();
    }

}

StepDefination:

package StepDefinations;

import java.time.Duration;

import javax.imageio.IIOException;

import org.junit.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.cucumber.java.en.*;
import io.github.bonigarcia.wdm.WebDriverManager;
import pages.Pom;


public class PomTest {
    
    public WebDriver driver;
    Pom pom=new Pom(driver);
    @Given("User launches the browser")
    public void user_launches_the_browser() {
        // Write code here that turns the phrase above into concrete actions
        WebDriverManager.chromedriver().setup();
           driver=new ChromeDriver();
           driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
           driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(6));
           driver.manage().window().maximize();
    }
    @Given("user navigate to the Login landing Page")
    public void user_navigate_to_the_login_landing_page() throws InterruptedException {
         
        // Write code here that turns the phrase above into concrete actions
        driver.navigate().to("https://www.saucedemo.com/");
          Thread.sleep(3000);
    }
    
     @When("^User enters the credentials as (.+) and (.+) $")
        public void user_enters_the_credentials_as_and(String username, String password) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        
        // pom=new Pom(driver);
         pom.enter_username(username);
         Thread.sleep(3000);
      pom.enter_password(password);
      Thread.sleep(3000);
      
      
    }
    @And("User clicks on the login button after entering credentials")
    public void user_clicks_on_the_login_button_after_entering_credentials() {
     pom.click_login();
      
    }
    @Then("User lands on the user account home page and checks the logout button")
    public void user_lands_on_the_user_account_home_page_and_checks_the_logout_button() {
        // Write code here that turns the phrase above into concrete actions
      pom.click_open_menu();
      pom.check_logout();
    }
    @Then("the user closes the browser")
    public void the_user_closes_the_browser() {
        // Write code here that turns the phrase above into concrete actions
       driver.close();
    }

}

Faeture File:

Feature: Login functionality Test 
Scenario Outline: Verify the login of the user on the particvular website 
    Given User launches the browser 
    And user navigate to the Login landing Page
    When User enters the credentials as <username> and <password> 
    And User clicks on the login button after entering credentials 
    Then User lands on the user account home page and checks the logout button
    And the user closes the browser

    Examples:
    |username|password|
    |standard_user|secret_sauce|
    |locked_out_user|secret_sauce|
    |problem_user|secret_sauce|
    |performance_glitch_user|secret_sauce|

I am expecting this code to run in a data driven way.

1 Answers1

0

@user3857859 , in your step definition file, move the initialization of pom object to inside @Given("User launches the browser"), after you initialize the driver. so after changes your @Given should look like below:

    public WebDriver driver;
    Pom pom;
    @Given("User launches the browser")
    public void user_launches_the_browser() {
        // Write code here that turns the phrase above into concrete actions
        WebDriverManager.chromedriver().setup();
           driver=new ChromeDriver();
           pom=new Pom(driver);
           driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
           driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(6));
           driver.manage().window().maximize();
    }

this way, you not sending the driver without initializing to your Pom constructor.

ketanvj
  • 511
  • 4
  • 5