Closed seema-realforce closed 7 years ago
@seema-realforce you can look for the implementation done for tests in parallel, where they presented a way to store loggers in a map that has the thread id as key, and the logger as value. So you can get the logger that has been used in the previous steps(same thread id).
Well, i looked at example for parallel methods under http://extentreports.relevantcodes.com/java/#parallel-methods, but with ExtentTestManager class, i am seeing error 'Type mismatch: cannot convert from Object to ExtentTest' for return extentTestMap.get((int) (long) (Thread.currentThread().getId()));
and error 'The method endTest(ExtentTest) in the type ExtentReports is not applicable for the arguments (Object)' for extent.endTest(extentTestMap.get((int) (long) (Thread.currentThread().getId())));
public class ExtentTestManager {
static Map extentTestMap = new HashMap();
private static ExtentReports extent = ExtentManager.getReporter();
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;
}
I dont think i need to type cast here as this could be proven example and not sure what i am doing wrong in above code.
Also when you say that i can use the logger, how can i pass it to my pagefactory classes?
whenever you call
ExtentTestManager.getTest()
you will get the logger it is a public static method. And you still need to start and stop the ExtentTest objects that will be in that map. So logger will start in a @BeforeMethod adding an ExtentTest value to the map, and in every page object you use while being in the same thread, the get method will find the extent test created. Once done you need to flush that object in an @AfterMethod and so on. By the way I am talking about versions 2.x maybe this won't work for the newest versions of the tool.
I can implement that, but do you know the solution for the error i had mentioned above for ExtentTestManager class, i am seeing error 'Type mismatch: cannot convert from Object to ExtentTest' for return extentTestMap.get((int) (long) (Thread.currentThread().getId()));
and error 'The method endTest(ExtentTest) in the type ExtentReports is not applicable for the arguments (Object)' for extent.endTest(extentTestMap.get((int) (long) (Thread.currentThread().getId())));
Change this line : static Map extentTestMap = new HashMap(); like : static Map<Integer, ExtentTest> extentTestMap = new HashMap<Integer, ExtentTest>();
Its working now with above implementation. Appreciate your effort.
I have one more question regarding logging. Is there a way where i can pass the function name under which the log statement is placed? Also i dont see the stepname being logged using the syntax ExtentTestManager.getTest().log(logStatus, stepName, details);
I get only 3 columns, status, timestamp and details.
Also how does this work when parallel tests are run? test = ExtentTestManager.startTest(Thread.currentThread().getStackTrace()[1].getMethodName()); test.log(LogStatus.INFO, "Log from threadId: " + Thread.currentThread().getId()); Will show me thread 2 under logs for parallel method?
Create one Testnglistener class where u can call these static methods(startTest,getTest,endTest) in the respective overridden listener methods.
public class Listeners extends TestListenerAdapter { WebDriver driver;
@Override
public void onTestStart(ITestResult result) {
ExtentTestManager.startTest(result.getName());
ExtentManager.getReporter().loadConfig(new File(System.getProperty("user.dir") + File.separator + "src"
+ File.separator + "ExtentReportConfig.xml"));
}
@Override
public void onTestSuccess(ITestResult result) {
ExtentTestManager.getTest().log(LogStatus.PASS, "Test " + result.getName() + " passed");
}
@Override
public void onTestFailure(ITestResult result) {
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String directoryPath = System.getProperty("user.dir") + File.separator;
String imageName = tr.getName() + "failureScreenShot_at_" + DateUtils.getTimestampforImage() + ".png";
String imagePath = "ScreenShots"
+ File.separator + imageName;
FileUtils.copyFile(scrFile, new File(directoryPath + imagePath));
// set failure screenshot to Extent reports
ExtentTestManager.getTest().log(LogStatus.FAIL, "Test " + result.getName() + " Failed");
ExtentTestManager.getTest().log(LogStatus.FAIL, result.getThrowable());
ExtentTestManager.getTest().log(LogStatus.INFO,ExtentTestManager.getTest().addScreenCapture("..\\ScreenShots\\" + imageName));
}
@Override
public void onTestSkipped(ITestResult result) {
ExtentTestManager.getTest().log(LogStatus.SKIP, "Test " + result.getName() + " skipped");
}
@Override
public void onFinish(ITestContext context) {
ExtentManager.getReporter().endTest(ExtentTestManager.getTest());
ExtentManager.getReporter().flush();
}
}
Add this Listener class to Ur Base class as @Listener or add this to TestNG xml file using listener tag
I will be using this as listener, but can you provide me getTimestampforImage() function under your dateutils. Also "tr" refered in String imageName = tr.getName() + "failureScreenShotat" + DateUtils.getTimestampforImage() + ".png"; is result right? If i use the above listener class, how can i log the info for all my test methods using ExtentTestManager.getTest().log(LogStatus.INFO, "Welcome Page : Validating the welcome page "); as this hold null value
@seema-realforce give me ur mail id.
@seema-realforce : Hope you were able to go ahead with the help provided by other members.
Kindly re-open in case you have any issues with the Extent Report's implementation.
I have a pagefactory framework to handle test and webpages. Currently i have extent report configured for tests and all the loggin is in @test methods. I want to pass this instance to wepages class and log the steps. How can this be done?
public class maintest(){ ExtentReports extent; ExtentTest test; @BeforeTest public void startreport(){ //extent=new ExtentReports (System.getProperty("user.dir")+"/target/surefire-reports/Extentreport.html"); extent= utils.ExtentManager.instance(); } @Test(groups = "login")
}
@AfterMethod public void teardown(ITestResult result) throws IOException{ if(result.getStatus()==ITestResult.FAILURE){
} @AfterTest public void endreport(){
}
Now how can i use extent report test instance inside the login class to log reporting?