两个关系类(页面对象模型)传递对象时抛出空指针异常

Relationship between two classes (page object model) throws null pointer exception while passing objects

我是论坛新手。抱歉,如果问题太长。我粘贴了四个文件。两个 class 文件是 PageObject 文件。一个文件是测试文件,另一个是初始化WebDriver。

当我 运行 登录测试文件时出现空指针异常。

//首页

package com.base.pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.Reporter;

public class ActitimeLoginPage {
protected WebDriver driver;

public ActitimeLoginPage(WebDriver driver){
    PageFactory.initElements(driver, this);

}

@FindBy(xpath="//input [@ name = 'username']")
public WebElement txtusername;

@FindBy(xpath="//input[@type='password']")
public WebElement txtpassword;

@FindBy(xpath="//input[@type='submit']")
public WebElement btnLogin;

@FindBy(xpath="//table[1]/tbody/tr[7]/td[3]")
public WebElement versionnumber;

@FindBy(xpath="//a[@href='http://www.actimind.com']")
public WebElement actimindlink;

public Opentaskpage login(String username, String password){
    txtusername.sendKeys(username);
    Reporter.log("Entered Username",true);
    txtpassword.sendKeys(password);
    Reporter.log("Entered password",true);
    btnLogin.click();
    Reporter.log("clicked on login",true);

    return new Opentaskpage(driver);

}

}

//第二页

package com.base.pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.Reporter;

public class Opentaskpage {
protected WebDriver driver;

public Opentaskpage(WebDriver driver){
    PageFactory.initElements(driver, this);

}

@FindBy(xpath="//td[contains(text(),'Open Tasks')]")
public WebElement Heading;

@FindBy(xpath="/login.do?logout=1")
public WebElement btnLogout;

public void Logout(){
    btnLogout.click();
    Reporter.log("Clicked on logout",true);
    //return new ActitimeLoginPage(this.driver);
}


}

//预处理文件

package com.base.generic;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

public class SuperTestNg {


public WebDriver driver = new FirefoxDriver();
@BeforeMethod
public void precondtion(){

    driver.get("http://indbrnb0033/login.do");
    driver.manage().window().maximize();



}

@AfterMethod
public void postcondtion(){
    driver.close();

}

}

// 测试文件

package com.Acti.Tests;

import org.testng.annotations.Test;

import com.base.generic.SuperTestNg;
import com.base.pages.ActitimeLoginPage;
import com.base.pages.Opentaskpage;

public class Login extends SuperTestNg {
@Test
public void Test_Login(){

    ActitimeLoginPage page = new ActitimeLoginPage(driver);
    Opentaskpage page2 = page.login("admin", "manager");
    page2.Logout();


}

}

我不知道如何准确地提出问题。第一页的一个方法returns一个构造函数,我无法初始化第二页。

如果我做错了什么,请帮助我,或者告诉我最好的方法。提前致谢。

我收到错误:

Exception in thread "main" java.lang.NullPointerException
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
    at com.sun.proxy.$Proxy2.click(Unknown Source)
    at com.actitime.OpentaskPage.clicklogout(OpentaskPage.java:20)
    at com.actitime.ActitileLogin_Test.main(ActitileLogin_Test.java:27)

看起来像打字错误。您能否在以下构造函数中将输入参数的名称更改为 "driver":

public Opentaskpage(WebDriver drier){
PageFactory.initElements(driver, this);

}

它说 "drier",请将其更改为 "driver" 并重试。