我正在调用我的 extentReportManager 对象来记录测试步骤。当我在没有并行的情况下运行测试时,它可以很好地执行测试。当我单独并行运行时,它为 ExtentReportManager 对象行抛出空指针异常。
<块引用>ExtentReportManager.java
public class ExtentReportManager {
private static ThreadLocal<ExtentTest> extTest = new ThreadLocal<>();
static void setExtent(ExtentTest test){
if(Objects.nonNull(test)) {
extTest.set(test);
}
}
public static synchronized ExtentTest getTest(){
return extTest.get();
}
static void unload(){
extTest.remove();
}
}
<块引用>登录页面.java
public class LoginPage extends Base {
private WebDriver driver;
ElementUtils elementUtils;
public LoginPage(WebDriver driver){
this.driver = driver;
elementUtils = new ElementUtils(driver);
}
private final By userEmail = By.cssSelector("input[name='email']");
private final By userPassword = By.cssSelector("input[name='password']");
private final By sign_InButton = By.xpath("//div[@class='login_container']//button[@type='submit']");
private final By pageHeader= By.xpath("//div[contains(text(),'Use your email')]");
private final By errorMessage = By.xpath("//div[@class='Toastify__toast-body']");
private final By googleSignIn = By.xpath("//div[text()='Sign in with Google']");
private final By esopManagementTab = By.xpath("//button[text()='ESOP Management']");
public WebElement getPageHeader() {
return elementUtils.getElement(pageHeader);
}
//get loginPage title
public String getLoginPageTitle(){
return elementUtils.waitForTitleToBePresent(AppConstants.LOGINPAGE_TITLE,10);
}
//Setting userEmail
public void setUserEmail(String Email){
elementUtils.doSendKeys(userEmail,Email,"Email Address");
ExtentReportManager.getTest().info("Entered Email as " + Email);
}
<块引用>
ExtentReporter.java
public class ExtentReporter extends Base {
static LocalDateTime now = LocalDateTime.now();
static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy MMM dd HH-mm-ss");
static String time = dtf.format(now);
private static ExtentReports extent;
private static final String reportFileName = "MSE-Test-Automation-Report"+time+".html";
private static final String reportDirectory = System.getProperty("user.dir")+ File.separator + "Build-Reports";
private static final String reportFilePath = reportDirectory+ File.separator + reportFileName;
//Create an extent report instance
public static void initReports(){
String filePath = getReportFileLocation();
ExtentSparkReporter sparkReporter = new ExtentSparkReporter(filePath);
sparkReporter.config().setTheme(Theme.STANDARD);
sparkReporter.config().setDocumentTitle("My StartUp Equity Automation Result");
sparkReporter.config().setEncoding("utf-8");
sparkReporter.config().setReportName("My StartUp Equity Automation Result");
extent = new ExtentReports();
extent.setSystemInfo("OS",System.getProperty("os.name"));
extent.setSystemInfo("Host Name",System.getProperty("user.name"));
extent.setSystemInfo("Java Version",System.getProperty("java.version"));
extent.attachReporter(sparkReporter);
}
public static void createTest(String testName,String description){
ExtentTest test = extent.createTest(testName, description);
ExtentReportManager.setExtent(test);
}
<块引用>
监听器.java
@Override
public synchronized void onTestStart(ITestResult result) {
String methodName = result.getMethod().getMethodName();
String qualifiedName = result.getMethod().getQualifiedName();
int last = qualifiedName.lastIndexOf(".");
int mid = qualifiedName.substring(0, last).lastIndexOf(".");
String className = qualifiedName.substring(mid + 1, last);
System.out.println(methodName + " started!");
ExtentReporter.createTest((result.getMethod().getMethodName()),result.getMethod().getDescription());
ExtentReportManager.getTest().assignCategory(result.getTestContext().getSuite().getName());
ExtentReportManager.getTest().assignCategory(className); test.set(ExtentReportManager.getTest());
ExtentReportManager.getTest().getModel().setStartTime(getTime(result.getStartMillis()));
}