November 24, 2019

How to write logs to extent report with screenshot as base64 image

  November 24, 2019
It is always good to have a single method for writing logs to the reports. We can refer to the same method anywhere in the tests or in page object methods to write different logs like Info, Pass, Fail, Warning, etc.



When we are using any reporting tool, it is a concern that where we can keep the screenshots.
If we manage to keep the screenshot and the HTML report in the same folder, we have to use the relative path of the screenshot image in the HTML file.

If that is acceptable, the main issue comes when we need to share this report through Email or in any other tool as an attachment. The relative path of the image doesn't work there and we are stuck with the broken image in the HTML file.

This is where the importance of using the Base64 image comes in place. When using base64 image conversion, we are converting the image to a string and this string is used in the HTML file.
So this solves the HTML file and image connection issue.



Now we can attach the single HTML file in any tool or with email and works like a charm!
And as you are now thinking the HTML file will be a bit heavy, and that is a perk we have to live with.

The below code can be used as a single point logger, where it takes a screenshot on Pass, Fail, and Error scenarios.

Screenshot only on necessary cases will optimize the size of the HTML report.

        /// <summary>
        /// Method to write logs in ExtentReports
        /// </summary>
        /// <param name="status">info,pass,fail,fatal,error,warning,skip</param>
        /// <param name="msg">Message to be displayed</param>
        public void WriteLog(string status, string msg)
        {
            string[] strUserName = (System.Security.Principal.WindowsIdentity.GetCurrent().Name).Split('\\');

            status = status.ToLower();

            if (status.Equals("info"))
            {
                exTest.Log(Status.Info, msg);
                System.Diagnostics.Trace.WriteLine("INFO >>>>  " + msg);
            }
            else if (status.Equals("pass"))
            {
                Screenshot file = ((ITakesScreenshot)driver).GetScreenshot();
                string image = file.AsBase64EncodedString;

                exTest.Pass(msg, MediaEntityBuilder.CreateScreenCaptureFromBase64String(image).Build());
                System.Diagnostics.Trace.WriteLine("PASS >>>>  " + msg);
            }
            else if (status.Equals("fail"))
            {

                Screenshot file = ((ITakesScreenshot)driver).GetScreenshot();
                string image = file.AsBase64EncodedString;

                exTest.Fail(msg, MediaEntityBuilder.CreateScreenCaptureFromBase64String(image).Build());
                boolFailFlag = true;
                strFailMsg = strFailMsg + msg;
                System.Diagnostics.Trace.WriteLine("FAIL >>>>  " + msg);
            }
            else if (status.Equals("error"))
            {
                exTest.Log(Status.Error, msg);
                boolFailFlag = true;
                System.Diagnostics.Trace.WriteLine("ERROR >>>>  " + msg);
            }
            else if (status.Equals("warning"))
            {
                Screenshot file = ((ITakesScreenshot)driver).GetScreenshot();
                string image = file.AsBase64EncodedString;

                exTest.Warning(msg, MediaEntityBuilder.CreateScreenCaptureFromBase64String(image).Build());
                System.Diagnostics.Trace.WriteLine("WARNING >>>>  " + msg);
            }
            else if (status.Equals("skip"))
            {
                exTest.Log(Status.Skip, msg);
                System.Diagnostics.Trace.WriteLine("SKIP >>>>  " + msg);

            }
        }
logoblog

Thanks for reading How to write logs to extent report with screenshot as base64 image

Previous
« Prev Post

No comments:

Post a Comment

Bookmark this website for more workarounds and issue fixes.

Verify Zip file contents without extracting using C# + Selenium

While doing automation testing, we may get a scenario where we need to download a zip file and to validate the contents inside it. One way t...