Java 页面对象工厂-NullPointerException
对登录页面使用页面对象模型和页面分解,其中我在LoginPage.java中获取对象,操作在LoginScript.java中。 我在“Ele_usernamedit.clear();”行中得到一个java.lang.NullPointerException,请帮助检查代码。谢谢 这是我的Loginpage.java:Java 页面对象工厂-NullPointerException,java,selenium,pageobjects,page-factory,Java,Selenium,Pageobjects,Page Factory,对登录页面使用页面对象模型和页面分解,其中我在LoginPage.java中获取对象,操作在LoginScript.java中。 我在“Ele_usernamedit.clear();”行中得到一个java.lang.NullPointerException,请帮助检查代码。谢谢 这是我的Loginpage.java: package com.cos.pages; import org.openqa.selenium.By; import org.openqa.selenium.WebDri
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;
}
}
首先,您复制了
@FindBy(id=“email\u input”)
当使用new loginpage()调用初始化时,公共类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类下的构造函数中
希望这有帮助。页面工厂页面类未初始化。请查收。
public LoginPage(WebDriver webDriver) {
super(webDriver); //this is if You extend driver from super-class
PageFactory.initElements(webDriver, this);
}