如何在页面对象中使用 Extent Report 日志?

How to use Extent Report logs in Page Objects?

在页面对象中使用 test.log() 方法时出现空指针异常。

我的范围报告测试在 TestBase class 的“@BeforeMethod”中定义。因此,我需要访问 test.log();进入页面对象,例如LoginPage.java。它在测试用例级别运行良好,即 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);
}

下面是 LoginPage.java(这是一个页面对象 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());
    }
}
}

正如您在 LoginPage class 中看到的那样,我使用了 TestBase.logExtentReport() 方法,该方法显示 NullPointerException,我无法在 PageObject class 中初始化 TestBase 引用.因此,如何在那里使用 logExtentReport 方法?

Helper Class 也出现 NPE,即使在将记录器的范围从最终更改为静态之后也是如此。下面是代码:

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;
    }
}

}

改变一下

private final Logger log = LoggerHelper.getLogger(LoginPage.class);

private static final Logger log = LoggerHelper.getLogger(LoginPage.class);