简体   繁体   English

使用范围报告时出现空指针异常

[英]Null pointer exception when using extent report

I have two classes baseClass and TesCaseLoginPage .我有两个类baseClassTesCaseLoginPage When i am trying to execute TesCaseLoginPage class without any Report/Logger (ExtentReport),Test case is getting pass.当我尝试在没有任何报告/记录器(ExtentReport)的情况下执行TesCaseLoginPage类时,测试用例正在通过。 But when i am puttin extend report into my code, I am getting NullPointerException .Could you please help me但是当我将报告扩展到我的代码中时,我收到了NullPointerException 。你能帮我吗

I have written report.endTest(logger);我已经写了report.endTest(logger); in line no 51. Tried to comment this line and then executed.在第 51 行。尝试注释此行,然后执行。 This time i got same error in line no 50 ie这次我在第 50 行遇到了同样的错误,即

logger.log(LogStatus.INFO,"Browser Closed"); . . So every time i am commenting error line, getting same error in new log related line only.所以每次我评论错误行时,只会在新的日志相关行中遇到相同的错误。

Error log is as below :错误日志如下:

Url : http://store.demoqa.com//products-page//your-account// FAILED
> CONFIGURATION: @AfterClass close java.lang.NullPointerException at
> testcases.testCaseLoginPage.close(testCaseLoginPage.java:51) at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
> sun.reflect.NativeMethodAccessorImpl(NativeMethodAccessorImpl.java:62)
> 
> sun.reflect.DelegatingMethodAccessorImpl.(DelegatingMethodAccessorImpl.
> java:43) at java.lang.reflect.Method.invoke(Method.java:498) at 
> org.testng.internal.MethodInvocationHelper.(MethodInvocationHelper.java:104)
> 
> at
> org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:515)
> at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:217)
> at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:144)
> at 
> org.testng.internal.invokeAfterClassMethods(TestMethodWorker.java:217)
> at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:115)
> at org.testng.TestRunner.privateRun(TestRunner.java:756) at
> org.testng.TestRunner.run(TestRunner.java:610) at
> org.testng.SuiteRunner.runTest(SuiteRunner.java:387)   at
> org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382) at
> org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) at
> org.testng.SuiteRunner.run(SuiteRunner.java:289) at
> org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at
> org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at
> org.testng.TestNG.runSuitesSequentially(TestNG.java:1293) at
> org.testng.TestNG.runSuitesLocally(TestNG.java:1218) at
> org.testng.TestNG.runSuites(TestNG.java:1133) at
> org.testng.TestNG.run(TestNG.java:1104) at
> org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
> at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236) at
> org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81) FAILED:
> login java.lang.NullPointerException at
> testcases.testCaseLoginPage.login(testCaseLoginPage.java:35) at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
> reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

Here is the code :这是代码:

baseClass.java基类.java

package testcases;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
import factory.BrowserFactory;
import factory.DataproviderFactory;
import pages.LoginPage;

public class baseClass 
{

    WebDriver driver;
    ExtentReports report;
    ExtentTest logger;
    String loginText;

    public void setUP()
    {
        driver=BrowserFactory.getBrowser("chrome");
        driver.get(DataproviderFactory.getConfig().getApplicationurl());
    }

    public void loginIntoApplication()
    {       
        LoginPage login=PageFactory.initElements(driver, LoginPage.class);
        login.Login(DataproviderFactory.getExcel().getData(0, 0, 0), 
        DataproviderFactory.getExcel().getData(0, 0, 1));
    }

    public String LoginVerification()
    {
        try {
            Thread.sleep(5000);
        } catch (Exception e) {
        System.out.println(e.getMessage());
        }
        //WebDriverWait wait=new WebDriverWait(driver, 20);
        //wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[@id='wp-admin-bar-my-account']/a")));

        loginText=driver.findElement(By.xpath("//li[@id='wp-admin-bar-my-account']/a")).getText();
        logger.log(LogStatus.INFO, "Logiin account text has been captured successfully");
        return loginText;
    }

    public void TearDown()
    {
        driver.quit();
    }
}

testCaseLoginPage.java testCaseLoginPage.java

package testcases;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; 
import com.relevantcodes.extentreports.LogStatus;
import pages.LoginPage;


public class testCaseLoginPage extends baseClass
{

    //WebDriver driver;
    //baseClass base=PageFactory.initElements(driver, baseClass.class);

    @BeforeTest
    public void initial()
    {
        base.setUP();
    }

    @Test(priority=1)
    public void login()
    {

        base.loginIntoApplication();
        try {
            Thread.sleep(5000);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        //WebDriverWait wait=new WebDriverWait(driver, 20);
        //wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(.,'Howdy')]")));
        logger.log(LogStatus.INFO, "Logiin into applicaion successfully");
        //LoginPage login=PageFactory.initElements(driver, LoginPage.class);

        LoginPage login=new LoginPage(driver);
        base.LoginVerification();
        System.out.println("Login Text : "+base.loginText);

        Assert.assertEquals(base.loginText, login.Exp,"Unable to verify login verification");   
        logger.log(LogStatus.PASS, "Verified login account with expected user successfully");

    }

    @AfterTest
    public void close()
    {
        base.TearDown();
        logger.log(LogStatus.INFO, "Browser Closed");
        report.endTest(logger);
        report.flush();
    }
}

Initialize the logger in your Test.在您的测试中初始化记录器。

use this line:使用这一行:

logger = extent.startTest("Test Name","Description");

Make it the first line even before you call the logger in your test.甚至在测试中调用记录器之前,将其作为第一行。

For reference see Software Automata - Extent Reports in Selenium有关参考,请参阅软件自动机 - Selenium 中的范围报告

I am also facing the same issue, using Extent 2.41 Keeping default file on loaction is mandatory?, if yes then what will happen in case of you are generating the file at run time and providing the new file name every time @ Upkar singh 我也面临着相同的问题,使用范围2.41是否必须保留默认文件在位置上?如果是,那么在运行时生成文件并每次@Upkar singh都提供新文件名的情况下会发生什么?

#
package pages;

import java.util.concurrent.TimeUnit;

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.How;
import org.openqa.selenium.support.PageFactory;

import org.testng.Assert;

import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;

public class homePage   {

    WebDriver driver;
     ExtentTest reports;
     ExtentReports reporting;
    @FindBy(how = How.XPATH, using ="//a[@title=\"Log in to your customer account\"]")
    public WebElement signin;
    @FindBy(how = How.XPATH, using ="//img[@alt=\"My Store\"]")
    public WebElement pageLogo;
    @FindBy(how = How.XPATH, using ="//input[@name=\"search_query\"]")
    public WebElement search;
    @FindBy(how = How.XPATH, using ="//a[ @title=\"View my shopping cart\"]/descendant::span[5][text()=\"(empty)\"]")
    public WebElement emptycart;
    @FindBy(how = How.XPATH, using ="//strong[text()=\"0123-456-789\"]")
    public WebElement phonenumber;
    @FindBy(how = How.XPATH, using ="//a[@title=\"Women\"]")
    public WebElement womenLink;
    @FindBy(how = How.XPATH, using ="//a[@title=\"Dresses\"]")
    public WebElement dressesLink;
    @FindBy(how = How.XPATH, using ="//a[@title=\"T-shirts\"]")
    public WebElement TshirtsLink;
    @FindBy(how = How.XPATH, using ="//li//li[@class=\"sfHoverForce\"]//ul[@style=\"display: none;\"]/descendant::a[text()=\"Casual Dresses\"][1]")
    public WebElement casualdressesLink;

    public homePage(WebDriver driver, ExtentReports reporting)
    {  
        //
        this.driver=driver;
        this.reporting=reporting;
        Extend_Report.invokeReporting();
        PageFactory.initElements(driver, this);

    }   
#

Page code 页码

 package pages; import java.util.concurrent.TimeUnit; 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.How; import org.openqa.selenium.support.PageFactory; import org.testng.Assert; import com.relevantcodes.extentreports.ExtentReports; import com.relevantcodes.extentreports.ExtentTest; import com.relevantcodes.extentreports.LogStatus; public class homePage { WebDriver driver; ExtentTest reports; ExtentReports reporting; @FindBy(how = How.XPATH, using ="//a[@title=\\"Log in to your customer account\\"]") public WebElement signin; @FindBy(how = How.XPATH, using ="//img[@alt=\\"My Store\\"]") public WebElement pageLogo; @FindBy(how = How.XPATH, using ="//input[@name=\\"search_query\\"]") public WebElement search; @FindBy(how = How.XPATH, using ="//a[ @title=\\"View my shopping cart\\"]/descendant::span[5][text()=\\"(empty)\\"]") public WebElement emptycart; @FindBy(how = How.XPATH, using ="//strong[text()=\\"0123-456-789\\"]") public WebElement phonenumber; @FindBy(how = How.XPATH, using ="//a[@title=\\"Women\\"]") public WebElement womenLink; @FindBy(how = How.XPATH, using ="//a[@title=\\"Dresses\\"]") public WebElement dressesLink; @FindBy(how = How.XPATH, using ="//a[@title=\\"T-shirts\\"]") public WebElement TshirtsLink; @FindBy(how = How.XPATH, using ="//li//li[@class=\\"sfHoverForce\\"]//ul[@style=\\"display: none;\\"]/descendant::a[text()=\\"Casual Dresses\\"][1]") public WebElement casualdressesLink; public homePage(WebDriver driver, ExtentReports reporting) { // this.driver=driver; this.reporting=reporting; Extend_Report.invokeReporting(); PageFactory.initElements(driver, this); } 

public void LaunchHomepage() { public void LaunchHomepage(){

 driver.get("http://automationpractice.com"); 

} }

public void verificationofElementsHomepage() { try { //driver.wait(10000); 公共无效的VerificationofElementsHomepage(){试试{// driver.wait(10000); if(signin.isDisplayed()) { System.out.println("element found"); if(signin.isDisplayed()){System.out.println(“找到元素”);

  } Assert.assertTrue(signin.isDisplayed()); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); Assert.assertTrue(true); System.out.println("Assertion Started"); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); Assert.assertTrue(pageLogo.isDisplayed(),"page logo not found"); System.out.println("Assertion1 Started"); //driver.wait(10000); Assert.assertTrue(search.isDisplayed()); System.out.println("Assertion2 Started"); Assert.assertTrue(emptycart.isDisplayed(),"342"); System.out.println("Assertion3 Started"); //driver.wait(10000); Assert.assertTrue(phonenumber.isDisplayed()); //driver.wait(10000); System.out.println("Assertion4 Started"); //driver.wait(10000); Assert.assertTrue(womenLink.isDisplayed()); /*System.out.println("Assertion5 Started"); Assert.assertTrue(dressesLink.isDisplayed()); driver.wait(10000);; System.out.println("Assertion6 Started"); Assert.assertTrue(TshirtsLink.isDisplayed()); System.out.println("Assertion7 Started"); Actions eleselection=new Actions(driver); eleselection.moveToElement(dressesLink).build().perform(); Assert.assertTrue(casualdressesLink.isDisplayed()); System.out.println("Assertion10 Started");*/ reports.log(LogStatus.PASS, "All elements found"); Extend_Report.reporting.endTest(reports); Extend_Report.reporting.flush(); } catch(Exception e) { System.out.println(e.getMessage()); System.out.println("On Home page Elements not found" + e.getMessage()); reports.log(LogStatus.FAIL, e.getMessage()); driver.close(); Extend_Report.reporting.endTest(reports); Extend_Report.reporting.flush(); } 

} }

 public void login(String Uname, String Password) { driver.findElement(By.id("Abc")).sendKeys("Uname"); driver.findElement(By.id("Def")).sendKeys("Password"); } 

} }

#

Extent Report class 范围报告类

package pages; 包装页面;

import java.io.File; 导入java.io.File; import java.util.Date; 导入java.util.Date;

import com.relevantcodes.extentreports.ExtentReports; 导入com.relevantcodes.extentreports.ExtentReports;

public class Extend_Report { 公共类Extend_Report {

 public static ExtentReports reporting; public static String reportpath=System.getProperty("user.dir")+"//reports1//"+new Date().getTime()+".html"; public static ExtentReports invokeReporting() //public Extend_Report() { if(reporting==null) reporting=new ExtentReports(reportpath,false); reporting.loadConfig(new File(reportpath)); return reporting; }`enter code here` 

} }

#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM