gpt4 book ai didi

java - 如何为 Selenium WebDriver 实现高级 ExtentReports 逻辑

转载 作者:太空宇宙 更新时间:2023-11-04 09:28:21 25 4
gpt4 key购买 nike

我正在寻找一种方法来实现高级范围报告,其中包含基本设置之外的测试步骤和结果数据。

我目前有一个可用的 ExtentReports 报告,并且正在寻找一种使用 ExtentReports (Logger) 类来实现和显示测试步骤的方法。我已尝试向我的类添加 (Logger.log) 语句,但无法查看我的 ExtentReports 文件中的测试步骤。

public static WebDriver driver;
public static ExtentReports extentReports;
public static ExtentTest extentTest;
public static ExtentHtmlReporter htmlReporter;
public static ExtentTest test;
public static ExtentTest logger;

@BeforeSuite
public void beforeSuite(){
htmlReporter = new ExtentHtmlReporter(new File(("reports/Test Automation Report.html")));
htmlReporter.loadXMLConfig(new File("extent-config.xml"));
extentReports=new ExtentReports();
extentReports.attachReporter(htmlReporter);
extentReports.setSystemInfo("Environment", "QA");
htmlReporter.config().setChartVisibilityOnOpen(true);
htmlReporter.config().setDocumentTitle("AutomationTesting.in Demo Report");
htmlReporter.config().setReportName("My Own Report");
htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
htmlReporter.config().setTheme(Theme.DARK);
}

@BeforeMethod
public void before(Method method) {
extentTest=extentReports.createTest(method.getName().toString());
driver = DriverType.chromeDriver();
driver.manage().timeouts().setScriptTimeout(10,TimeUnit.SECONDS);
IRISBase base = new IRISBase(driver);
base.fmaLoad();
}

@AfterMethod
public void getResult(ITestResult result) throws Exception {
if (driver!=null){
System.out.println("deleteAllCookies");
driver.manage().deleteAllCookies();
}

if (result.getStatus() == ITestResult.FAILURE)
{
System.out.println(">>>Test Failed"); //Which Test Failed and Why (Method Name Output)
extentTest.log(Status.FAIL, MarkupHelper.createLabel(result.getName() + " Test case FAILED due to below issues:", ExtentColor.RED));
extentTest.fail(result.getThrowable());
System.out.println("Automation Test Run: " + result.getMethod().toString()); //Testing and Results (Relevant)
}
else if (result.getStatus() == ITestResult.SUCCESS)
{
System.out.println(">>>Test Passed");
extentTest.log(Status.PASS, MarkupHelper.createLabel(result.getName() + " Test Case PASSED", ExtentColor.GREEN));
logger.info(MarkupHelper.createLabel(result.getName() + " Test Case PASSED", ExtentColor.GREEN));
System.out.println("=====================================================================================================");
System.out.println("Automation Test Run: " + result.getMethod().toString()); //Testing and Results
System.out.println("=====================================================================================================");
}
else if (result.getStatus() == ITestResult.SKIP)
{
System.out.println(">>>Test Skipped");
extentTest.log(Status.SKIP, MarkupHelper.createLabel(result.getName() + " Test Case SKIPPED", ExtentColor.BLUE));
System.out.println("Automation Test Run: " + result.getMethod().toString()); //Testing and Results
}

if(driver!=null) {
driver.manage().deleteAllCookies();
}
}

@AfterTest
public void testComplete() {
extentReports.flush();
driver.manage().deleteAllCookies();
driver.quit();
}

private static File fileWithDirectoryAssurance(String directory, String filename) {

Date date = new Date() ;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss") ;

File dir = new File(directory);
if (!dir.exists()) dir.mkdirs();
return new File(directory + "/" + filename);
}
}

预期范围报告文件 Expected ExtentReports file

实际范围报告文件 Actual ExtentReports file

最佳答案

使用 ExtentTest 对象进行日志记录示例:
注意:更改你的@aftermethod

    public static ExtentReports extent;
public static ExtentTest logger;
@BeforeSuite(alwaysRun = true)
public void config(){
extent = new ExtentReports(path, true);
extent.addSystemInfo("Host Name", System.getProperty("user.name"));
extent.addSystemInfo("Environment", "API Testing");
extent.loadConfig(new File(XMLPath));}

@AfterMethod(alwaysRun = true)
public void getResult(ITestResult result) {
if (result.getStatus() == ITestResult.SUCCESS) {
logger.log(LogStatus.PASS, "Test case is passed " + result.getName());

} else if (result.getStatus() == ITestResult.FAILURE) {
logger.log(LogStatus.FAIL, "Test case is failed " + result.getName());
logger.log(LogStatus.FAIL, "Test case is failed " + result.getThrowable());
} else if (result.getStatus() == ITestResult.SKIP) {
logger.log(LogStatus.SKIP, "Test case is Skiped " + result.getName());
}
extent.endTest(logger);
}

@AfterSuite(alwaysRun = true)
public void endReport() {
extent.flush();
// SendEmail.Email(ReportPath);
extent.close();
log.setinstanceNull();
}

使用 ExtentTest 对象记录测试用例中的测试步骤导入基类并使用 ExtentTest 对象,如下所示示例测试用例

logger.log(LogStatus.INFO, message);
logger.log(LogStatus.ERROR, message);

要在开始测试用例之前获取测试用例列表,请使用extent.startTest方法,其中extent是ExtentReports的对象

@Test(groups = { "sanityuserfolw" })
public void testcase() {

logger = extent.startTest("TestCase Name", "test case summary");



resp = part_study.sampletest(input);
logger.log(LogStatus.INFO, "response is "+resp.getStatusCode());
assertEquals(resp.getStatusCode(), 200, "Error in adding study " + resp.statusLine());

}

关于java - 如何为 Selenium WebDriver 实现高级 ExtentReports 逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57422070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com