[英]Page Object Factory - NullPointerException
Using Page Object Model and Page Factoring for the login page, where I get the objects in the LoginPage.java and the actions are in the LoginScript.java. 使用登录页面的页面对象模型和页面分解,在LoginPage.java中获得对象,而操作在LoginScript.java中。 I get a java.lang.NullPointerException in the line "Ele_UserNameEdit.clear();" 我在“ Ele_UserNameEdit.clear();”行中得到了一个java.lang.NullPointerException。 please help to check the code. 请帮助检查代码。 Thanks. 谢谢。
This is my Loginpage.java : 这是我的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();
}
}
This is my LoginScript.java : 这是我的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;
}
}
First of all You have duplicated @FindBy(id = "email_input")
. 首先,您已经复制了@FindBy(id = "email_input")
。
public class loginpage should have constructor when initialization is called with new loginpage();
使用new loginpage();
调用初始化时,公共类loginpage应该具有构造函数new loginpage();
PageFactory.initElements(driver, this);
so when You do loginpage login = new loginpage(); 因此,当您执行登录页面登录时=新的loginpage(); it doesn't initialize Your PageFactory, because You've made it an method not constructor. 它不会初始化您的PageFactory,因为您已将其设为方法而非构造函数。
So try like this 所以尝试这样
public LoginPage(WebDriver webDriver) {
super(webDriver); //this is if You extend driver from super-class
PageFactory.initElements(webDriver, this);
}
But just add code into constructor under pagelogin class. 但是只需将代码添加到pagelogin类下的构造函数中即可。
Hope this is helpful. 希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.