0

ExtentReports 报告;
范围测试记录器;
当我使用 Extent 报告 1 时,代码对 1 个类运行正确,但对第 2 个类抛出空指针异常
。我在 @BeforeSuite
2 中初始化。然后在 @BeforeMethod
3 中初始化。在 testng.xml 中有 2 个类 Class1 和 Class2
4.执行时testng.xml - class1 的所有@Test 完全运行,但 Class2 在读取 BeforeMethod 时抛出空指针异常错误(如 step2 中所述初始化)

  1. 范围报告已在 Testbase 类中初始化并为其创建了getter,以便其他类可以读取它
    注意:当我将 BeforeSuite 更改为 BeforeClass 时,即在 BeforeClass 中完成初始化,然后它运行良好,但它只生成 class1 的范围报告。

另外我正在使用 Aftermethod 刷新报告并退出驱动程序。摆脱这个空指针异常的任何解决方案

下面是完整代码
1. TestBase 类

package sampletestproject;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import 
 org.openqa.selenium.chrome.ChromeDriver;
import 
org.openqa.selenium.firefox.FirefoxDriver;
import 
org.openqa.selenium.support.PageFactory;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeSuite;



public class TestBasee {
    private ExtentReports report;   
    public WebDriver driverObj;
    public Homepagee homeObj;

@BeforeSuite (alwaysRun = true)
public void beforeTest(){
    System.out.println("In @BeforeSuite");
    report = new ExtentReports("G:\\ExtentReport"+fn_GetTimeStamp()+".html");
    System.out.println("Out @BeforeSuite");
}



@AfterClass(alwaysRun = true)
public void tearDown(ITestContext context) throws IOException, InterruptedException{
    System.out.println("@AfterClass In tear down");
    ITestNGMethod[] tngMethods  = context.getAllTestMethods();
    int i=1;
    for(ITestNGMethod tng: tngMethods){         
        String methodName = "Method"+i+": "+ tng.getMethodName();
        i++;
        System.out.println(methodName);
    }
}
public static String fn_GetTimeStamp() {
    DateFormat DF = DateFormat.getDateTimeInstance();
    String DateVal = DF.format(new Date());
    DateVal = DateVal.replaceAll("/", "_");
    DateVal = DateVal.replaceAll(",", "_");
    DateVal = DateVal.replaceAll(":", "_");
    DateVal = DateVal.replaceAll(" ", "_");
    return DateVal;
}
                /******************** Open Site **************************/
public Homepagee gm_OpenApp(String BrowserName, String URL) throws Exception {
    System.out.println("In gm_OpenAp Method");
    System.out.println(BrowserName+" -- "+URL);     
    gm_LaunchBrowser(BrowserName);
    Thread.sleep(2000);
    gm_OpenURL(URL);
    Thread.sleep(2000);
    homeObj = PageFactory.initElements(driverObj, Homepagee.class);
    return homeObj;
}
public void gm_OpenURL(String URL) {
    driverObj.get(URL);
}   
public void gm_LaunchBrowser(String browserName) throws Exception{
    // Launch Chrome browser
     if (browserName.equalsIgnoreCase("CH") == true) {
         System.setProperty("webdriver.chrome.driver", "MasterFiles\\Drivers\\ChromeDriver\\Chromedriver_win32_v2.38\\chromedriver.exe");
        driverObj = new ChromeDriver();
     }
     // Launch Firefox browser
     else if (browserName.equalsIgnoreCase("FF") == true) {
         System.setProperty("webdriver.gecko.driver", "MasterFiles\\Drivers\\GeckoDriver\\64Bit\\v20\\geckodriver.exe");
         driverObj = new FirefoxDriver();
     }
     driverObj.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
     driverObj.manage().timeouts().pageLoadTimeout(250, TimeUnit.SECONDS);
     driverObj.manage().window().maximize();

}
                            /****************TAKE SCREENSHOT*****************/
public String gm_setScreenshotPath_forExtentReporting(String elementName, String resultStatus) {
    System.out.println("In gm_setScreenshotPath_forExtentReporting");
    System.out.println(elementName + "--" + resultStatus);
    String screenshotPath = "G:\\QA\\AutomationTools\\Selenium\\WorkspaceMars1\\1.2hp.com.automationprac\\TestReports\\ExtentReport\\Screenshots\\"+ resultStatus + "\\" + elementName + "_" + fn_GetTimeStamp() + ".png";
    return screenshotPath;
}
public void gm_TakeSnapshot(String destFilePath) throws IOException, InterruptedException {
    TakesScreenshot tss = (TakesScreenshot) driverObj;
    File srcfileobj = tss.getScreenshotAs(OutputType.FILE);
    File destFileObj = new File(destFilePath);
    FileUtils.copyFile(srcfileobj, destFileObj);

}
public ExtentReports getReport() {
    return report;
}


public void setReport(ExtentReports report) {
    this.report = report;
  }

  }

2.登录-页面对象

package sampletestproject;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class SignInPagee extends TestBasee{
@FindBy(xpath = "//div[@id = 'center_column']/h1")
 public static WebElement PageHeading_SignIn_txt;

 @FindBy(xpath = "//h3[contains(text(), 'Already registered?')]")
 public static WebElement SectionHeading_SignIn_txt;


    public SignInPagee(WebDriver driverObj){
        this.driverObj = driverObj;      
    }

  }

3. 主页 - PageObject

package sampletestproject;
import java.io.IOException;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import 
org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import 
org.openqa.selenium.support.PageFactory;
import 
org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import 
import com.google.common.base.Function;

public class Homepagee extends TestBasee {


public Homepagee(WebDriver driverObj){
      this.driverObj = driverObj;
  }

 public SignInPagee navigateToSignInPage(){
     System.out.println("In navigateToSignInPage"); 
     driverObj.navigate().to("http://automationpractice.com/index.php?controller=authentication&back=my-account");
     SignInPagee signInPageObj = PageFactory.initElements(driverObj, SignInPagee.class);
     return signInPageObj;
   }


}

4. HomepageTest -Testpage

package sampletestproject;
import java.lang.reflect.Method;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;

public class HomepageeTest extends TestBasee {
    //ExtentReports report;
    ExtentTest logger;
    String elementName;
    String Comment;
    String actualResult;
    String expectedResult;



    @BeforeMethod(alwaysRun = true)
    @Parameters({ "Browser", "URL"})
    public void getBrowser(Method method, String Browser, String URL) throws Exception{
        logger = getReport().startTest((this.getClass().getSimpleName()+"::"+method.getName()), method.getName());
        logger.assignAuthor("VK");
        logger.assignCategory("HomePage - Smoketesting and TextTesting--Live");
        homeObj = gm_OpenApp(Browser, URL);             

    }
     @AfterMethod (alwaysRun = true)
        public void publishReport_SIP(ITestResult result) throws Exception{
            System.out.println("publishReport_SIP");
            String resultStatus = null;         
            if(result.getStatus() == ITestResult.FAILURE){              
                resultStatus = "FAILED"; 
                String screenshot_Path = gm_setScreenshotPath_forExtentReporting(elementName, resultStatus);            
                gm_TakeSnapshot(screenshot_Path);               
                String image = logger.addScreenCapture(screenshot_Path);
                logger.log(LogStatus.FAIL, Comment, image);
            }else{              
                resultStatus = "PASSED";                
                String screenshot_Path = gm_setScreenshotPath_forExtentReporting(elementName, resultStatus);
                gm_TakeSnapshot(screenshot_Path);
                System.out.println("screenshot_Path: "+screenshot_Path);
                String image = logger.addScreenCapture(screenshot_Path);
                logger.log(LogStatus.PASS, Comment, image);
            }
            getReport().endTest(logger);
            getReport().flush();
            System.out.println("closing now_SIP.");
            driverObj.quit();
        }       

//"********Validation of SignIn Link********");
@Test(priority=0, groups = {"Smoke"})   
public  void validateHeaderSignInLink_HP() throws Exception{        
    System.out.println("In validateHeaderSignInLink Method_HP"); 
    elementName = "SignInLink";
    Comment = "validateHeaderSignInLink";
    actualResult = "http://automationpractice.com/index.php?controller=authentication&back=my-account";
    expectedResult = "http://automationpractice.com/index.php?controller=authentication&back=my-account";
    Assert.assertEquals(actualResult, expectedResult);
    System.out.println("Out  of validateHeaderSignInLink method_HP");
}       

//"********Validation of GetSavingNow Button********");
@Test (priority = 1, groups = {"Smoke"})
public  void validateGetSavingNowButton_HP() throws Exception{
    System.out.println("In validateGetSavingNowButton Method_HP");
    elementName = "GETSAVINGSNOWButton";        
    Comment = "validateGetSavingNowButton"; 
    expectedResult = "http://automationpractice.com/index.php";
    actualResult = "http://automationpractice.com/index.php";
    Assert.assertEquals(actualResult, expectedResult);      
    System.out.println("Out  of validateGetSavingNowButton method_HP");
}   

@Test (priority = 2, groups = {"UITest"})
//"********Validation of SearchBox********");
public  void validateSearchField_HP() throws Exception{
    System.out.println("In validateSearchField Method_HP");     
    elementName = "FadedShortSleeveTshirts_lnktxt";     
    Comment = "validateSearchField";        
    actualResult = "Faded Short Sleeve T-shirtss";  //Just to produce a failed result
    expectedResult = "Faded Short Sleeve T-shirts";
    Assert.assertEquals(actualResult, expectedResult);
    System.out.println("Out  of validateSearchField method_HP");
}


 @Test (priority = 3, enabled = true, groups = {"Smoke", "UITest"})
//"********Validation of Slider1********");  
public  void validateHomepageSlider1_HP() throws Exception{
    System.out.println("In validateHomepageSlider1 Method_HP"); 
    elementName = "Homepage Slider1";
    Comment = "validateHomepageSlider1";
    actualResult =  "https://www.prestashop.com/en?utm_source=v16_homeslider";
    expectedResult = "https://www.prestashop.com/en?utm_source=v16_homeslider";
    Assert.assertEquals(actualResult, expectedResult);  
    System.out.println("Out  of validateHomepageSlider1 method_HP");

    }   

  }

5.SignIntest Class-Testpage
封装sampletestproject;

import java.lang.reflect.Method;

import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import 
com.relevantcodes.extentreports.ExtentTest;
import 
com.relevantcodes.extentreports.LogStatus;

public class SignInnTest extends TestBasee{



SignInPagee lognObj;    
ExtentTest logger;
String elementName;
String Comment;
String expectedResult;
String actualResult;

@BeforeMethod(alwaysRun = true)
@Parameters({ "Browser", "URL"})
public void getBrowser(Method method, String Browser, String URL) throws Exception{
    logger = getReport().startTest((this.getClass().getSimpleName()+"::"+method.getName()), method.getName());
    logger.assignAuthor("VK");
    logger.assignCategory("SignInpage - Smoketesting and TextTesting--Live");
    homeObj = gm_OpenApp(Browser, URL);             
    lognObj = homeObj.navigateToSignInPage();           
}

 @AfterMethod (alwaysRun = true)
    public void publishReport_SIP(ITestResult result) throws Exception{
        System.out.println("publishReport_SIP");
        String resultStatus = null;         
        if(result.getStatus() == ITestResult.FAILURE){              
            resultStatus = "FAILED"; 
            String screenshot_Path = gm_setScreenshotPath_forExtentReporting(elementName, resultStatus);            
            gm_TakeSnapshot(screenshot_Path);               
            String image = logger.addScreenCapture(screenshot_Path);
            logger.log(LogStatus.FAIL, Comment, image);
        }else{              
            resultStatus = "PASSED";                
            String screenshot_Path = gm_setScreenshotPath_forExtentReporting(elementName, resultStatus);
            gm_TakeSnapshot(screenshot_Path);
            System.out.println("screenshot_Path: "+screenshot_Path);
            String image = logger.addScreenCapture(screenshot_Path);
            logger.log(LogStatus.PASS, Comment, image);
        }
        getReport().endTest(logger);
        getReport().flush();
        System.out.println("closing now_SIP.");
        driverObj.quit();
    }        
@Test (priority = 0, groups = {"Smoke""})
public  void validateSignInPage_PageHeading_SIP() throws Exception{
    System.out.println("In validateSignInPage_PageHeading Method_SIP");                 
    elementName = "SignIn_PageHeading_txt";     
    Comment = "validatePageHeading_SignInpage";     
    actualResult =  "AUTHENTICATION";           
    expectedResult = "AUTHENTICATION";
    Assert.assertEquals(actualResult, expectedResult);  //Here test will pass
    System.out.println("Out  of validateSignInPageHeading method_SIP");
}   

@Test (groups = {"UITest"}, dependsOnMethods = { "validateSignInPage_PageHeading_SIP" })
public  void validateSignInPage_SignInSectionHeading_SIP() throws Exception{
    System.out.println("In validateSignInPage_SignInSectionHeading Method_SIP");        
    elementName = "SignInPage_SignInSectionHeading_txt";        
    Comment = "validateSectionHeading_SignInpage";      
    actualResult =  "ALREADY REGISTERED1?"; 
    expectedResult = "ALREADY REGISTERED?";
    Assert.assertEquals(actualResult, expectedResult); //Here Test will fail as actual not equal to expected
    System.out.println("Out  of validateSignInPage_SignInSectionHeading method_SIP");
     }  
 }

6.testng.xml 套件名称=“测试”并行=“测试”线程数=“1”>

<test name="CHTest"  >   
    <parameter name="Browser" value="CH" ></parameter>
    <parameter name="URL" value="http://automationpractice.com/index.php"></parameter>          
      <groups><run>         
            <include name="Smoke"/>
            <include name="UITest"/>                 
        </run></groups>             
    <classes>
        <class name= "sampletestproject.SignInnTest" /> 
        <class name= "sampletestproject.HomepageeTest"/>
    </classes></test></suite>
4

2 回答 2

3

我会说最好在此处使用 threadlocal 或类似于用于管理测试的实现以及用于 ExtentReports 的类似实现:

https://github.com/anshooarora/extentreports-java/blob/master/src/test/java/com/aventstack/extentreports/common/ExtentTestManager.java

此外,如果您看到文档的示例部分,您已经有了一个准系统 ExtentTestNGReportBuilder 示例,您可以使用它而无需像这里创建任何自定义代码。

您面临的问题是由于 Java 的原则和管理实例而不是 ExtentReports。如果您想要所有类的单一报告,请确保始终只有 1 个实例用于整个运行。防止任何重新创建在您的情况下发生的实例的行为 - 每个类都会重置报告变量,从而重置 ExtentReports 实例。

此外,我建议在这种情况下使用 ITestListener,下面显示了一个示例,以便将报告与您的测试代码分开:

public class ExtentITestListener
implements ITestListener {

private static final ExtentReports EXTENT = Extent.getInstance();

private static ThreadLocal<ExtentTest> methodTest = new ThreadLocal<ExtentTest>();
private static ThreadLocal<ExtentTest> dataProviderTest = new ThreadLocal<>();

@Override
public synchronized void onStart(ITestContext context) { }

@Override
public synchronized void onFinish(ITestContext context) {
    EXTENT.flush();
}

@Override
public synchronized void onTestStart(ITestResult result) {      
    String methodName = result.getMethod().getMethodName();
    if (result.getParameters().length>0) {
        if (methodTest.get() != null && methodTest.get().getModel().getName().equals(methodName)) { } 
        else {
            createTest(result);
        }
        String paramName = Arrays.asList(result.getParameters()).toString();
        ExtentTest paramTest = methodTest.get().createNode(paramName);
        dataProviderTest.set(paramTest);
    } else {
        createTest(result);
    }
}

private void createTest(ITestResult result) {
    String methodName = result.getMethod().getMethodName();
    ExtentTest test = EXTENT.createTest(methodName);
    methodTest.set(test);

    String[] groups = result.getMethod().getGroups();
    if (groups.length > 0) {
        Arrays.asList(groups)
            .forEach(x -> methodTest.get().assignCategory(x));
    }
}

@Override
public synchronized void onTestSuccess(ITestResult result) {
    getTest(result).pass("Test passed");
}

private ExtentTest getTest(ITestResult result) {
    ExtentTest t = result.getParameters() != null && result.getParameters().length>0
            ? dataProviderTest.get()
            : methodTest.get();
    return t;
}

@Override
public synchronized void onTestFailure(ITestResult result) {
    getTest(result).fail(result.getThrowable());
}

@Override
public synchronized void onTestSkipped(ITestResult result) {
    getTest(result).skip(result.getThrowable());
}

@Override
public synchronized void onTestFailedButWithinSuccessPercentage(ITestResult result) { }

}
于 2018-09-09T19:49:26.347 回答
1

从 testNG.xml 文件执行 2 个类时,我也遇到了同样的问题。我发现 ExtentReport 变量在运行 @AfterMethod 注释后变为空,这就是为什么它在从另一个类运行测试用例时返回空指针异常的原因。

对我有用的解决方案是将 ExtentReport 变量设为静态,以便创建范围报告变量的单个副本,并且在运行多个类时不会出现该错误。

例如:静态 ExtentReports 报告;

让我知道问题是否仍然存在。

于 2019-10-10T05:11:55.027 回答