TestNG Tutorial

Simple steps to Generate logs in Selenium using TestNG reporter

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

 

Generate logs in Selenium

Now if you want to print on console and html report as well then we have the same method with diff argument

  1. if boolean value set to true then values will come on console and HTML report as well.
  2. 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

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

20 thoughts on “Simple steps to Generate logs in Selenium using TestNG reporter

  1. 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.

    1. 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…:)

  2. Harsha P says:

    Hi mukesh,

    thanks for your great sessions. Which is better log4j or testng log?

    regards,
    Harsha

    1. 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

  3. Piyush says:

    How to format Reporter.log? Like I wants to print the message in red color

    1. Hi Piyush,

      Please check this link

  4. 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.

    1. 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 https://vistasadprojects.com/mukeshotwani-blogs-v3/read-and-write-excel-files-in-selenium/

  5. 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.

    1. Hi Gaurav,

      Give Reporter.log(“I am log info”,true) to log to appear on console.

  6. 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

    1. Hey Pratik,

      Your most welcome. I am glad to know it helped. Keep visiting and be in touch.

  7. 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(“” +”“);

  8. 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.

    1. Hey Swapna,

      Thanks a ton 🙂 for the nice feedback. Please let me know if any help required in Selenium from my side.

      Keep learning.

  9. suguna says:

    Awesome website. Your You tube videos are awesome. I like ur clarity. very neat.

    1. Thanks Suguna 🙂 Keep visiting and let me know if any help from my side.

  10. shubham says:

    thanks..I followed your log4j tutorial and able to get logs, but this is additional which i learnt here

    1. Hi shubham,

      Glad to hear that 🙂 Keep visiting

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.