Showing posts with label Extert Report. Show all posts
Showing posts with label Extert Report. Show all posts

Thursday, February 1, 2018

Generate Extent Reports (Java) in Selenium WebDriver

In this article, we see one of the most popular and widely used Selenium Reporting Tools (Extent Reports).
What is Extent Reports?
Extent Report is an HTML reporting library for Selenium WebDriver for Java.
Features of Extent Report:
  • Allow is to generate logs inside HTML.
  • Generate PIE Chart based on test case status.
  • Generate Step summary.
  • We can filter reports depends on status.
  • It provides execution history.
  • It fetches system details like OS, Java Version, and Memory and so on.
  • Allow us the attached screenshot in the report that is a most important feature.

Pre-requisites to Generate Extent Reports:
1.    Java should be installed (Link to Install and setup Java )
2.    TestNG should be installed
3.    Extent Report Jars (Version 2.41.2)
4.    extent-config.xml – It allows to configure HTML Report

Steps To Generate Extent Reports:
1.    First, Create a TestNG project in eclipse
2.    Now download extent library files from the following link (http://extentreports.com/) and add the downloaded library files to your project.

Please refer the below sample code:

package com.Tests;
import java.io.IOException;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.BaseFixture.BaseFixture;
import com.CommonMethods.WebUtils;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;

public class HomePage extends BaseFixture
{            
              String Login_Button_Obj=null;String Enter_Username_Obj=null;String Enter_Password_Obj=null;String Click_Login_Obj=null;
              String Username_Data=null;String Password_Data=null;String Search_Box_Obj=null;String Search_Text_Data=null;
              String Click_POTUS_Obj=null;String Follow_Link_Obj=null;String Click_Follow_Obj=null;
             
              ExtentReports reports;
              ExtentTest logger;
             
              @BeforeSuite
              public void beforeSuite()
              {
                             reports=new ExtentReports("D:\\Interview\\balaji-interview-framework\\AdvanceTestRun.html",true);                                                    
                             reports.addSystemInfo("Application URL","https://twitter.com/");
                             reports.addSystemInfo("Environment", "Live Environment");
                             reports.addSystemInfo("Author","Balaji G");                   
              }
             
              @BeforeTest
              public void launchApplication() throws IOException
              {                                                                                                                                                                                         
                            Login_Button_Obj=WebUtils.readDataFromPropertiesFile("D:/Interview/balaji-interview-framework/src/test/resources/com/ObjectRepository/HomePage/HomePageObject.properties","Login_Button");                                                         
                            Enter_Username_Obj=WebUtils.readDataFromPropertiesFile("D:/Interview/balaji-interview-framework/src/test/resources/com/ObjectRepository/HomePage/HomePageObject.properties","Enter_Username");
                            Enter_Password_Obj=WebUtils.readDataFromPropertiesFile("D:/Interview/balaji-interview-framework/src/test/resources/com/ObjectRepository/HomePage/HomePageObject.properties","Enter_Password");
                            Click_Login_Obj=WebUtils.readDataFromPropertiesFile("D:/Interview/balaji-interview-framework/src/test/resources/com/ObjectRepository/HomePage/HomePageObject.properties","Click_Login");
                            Search_Box_Obj=WebUtils.readDataFromPropertiesFile("D:/Interview/balaji-interview-framework/src/test/resources/com/ObjectRepository/HomePage/HomePageObject.properties","Search_Box");
                            Click_POTUS_Obj=WebUtils.readDataFromPropertiesFile("D:/Interview/balaji-interview-framework/src/test/resources/com/ObjectRepository/HomePage/HomePageObject.properties","Click_POTUS");
                            Follow_Link_Obj=WebUtils.readDataFromPropertiesFile("D:/Interview/balaji-interview-framework/src/test/resources/com/ObjectRepository/HomePage/HomePageObject.properties","Follow_Link");
                            Click_Follow_Obj=WebUtils.readDataFromPropertiesFile("D:/Interview/balaji-interview-framework/src/test/resources/com/ObjectRepository/HomePage/HomePageObject.properties","Click_Follow");
                            
                            Username_Data=WebUtils.readDataFromPropertiesFile("D:/Interview/balaji-interview-framework/src/test/resources/com/DataRepository/HomePage/HomePageData.properties","Username");
                            Password_Data=WebUtils.readDataFromPropertiesFile("D:/Interview/balaji-interview-framework/src/test/resources/com/DataRepository/HomePage/HomePageData.properties","Password");
                            Search_Text_Data=WebUtils.readDataFromPropertiesFile("D:/Interview/balaji-interview-framework/src/test/resources/com/DataRepository/HomePage/HomePageData.properties","Search_Text");
              }
             
              @Test(priority=0)
              @Parameters({"browser"})
              public void verifyHomePage(String bro)
              {            
                             logger=reports.startTest("Verify Home Page - Twitter");
                             boolean isElementFound=WebUtils.IsElementDisplayed(getDriver(bro),Login_Button_Obj);
                  if(isElementFound)
                  {                                                        
                            System.out.println("Twitter Home page is loaded successfully");
                            logger.log(LogStatus.PASS, "Twitter Home Page is loaded Successfully");
                  }
                  else
                  {
                            System.out.println("Twitter Home page is not loaded successfully");
                            logger.log(LogStatus.FAIL, "Twitter Home Page is not loaded Successfully");
                  }
                  reports.endTest(logger);
               }
             
              @Test(priority=1)
              @Parameters({"browser"})
              public void loginwithValidCredentials(String bro) throws InterruptedException
              {            
                             logger=reports.startTest("Verify Login with Valid Credentials - Twitter");
                             boolean isElementFound=WebUtils.IsElementDisplayed(getDriver(bro),Enter_Username_Obj);                            
                  if(isElementFound)
                  {                                                        
                                WebUtils.ClickElementByXpath(getDriver(bro),Login_Button_Obj);
                           
                            WebUtils.ClearInputByXpath(getDriver(bro), Enter_Username_Obj);
                                           WebUtils.EnterInputByXpath(getDriver(bro), Enter_Username_Obj, Username_Data);
                                          
                                           WebUtils.ClearInputByXpath(getDriver(bro), Enter_Password_Obj);
                                           WebUtils.EnterInputByXpath(getDriver(bro), Enter_Password_Obj, Password_Data);
                                           Thread.sleep(2500);
                                          
                                           WebUtils.ClickElementByXpath(getDriver(bro),Click_Login_Obj);
                                           System.out.println("Login Successful - Twitter");
                                           Thread.sleep(2500);
                            logger.log(LogStatus.PASS, "Login Successful - Twitter");
                  }
                  else
                  {
                            System.out.println("Login button is not available");
                            logger.log(LogStatus.FAIL, "Login button is not available. So Unable to login - Twitter");
                  }
                  reports.endTest(logger);
               }
             
              @Test(priority=2)
              @Parameters({"browser"})
              public void followPOTUS(String bro) throws InterruptedException
              {            
                             String expectedText="Follow";
                             logger=reports.startTest("Verify whether POTUS is following or not - Twitter");
                             boolean isElementFound=WebUtils.IsElementDisplayed(getDriver(bro),Search_Box_Obj);                            
                  if(isElementFound)
                  {                                                                               
                            WebUtils.ClearInputByXpath(getDriver(bro), Search_Box_Obj);
                                           WebUtils.EnterInputByXpath(getDriver(bro), Search_Box_Obj, Search_Text_Data);
                                           Thread.sleep(2500);                                  
                                           WebUtils.ClickElementByXpath(getDriver(bro),Click_POTUS_Obj);
                                           Thread.sleep(5000);
                                           String text=WebUtils.GetElementText(getDriver(bro), Follow_Link_Obj);
                                           if(text.equals(expectedText))
                                           {
                                                         WebUtils.ClickElementByXpath(getDriver(bro),Click_Follow_Obj);
                                                          Thread.sleep(5000);
                                                          System.out.println("Successfully Started Following - POTUS(Twitter)");
                                           logger.log(LogStatus.PASS, "Successfully Started Following - POTUS(Twitter)");
                                           }
                                           else
                                           {
                                                          System.out.println("Already Logged-in is Following - POTUS(Twitter)");
                                           logger.log(LogStatus.PASS, "Already Logged-in User is Following - POTUS(Twitter)");                  
                                           }
                  }
                  else
                  {
                            System.out.println("Search Textbox is not available");
                            logger.log(LogStatus.FAIL, "Search Textbox is not available");
                  }
                  reports.endTest(logger);
               }
             
              @AfterMethod
              @Parameters({"browser"})
              public void closeApplication(String bro,ITestResult result) throws Exception
              {
                             if(result.getStatus()==ITestResult.FAILURE)
                             {
                                           String screenShot_Path=WebUtils.captureScreenshot(getDriver(bro), result.getName());
                                           String image=logger.addScreenCapture(screenShot_Path);                                                                
                                           logger.log(LogStatus.FAIL, "FailedScreenshot", image);
                             }            
              }
             
              @AfterTest
              @Parameters({"browser"})
              public void endReport(String bro)
              {
                             getDriver(bro).quit();                  
    }
             
              @AfterSuite
              public void afterSuite()
              {
                             reports.flush();
                             SendMailSSLWithAttachment.mailTrigger();
              }

}

Sample Report: