0

对登录页面使用页面对象模型和页面分解,我在 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;
    }
}
4

1 回答 1

0

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

调用初始化时,公共类登录页面应该有构造函数new 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 类下的构造函数中。

希望这会有所帮助。

于 2018-05-24T11:28:09.543 回答