Logging is most important part in any automation tools which make our script in more readable format.Today we will discuss one easy way to Generate logs in Selenium via TestNG Reporter class.
We have already seen some default reporting which is generated by TestNG and Advance XSLT reports as well.
As a good automation engineer, you should implement logging feature in your script, which helps you to debug your code easily if your script is failing. We have seen how to create log files in Selenium using log4j.
I want to share one more report which is best among all and very handy to use as well. Extent report generates logs and it allows us to attach the screenshot as well which make our report more attractive.
Reporter Class in TestNG for Generate logs in Selenium
The Reporter is a separate class in TestNG that is available under org.testng package.
In Selenium you can specify steps, which we are performing so that we can check our output, and in case any issue we can debug at which point our test cases failed.
Let me explain with the help of an example
Scenario 1- We have a script, which has almost 100 steps so if we do not use Reporter or any other reporting feature then we have to check where exactly we are getting the issue. We can use Reporter class here and we can check steps which have executed successfully and where our program has stopped.
Before starting below program make sure Eclipse Selenium and TestNG is installed
Generate logs in Selenium
Syntax-Reporter.log("String", boolean);
Complete program to check Generate logs in Selenium
package testngDemo; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Reporter; import org.testng.annotations.Test; public class ReporterDemo { @Test public void testReport(){ WebDriver driver=new FirefoxDriver(); Reporter.log("Browser Opened"); driver.manage().window().maximize(); Reporter.log("Browser Maximized"); driver.get("http://www.google.com"); Reporter.log("Application started"); driver.quit(); Reporter.log("Application closed"); } }
Once you run above program check TestReport
Now if you want to print on console and html report as well then we have the same method with diff argument
- if boolean value set to true then values will come on console and HTML report as well.
- if boolean value set to false then values will come on HTML report only.
Hope you liked above post. Keep sharing with friends.Comment below if you finding some issues.
Have a nice day 🙂
Like Facebook Page for more updates-Learn-Automation facebook page
Join Selenium Group for discussion and queries- Learn-Automation group
Muhammad Zeeshan Ismail says
Thanks for sharing your knowledge with us. My concept regarding Logs is now cleared and also got the difference between Syso and log.
Mukesh Otwani says
Hi Muhammad,
Glad that my blog post helped you to gain knowledge. You’re always welcome to ask your doubts and I’ll try my level best to answer them.
Thanks for your appreciation…:)
Harsha P says
Hi mukesh,
thanks for your great sessions. Which is better log4j or testng log?
regards,
Harsha
Mukesh Otwani says
Hi Harsha,
Both are equally good depends on your project requirements. TestNG logs can be written directly into TestNG report while Log4j, can be redirected to any external log file
Piyush says
How to format Reporter.log? Like I wants to print the message in red color
Mukesh Otwani says
Hi Piyush,
Please check this link
Aarati says
hello ,How do I take Repoter.log statement from excel as I have no.of test cases.
I will really Appreciate for help.
Mukesh Otwani says
Hi Aarati,
Since you are reading from excel, read data from particular excel cell and pass it as parameter to Reporter like Reporter.log(, true);
How to read from excel, kindly check this link http://learn-automation.com/read-and-write-excel-files-in-selenium/
Gaurav Khurana says
Wow this logging is so easy compared to log4j. Thanks for the easy explanation and it’s so easy to use.
Also just for everyone knowledge
Reporter.log(“I am log info”,false)
Reporter.log(“I am log info”)
The above two statements are equal and will not print on the console.
Mukesh Otwani says
Hi Gaurav,
Give Reporter.log(“I am log info”,true) to log to appear on console.
Pratik says
Hey Mukesh,
Thanks a lot for sharing all this knowledge and problems with possible solutions.
Your blog has helped me a lot to learn Selenium in a very basic to advance.
Highly appreciate…!!
Thanks
Mukesh Otwani says
Hey Pratik,
Your most welcome. I am glad to know it helped. Keep visiting and be in touch.
jayesh says
Hi Mukesh – Thanks a lot for keep helping us to understand automation- testing in detail.
I have a problem here too, can you help with this
I am trying to attach a screen print inside testng report. this is working fine in index.html report but in emailable report it just html markup the image is not rendering ,when I checked the emailable html file it logged as html escaps, any help on this.. TestNG version is 6.9
Reporter.log(“” +”“);
Mukesh Otwani says
Hi Jayesh,
Please give a try with extent report also
http://learn-automation.com/advance-selenium-reporting-with-screenshots-2/
Swapna says
Your way of explanation is very detailed and very easy to understand.Thank you so much for all your efforts. Usually I don’t write comments in any website but couldn’t resist myself by looking at your tutorial. Keep up good work.
Mukesh Otwani says
Hey Swapna,
Thanks a ton 🙂 for the nice feedback. Please let me know if any help required in Selenium from my side.
Keep learning.
suguna says
Awesome website. Your You tube videos are awesome. I like ur clarity. very neat.
Mukesh Otwani says
Thanks Suguna 🙂 Keep visiting and let me know if any help from my side.
shubham says
thanks..I followed your log4j tutorial and able to get logs, but this is additional which i learnt here
Mukesh Otwani says
Hi shubham,
Glad to hear that 🙂 Keep visiting