简体   繁体   中英

java.lang.NullPointerException error On Page Object Model

Hi Guys I am following Page Object Model in Java with Cucumber. I have following classes:

AbstractClass.java: Initiates the webdriver instance and serves as Master class for other object classes.

package ObjectRepository;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


/**
 * Hello world!
 *
 */
public class AbstractClass {
    public static WebDriver driver=null;
    Properties p = new Properties();   // create an object of properties

    public AbstractClass(WebDriver driver){
        this.driver=driver;

    }
    public void setup() throws IOException{

        // file input stream obect
        FileInputStream fi= new FileInputStream("C:\\Users\\sulemanmazhar\\Google Drive\\Automation Learning\\Java\\udemyWorkspace\\DebitCard_PageObject\\src\\test\\java\\Banking\\global.properties");
        p.load(fi); // load the properties
        if (p.getProperty("browser").contains("fireFox")){  // if browser in property file is firefox
            driver=new FirefoxDriver();         
        };  

        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    }



    public loginPage navigateToWelcomePage(){
        String url=p.getProperty("url");
        System.out.println(url);
        driver.navigate().to(url);
        return new loginPage(driver);
    }

    public String getTitle(){
        String title= driver.getTitle().trim();
        return title;
    }

    public void quitDriver(){
        driver.quit();
    }
}

loginPage.Java: Serves as Object repository class for login Page of my SUT. Inherits Abstract Class

      package ObjectRepository;

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


    /**
     * This class is within the object repository package
     * This package will contain all the objects which will
     * These objects will be stored and the will be accessed from the test/java folder


    public class loginPage extends AbstractClass 
        public loginPage(WebDriver driver){
            super(driver);

        }
        // all login page objects belong to this page
        By username = By.name("userName");   // object
        By password = By.name("password");
        By name= By.name("login");  
        By registerLink = By.linkText("REGISTER");
        By signInLink=By.linkText("SIGN-ON");

    // constructor passes a webdriver object    
        public flightFinder login(String userId,String passw){
            driver.findElement(username).sendKeys(userId);
            driver.findElement(password).sendKeys(passw);
            driver.findElement(name).click();

            return new flightFinder(driver);
           }


        public registerPage navigateToRegister() {

            driver.findElement(registerLink).click();
            return new registerPage(driver);
        }
        public loginPage navigateToSignOn(){
       driver.findElement(signInLink).click();
            return new loginPage(driver);   
        }
}

I also have registerPage.java , flighFinder.java which serves the same purpose as loginPage.java

I have corresponding test classes for each of the classes. The two classes I want to discuss are:

stepsDefiniton.java : This class contains most of the common methods that are applicable across different classes.

package TestClasses;

import java.io.IOException;

import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;

import ObjectRepository.flightFinder;
import ObjectRepository.loginPage;
import ObjectRepository.registerPage;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class StepsDefinition {

    WebDriver  driver;
    loginPage loginPage;
    //flightFinder flightFinder;
    registerPage registerPage;


    @Before
    public void setUp() throws IOException{
        System.out.println("Executing before method");

        loginPage = new loginPage(driver);
        loginPage.setup();
        loginPage.navigateToWelcomePage();
    }

    @After
    public void CloseDriver(){
        System.out.println("Executing after method");

        loginPage.quitDriver();
    }

    /* Shared steps */
    @Given("^I am on \"([^\"]*)\" page$")
    public void i_am_on_page(String page) {

    if(page.equalsIgnoreCase("sign-on")){
            loginPage=loginPage.navigateToSignOn();
        }   

    if(page.equalsIgnoreCase("register")){
        registerPage=loginPage.navigateToRegister();
        }   
        String Title =loginPage.getTitle();
        System.out.println("Actual page title is :"+ Title+" Expected Title is :"+page);
        Assert.assertTrue(Title.contains(page));

    }

    @When("^I click on \"([^\"]*)\" button$")
    public void i_click_on_button(String button) {
        if (button.equalsIgnoreCase("register")){
            registerPage=loginPage.navigateToRegister();    
        }
        else if(button.equalsIgnoreCase("sign-on")){
            loginPage=loginPage.navigateToSignOn();
        }
    }

    @Then("^I should be navigated to \"([^\"]*)\" page$")
    public void i_should_be_navigated_to_page(String page) {
        String Title =loginPage.getTitle().trim();
        System.out.println("Actual page is :"+Title +" Expected page is :"+page);
        Assert.assertTrue(Title.contains(page));
    }
}

LoginTest.Java : Is the Test class contains the code to test the logiN Page. package TestClasses;

import org.openqa.selenium.WebDriver;

import ObjectRepository.flightFinder;
import ObjectRepository.loginPage;
import ObjectRepository.registerPage;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;

public class LoginTest {
    //WebDriver  driver;
    loginPage loginPage;
    flightFinder flightFinder;
    registerPage registerPage;

    /* Login Page */
    @When("^I input my details as \"([^\"]*)\" and \"([^\"]*)\"$")
    public void i_input(String username, String password) {
        flightFinder=loginPage.login(username,password);

    }

    @Given("^I am logged in as \"([^\"]*)\" and \"([^\"]*)\"$")
    public void i_am_logged_in(String username, String password){
    flightFinder=loginPage.login(username,password);

    }
}

Ok my question is: When I execute the test all the tests within the StepsDefinition.java runs fine and as expectedly. As soon as it moves onto LoginTest.Java I get following error:

5 Scenarios ([31m3 failed[0m, [32m2 passed[0m)
16 Steps ([31m3 failed[0m, [36m5 skipped[0m, [32m8 passed[0m)
1m30.444s

java.lang.NullPointerException
    at TestClasses.LoginTest.i_am_logged_in(LoginTest.java:30)
    at ✽.Given I am logged in as "test" and "test"(featureFiles/flightFinder.feature:5)

java.lang.NullPointerException
    at TestClasses.LoginTest.i_input(LoginTest.java:23)
    at ✽.When I input my details as "test" and "test"(featureFiles/login.feature:12)

java.lang.NullPointerException
    at TestClasses.Register_Test.i_input_my_details(Register_Test.java:21)
    at ✽.When I input my details(featureFiles/register.feature:12)

What I think happening is, I am initiating the objects in stepsDefinition.Java class and as soon as the test moves onto another test class it is not carrying on with the previous state of the object.

If thats the case how can I pass on the state of one object to another? If not then where am I going wrong? Any help will be much appreciated.

The WebDriver is instantiated in the abstract class.

In the loginPage.Java,registerPage.java,flighFinder.java you are using driver which is not instantiated or driver which is instantiated in the abstract class is not passed to these classes.

Solution can be Remove driver=new FirefoxDriver(); from the setup of Abstract class instead place that in LoginTest and pass the driver object during loginPage,registerPage object creation.

ex: WebDriver driver = new ChromeDriver(); loginPage loginpage = new loginPage(driver);

I have tried it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM