java.lang.NullPointerException 页面对象模型错误
java.lang.NullPointerException error On Page Object Model
大家好,我正在使用 Cucumber 在 Java 中关注页面对象模型。
我有以下 classes:
AbstractClass.java: 启动 webdriver 实例并作为其他对象 classes.
的 Master class
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 登录页面的对象存储库 class。继承摘要 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);
}
}
我也有 registerPage.java,flighFinder.java 与 的用途相同loginPage.java
对于每个 classes,我都有相应的测试 classes。我想讨论的两个 classes 是:
stepsDefiniton.java :这个 class 包含大多数适用于不同 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 : 是测试 class 包含测试登录页面的代码。
包测试Classes;
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 class 中的对象,一旦测试进入另一个测试 class 它没有继续对象的先前状态。
如果是这样,我如何将一个对象的状态传递给另一个对象?如果不是那么我哪里错了?任何帮助将不胜感激。
WebDriver 在抽象中实例化 class。
在 loginPage.Java、registerPage.java、flighFinder.java 中,您正在使用未实例化的驱动程序或在抽象 class 中实例化的驱动程序未传递给这些驱动程序classes。
解决方法可以是 Remove driver=new FirefoxDriver();从 Abstract class 的设置中改为将其放在 LoginTest 中,并在 loginPage、registerPage 对象创建期间传递驱动程序对象。
例如:WebDriver 驱动程序 = new ChromeDriver();
loginPage loginpage = new loginPage(driver);
我已经试过了。
大家好,我正在使用 Cucumber 在 Java 中关注页面对象模型。 我有以下 classes:
AbstractClass.java: 启动 webdriver 实例并作为其他对象 classes.
的 Master classpackage 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 登录页面的对象存储库 class。继承摘要 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);
}
}
我也有 registerPage.java,flighFinder.java 与 的用途相同loginPage.java
对于每个 classes,我都有相应的测试 classes。我想讨论的两个 classes 是:
stepsDefiniton.java :这个 class 包含大多数适用于不同 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 : 是测试 class 包含测试登录页面的代码。 包测试Classes;
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 class 中的对象,一旦测试进入另一个测试 class 它没有继续对象的先前状态。
如果是这样,我如何将一个对象的状态传递给另一个对象?如果不是那么我哪里错了?任何帮助将不胜感激。
WebDriver 在抽象中实例化 class。
在 loginPage.Java、registerPage.java、flighFinder.java 中,您正在使用未实例化的驱动程序或在抽象 class 中实例化的驱动程序未传递给这些驱动程序classes。
解决方法可以是 Remove driver=new FirefoxDriver();从 Abstract class 的设置中改为将其放在 LoginTest 中,并在 loginPage、registerPage 对象创建期间传递驱动程序对象。
例如:WebDriver 驱动程序 = new ChromeDriver(); loginPage loginpage = new loginPage(driver);
我已经试过了。