页面对象工厂 - NullPointerException

时间:2018-05-24 10:02:08

标签: java selenium pageobjects page-factory

使用页面对象模型和页面因子来登录页面,我在LoginPage.java中获取对象,并且操作在LoginScript.java中。 我在行" Ele_UserNameEdit.clear();"中获得了java.lang.NullPointerException。请帮助检查代码。感谢。

这是我的Loginpage.java:

package com.cos.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

public class loginpage {
    public String Logout_Xpath = "//nav[@class=\"menu_right ng-tns-c1-0 ng-star-inserted\"]/a[3]/div/img";

    @FindBy(id = "email_input") 
    WebElement Ele_UserNameEdit;

    @FindBy(id = "email_input")
    WebElement USERNAME_ELE;

    @FindBy(id="password_input") 
    WebElement Ele_PasswordEdit;

    @FindBy(xpath = ".//*[@class='theme_button blue log_in_btn']")
    WebElement Ele_LoginButton;

    @FindBy(xpath = "html/body/ngb-modal-window/div/div/selectcountry/div/div/div[1]/div/div[1]/div/div/i")
    WebElement Ele_ThailandRadioButton; 

    @FindBy(xpath = "//div[@class=\"theme_button blue pointer\"]")
    WebElement Ele_ExploreThePortalButton;      

    @FindBy(xpath = "//div[@class=\"theme_button blue logout_btn\"]")
    WebElement Ele_LogoutConfirmationButton;    

    public void HomePage(WebDriver driver){
        PageFactory.initElements(driver, this);
    }

    public void enterUserName(String UserName){
        Ele_UserNameEdit.clear();
        Ele_UserNameEdit.sendKeys(UserName);
    }

    public void enterPassword(String Password){
        Ele_PasswordEdit.clear();
        Ele_PasswordEdit.sendKeys(Password);
    }

    public void clickOnLogin(WebDriver driver){
        try {
            WebDriverWait wait = new WebDriverWait(driver, 20);
            Ele_LoginButton.click();
            Thread.sleep(4000);
            Ele_ThailandRadioButton.click();
            Thread.sleep(4000);
            Ele_ExploreThePortalButton.click();
            Thread.sleep(4000);
            WebElement coslogoutLink;
            coslogoutLink = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(Logout_Xpath)));
            Assert.assertTrue(coslogoutLink.isDisplayed());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void clickOnLogout(WebDriver driver) {
        //Logout
        WebDriverWait wait = new WebDriverWait(driver, 20);
        WebElement coslogoutLink;
        coslogoutLink = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(Logout_Xpath)));
        Assert.assertTrue(coslogoutLink.isDisplayed());
        coslogoutLink.click();
        Ele_LogoutConfirmationButton.click();
        // close Fire fox
        driver.close();         
    }
}

这是我的LoginScript.java:

package com.cos.test;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.cos.actions.LoginActions;

import excel.ReadDataFromExcel;

public class LoginScript extends BaseClass {

    @Test(dataProvider = "readFromExcel")
    public void testLogin(String userName, String password) {
        LoginActions actions = new LoginActions();
        actions.login(driver, userName, password);
        actions.logout(driver);
    }

    @DataProvider(name = "readFromExcel")
    public Object[][] readDataFromExcel() throws Exception {
        Object[][] dataFromExcel = ReadDataFromExcel.readDataFromExcel();
        return dataFromExcel;
    }
}

1 个答案:

答案 0 :(得分:0)

首先,您已复制@FindBy(id = "email_input")

使用new loginpage();

调用初始化时,

public class loginpage应该有构造函数

PageFactory.initElements(driver, this);

所以当你做loginpage login = new loginpage();它没有初始化你的PageFactory,因为你已经把它变成了一个方法而不是构造函数。

所以试试这个

public LoginPage(WebDriver webDriver) { 
    super(webDriver); //this is if You extend driver from super-class
    PageFactory.initElements(webDriver, this);
}

但只需将代码添加到pagelogin类下的构造函数中。

希望这有用。