java.lang.NullPointerException错误在页面对象模型上

时间:2015-10-22 11:26:14

标签: java selenium-webdriver cucumber automated-tests pageobjects

嗨大家我正在使用Cucumber跟踪Java中的页面对象模型。 我有以下课程:

AbstractClass.java:启动webdriver实例,并作为其他对象类的Master类。

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:用作我的SUT登录页面的对象存储库类。继承抽象类

      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);   
        }
}

我还有 registerPage.java flighFinder.java ,其功能与 loginPage.java

相同

我为每个类都有相应的测试类。我想讨论的两个课程是:

stepsDefiniton.java :此类包含适用于不同类的大多数常用方法。

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 :Test类是否包含测试logiN页面的代码。 包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);

    }
}

好的我的问题是: 当我执行测试时, StepsDefinition.java 中的所有测试都运行正常并且正如预期的那样。一旦它移动到 LoginTest.Java ,我就会收到以下错误:

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)

我的想法是,我在 stepsDefinition.Java 类中启动对象,并且一旦测试移动到另一个测试类,它就不会继续使用该对象的先前状态。

如果是这样的话,我怎样才能将一个对象的状态传递给另一个对象?如果不是那么我哪里错了?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

WebDriver在抽象类中实例化。

在loginPage.Java,registerPage.java,flighFinder.java中,您使用的是未实例化的驱动程序,或者在抽象类中实例化的驱动程序未传递给这些类。

解决方案可以是删除driver = new FirefoxDriver();从Abstract类的设置代替放置在LoginTest中并在loginPage期间传递驱动程序对象,创建registerPage对象。

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

我试过它有效。