Selenium 使用自定义日志创建扩展数据块报告
需要在数据块报告中打印log4j日志。我该怎么做呢?尝试结束测试并在@AfterMethod中刷新报告,然后在@AfterMethod中关闭报告。这对我有用。请按以下代码进行尝试:Selenium 使用自定义日志创建扩展数据块报告,selenium,extentreports,Selenium,Extentreports,需要在数据块报告中打印log4j日志。我该怎么做呢?尝试结束测试并在@AfterMethod中刷新报告,然后在@AfterMethod中关闭报告。这对我有用。请按以下代码进行尝试: @AfterMethod(alwaysRun=true) public void TearDown_AM(ITestResult result) throws IOException { System.out.println("@After Method"); try {
@AfterMethod(alwaysRun=true)
public void TearDown_AM(ITestResult result) throws IOException
{
System.out.println("@After Method");
try
{
if(result.getStatus()==ITestResult.FAILURE)
{
String res = captureScreenshot(Driver, result.getName());
String image= logger.addScreenCapture(res);
System.out.println(image);
String TestCaseName = this.getClass().getSimpleName() + " Test Case Failure and Title/Boolean Value Failed";
logger.log(LogStatus.FAIL, TestCaseName + logger.addScreenCapture(res));
// logger.log(LogStatus.FAIL, image, this.getClass().getSimpleName() + " Test Case Failure and Title/Boolean Value Failed");
}
else if(result.getStatus()==ITestResult.SUCCESS)
{
logger.log(LogStatus.PASS, this.getClass().getSimpleName() + " Test Case Success and Title Verified");
}
else if(result.getStatus()==ITestResult.SKIP)
{
logger.log(LogStatus.SKIP, this.getClass().getSimpleName() + " Test Case Skipped");
}
report.endTest(logger);
report.flush();
}
catch(Throwable t)
{
logger.log(LogStatus.ERROR,t.fillInStackTrace());
}
}
@AfterTest(alwaysRun=true)
public void AfterTest()
{
System.out.println("@After Test");
Driver.close();
report.close();
}
尝试结束测试并在@AfterMethod中刷新报告,并在@AfterTest方法中关闭报告。这对我有用。请按以下代码进行尝试:
@AfterMethod(alwaysRun=true)
public void TearDown_AM(ITestResult result) throws IOException
{
System.out.println("@After Method");
try
{
if(result.getStatus()==ITestResult.FAILURE)
{
String res = captureScreenshot(Driver, result.getName());
String image= logger.addScreenCapture(res);
System.out.println(image);
String TestCaseName = this.getClass().getSimpleName() + " Test Case Failure and Title/Boolean Value Failed";
logger.log(LogStatus.FAIL, TestCaseName + logger.addScreenCapture(res));
// logger.log(LogStatus.FAIL, image, this.getClass().getSimpleName() + " Test Case Failure and Title/Boolean Value Failed");
}
else if(result.getStatus()==ITestResult.SUCCESS)
{
logger.log(LogStatus.PASS, this.getClass().getSimpleName() + " Test Case Success and Title Verified");
}
else if(result.getStatus()==ITestResult.SKIP)
{
logger.log(LogStatus.SKIP, this.getClass().getSimpleName() + " Test Case Skipped");
}
report.endTest(logger);
report.flush();
}
catch(Throwable t)
{
logger.log(LogStatus.ERROR,t.fillInStackTrace());
}
}
@AfterTest(alwaysRun=true)
public void AfterTest()
{
System.out.println("@After Test");
Driver.close();
report.close();
}
首先,使用同步方法创建一个类,该方法返回
ExtentReports
的实例:
public class ExtentManager {
private static ExtentReports report;
public static synchronized ExtentReports getInstance() {
if (report == null) {
report = new ExtentReports("MyReport.html");
}
return report;
}
}
其次,创建另一个类,该类只声明测试相关的同步方法(当然,这些方法必须以线程方式处理)。代码段:
public class ExtentTestManager {
static Map<Integer, ExtentTest> extentTestMap = new HashMap<Integer, ExtentTest>();
private static ExtentReports extent = ExtentManager.getInstance();
public static synchronized ExtentTest getTest() {
return extentTestMap.get((int) (long) (Thread.currentThread().getId()));
}
public static synchronized void endTest() {
extent.endTest(extentTestMap.get((int) (long) (Thread.currentThread().getId())));
}
public static synchronized ExtentTest startTest(String testName) {
return startTest(testName, "");
}
public static synchronized ExtentTest startTest(String testName, String desc) {
ExtentTest test = extent.startTest(testName, desc);
extentTestMap.put((int) (long) (Thread.currentThread().getId()), test);
return test;
}
}
编辑:
使用测试创建测试类(ExampleTest.java
):
public class ExampleTest extends BaseClass{
@Test
public void test_01(){
Assert.assertTrue(false);
}
@Test
public void test_02(){
Assert.assertTrue(false);
}
@Test
public void test_03(){
Assert.assertTrue(true);
}
@Test
public void test_04(){
Assert.assertTrue(false);
}
}
必需的testng.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="DemoSuite1" parallel="methods" thread-count="2">
<test name = "Test">
<classes>
<class name = "com.extent.demo.ExampleTest" />
</classes>
</test>
</suite>
首先,使用一个同步方法创建一个类,该方法返回一个
扩展端口的实例
:
public class ExtentManager {
private static ExtentReports report;
public static synchronized ExtentReports getInstance() {
if (report == null) {
report = new ExtentReports("MyReport.html");
}
return report;
}
}
其次,创建另一个类,该类只声明测试相关的同步方法(当然,这些方法必须以线程方式处理)。代码段:
public class ExtentTestManager {
static Map<Integer, ExtentTest> extentTestMap = new HashMap<Integer, ExtentTest>();
private static ExtentReports extent = ExtentManager.getInstance();
public static synchronized ExtentTest getTest() {
return extentTestMap.get((int) (long) (Thread.currentThread().getId()));
}
public static synchronized void endTest() {
extent.endTest(extentTestMap.get((int) (long) (Thread.currentThread().getId())));
}
public static synchronized ExtentTest startTest(String testName) {
return startTest(testName, "");
}
public static synchronized ExtentTest startTest(String testName, String desc) {
ExtentTest test = extent.startTest(testName, desc);
extentTestMap.put((int) (long) (Thread.currentThread().getId()), test);
return test;
}
}
编辑:
使用测试创建测试类(ExampleTest.java
):
public class ExampleTest extends BaseClass{
@Test
public void test_01(){
Assert.assertTrue(false);
}
@Test
public void test_02(){
Assert.assertTrue(false);
}
@Test
public void test_03(){
Assert.assertTrue(true);
}
@Test
public void test_04(){
Assert.assertTrue(false);
}
}
必需的testng.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="DemoSuite1" parallel="methods" thread-count="2">
<test name = "Test">
<classes>
<class name = "com.extent.demo.ExampleTest" />
</classes>
</test>
</suite>
请发布您的代码。@Anshoo是否有更新?似乎您的测试被彼此覆盖,因此日志被共享。你能用这样的方法来管理你的测试吗:我们想要实现什么?这会有什么帮助?你能提供一些背景资料吗?它将如何使日志仅坚持其相应的测试?我们希望确保在任何给定的线程中,只有正确的测试用于记录信息。由于您没有共享整个代码,我假设extentTest正在被访问基类的多个线程和/或类覆盖。我们希望避免这种情况,只以线程安全的方式将信息发送到属于该线程的测试。请发布您的代码。@Anshoo是否有任何更新?似乎您的测试正在被彼此覆盖,因此日志正在共享。你能用这样的方法来管理你的测试吗:我们想要实现什么?这会有什么帮助?你能提供一些背景资料吗?它将如何使日志仅坚持其相应的测试?我们希望确保在任何给定的线程中,只有正确的测试用于记录信息。由于您没有共享整个代码,我假设extentTest正在被访问基类的多个线程和/或类覆盖。我们希望避免这种情况,只以线程安全的方式向属于该线程的测试发送信息。好的,测试用例将如何启动。实际上它给我的是空错误。请看编辑后的答案。希望,它可能会对您有所帮助。要重现此问题,您的testng.xml
中的
标记下应该有两个不同的
标记,并且线程计数应该为零。您也没有设置reporter,您的代码要求我至少启动一个reporter。如果您使用eclipse IDE并安装了TestNG插件,则可以轻松运行该套件,而无需进行任何修改。运行该套件没有问题,只是每当我在类标记和并行参数下有两个类作为方法时,然后将报告附加到最后一个测试。因此,它对我来说总是工作得很好。好的,测试用例是如何开始的。实际上它给我的是空错误。请看编辑后的答案。希望,它可能会对您有所帮助。要重现此问题,您的testng.xml
中的
标记下应该有两个不同的
标记,并且线程计数应该为零。您也没有设置reporter,您的代码要求我至少启动一个reporter。如果您使用eclipse IDE并安装了TestNG插件,则可以轻松运行该套件,而无需进行任何修改。运行该套件没有问题,只是每当我在类标记和并行参数下有两个类作为方法时,然后将报告附加到最后一个测试。这对我来说一直都很好。