I am getting Null Pointer Exception when using the test.log() method into Page Objects.
My Extent Report test is defined in the "@BeforeMethod" at the TestBase class. Hence, I need to access the test.log(); into the Page Object e.g. LoginPage.java. It's works fine at the test case level i.e. LoginPageTest.java
@BeforeMethod
public void beforeMethod(Method method) {
String testMethodName = method.getName();
test = extent.createTest(testMethodName);
String testReslt = method.getName();
test.info(MarkupHelper.createLabel(testReslt, ExtentColor.BLUE));
log.info("**************" + method.getName() + "Started***************");
}
public static void logExtentReport(String str) {
test.log(Status.INFO, str);
}
Below is the LoginPage.java (which is a page-object class)
public class LoginPage {
private WebDriver driver;
private final Logger log = LoggerHelper.getLogger(LoginPage.class);
VerificationHelper verificationHelper;
WaitHelper waitHelper;
@FindBy(css = "#email")
WebElement loginEmail;
@FindBy(css = "#password")
WebElement loginPassword;
@FindBy(css = "#loginbutton")
WebElement loginBtn;
@FindBy(css = "#loginerrormsg")
WebElement authenticationFailureMessage;
@FindBy(css = "#soflow-color")
WebElement userProfileDrpDwn;
@FindBy(xpath = "//option[@value='string:logout']")
WebElement logout;
@FindBy(tagName = "a")
List<WebElement> allLinks;
String urls[] = null;
public LoginPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
waitHelper = new WaitHelper(driver);
waitHelper.waitForElement(loginBtn,
ObjectReader.reader.getExplicitWait());
}
public void enterEmailAddress(String emailAddress) {
log.info("entering email address...." + emailAddress);
this.loginEmail.clear();
this.loginEmail.sendKeys(emailAddress);
}
public void enterPassword(String password) {
log.info("entering password...." + password);
this.loginPassword.clear();
this.loginPassword.sendKeys(password);
}
public ProspectorPage clickOnSubmitButton(String isValidCredentials) {
log.info("clicking on submit button...");
loginBtn.click();
if (isValidCredentials.equalsIgnoreCase("yes")) {
return new ProspectorPage(driver);
}
return null;
}
public boolean verifySuccessLoginMsg() {
return new VerificationHelper(driver).isDisplayed(userProfileDrpDwn);
}
public boolean verifyAuthenticationFailureMsg() {
return new
VerificationHelper(driver).isDisplayed(authenticationFailureMessage);
}
public void loginToApplication(String emailAddress, String password,
String isValidCredentials) {
enterEmailAddress(emailAddress);
loginBtn.click();
enterPassword(password);
new TestBase().captureScreen("Login Page_1", driver);
clickOnSubmitButton(isValidCredentials);
}
public void logout() {
userProfileDrpDwn.click();
new TestBase().captureScreen("Logout", driver);
waitHelper.waitForElement(logout,
ObjectReader.reader.getExplicitWait());
logout.click();
log.info("clicked on logout link");
TestBase.logExtentReport("clicked on logout link");
waitHelper.waitForElement(loginBtn,
ObjectReader.reader.getExplicitWait());
}
}
}
As you can see in the LoginPage class, I have used TestBase.logExtentReport() method, which is showing NullPointerException, and I cannot initialize the TestBase reference in the PageObject class. Hence, How can I use the logExtentReport method there?
Helper Class is also getting NPE, even after changing the scope of logger from final to static. Below is the code:
import com.uiFramework.engie.prospector.helper.logger.LoggerHelper;
import com.uiFramework.engie.prospector.testbase.TestBase;
public class VerificationHelper {
private WebDriver driver;
private static Logger log =
LoggerHelper.getLogger(VerificationHelper.class);
public VerificationHelper(WebDriver driver){
this.driver = driver;
}
public boolean isDisplayed(WebElement element){
try{
element.isDisplayed();
log.info("element is Displayed.."+element.getText());
TestBase.logExtentReport("element is
Displayed.."+element.getText());
return true;
}
catch(Exception e){
log.error("element is not Displayed..", e.getCause());
TestBase.logExtentReport("element is not
Displayed.."+e.getMessage());
return false;
}
}
public boolean isNotDisplayed(WebElement element){
try{
element.isDisplayed();
log.info("element is present.."+element.getText());
TestBase.logExtentReport("element is
present.."+element.getText());
return false;
}
catch(Exception e){
log.error("element is not present..");
return true;
}
}
public String readValueFromElement(WebElement element){
if(null == element){
log.info("WebElement is null..");
return null;
}
boolean status = isDisplayed(element);
if(status){
log.info("element text is .."+element.getText());
return element.getText();
}
else{
return null;
}
}
public String getText(WebElement element){
if(null == element){
log.info("WebElement is null..");
return null;
}
boolean status = isDisplayed(element);
if(status){
log.info("element text is .."+element.getText());
return element.getText();
}
else{
return null;
}
}
}
Just change
private final Logger log = LoggerHelper.getLogger(LoginPage.class);
to
private static final Logger log = LoggerHelper.getLogger(LoginPage.class);