01
Jan

Create Extent Reports with Selenium:

Pre-requisites Installations to Generate Extent Reports:

Following softwares needs to be configured / installed on local system:
1. JDK
2. TestNG
3. Extent Report Jars Download here
4. extent-config.xml

Step 1: Create an Init class with following capabilities:

  1. Create folder structure for storing Reports
  2. Launch Browser

import java.io.File;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.ITestContext;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;

public class Init {

public WebDriver driver;
public static ExtentReports extent;
public static ExtentTest logger;
private static ExtentHtmlReporter htmlReporter;
private static String configfilePath = “./extent-config.xml”;
private static String filePath;
public static String workingDir = System.getProperty(“user.dir”);
public static String appUrl, browserType;

String directoryName, suiteName;

@BeforeSuite(alwaysRun = true) // Creates necessary folder structure to
Create Extent reports
public void initiateExtentReport(ITestContext context) {
directoryName = getSuiteName(context);
Init.createDirectory(directoryName);
filePath = workingDir + “/” + “Reports” + “/” + directoryName +
“/” + directoryName + “.html”;
GetExtent(filePath);
}

@BeforeMethod(alwaysRun = true)
public void beforeMethod() throws Exception {
try {
browserType = “InternetExplorer”;
appUrl = “http://wwww.google.co.in”;
driver = launch(browserType, appUrl);
} catch (Exception e) {
System.out.println(e.getMessage());
Assert.fail(“Error occured while launching the
browser”);
}

}

/* Method to launch browser with configured url */
public WebDriver launch(String browserType, String appUrl) {
String browser = browserType.toLowerCase();
try {
switch (browser) {
case “chrome”:
driver = invokeChromeDriver(appUrl);
break;
case “internet explorer”:
case “ie”:
driver = invokeIEDriver(appUrl);
break;
default:
driver = invokeFirefoxDriver(appUrl);
break;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(“Driver intialization failed”);
}

return driver;
}

// Launches Chrome driver
public static WebDriver invokeChromeDriver(String url)
{
System.setProperty(“webdriver.chrome.driver”,
“./Resources/chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(20,
TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get(url);
driver.navigate().refresh();
return driver;
}

// Launches Firefox driver
public static WebDriver invokeFirefoxDriver(String url)
{
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30,
TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get(url);
driver.navigate().refresh();
return driver;
}

// launches Internet explorer
public static WebDriver invokeIEDriver(String url) throws
InterruptedException
{
System.setProperty(“webdriver.ie.driver”, workingDir
+”/”+”Resources”+”/”+”IEDriverServer.exe”);
WebDriver driver = new InternetExplorerDriver();
driver.manage().timeouts().implicitlyWait(20,
TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get(url);
Thread.sleep(50000);
driver.navigate().refresh();
return driver;
}

// returns a suitename
public String getSuiteName(ITestContext context) {
suiteName = context.getSuite().getName();
return suiteName;
}

// Creates a directory for storing Reports
public static String createDirectory(String SuiteName) {
new File(“./Reports/FailScreenshots”).mkdirs();

File dir = new File(“./Reports/” + SuiteName);
if (!dir.exists()) {
dir.mkdirs();
System.out.println(“New folder created in specified
path :” + dir);
} else {
System.out.println(“Pls check that directory is already
created for the specifed path”);
return dir.getName();
}
return dir.getName();
}

public static ExtentReports GetExtent(String filepath) {
if (extent != null)
return extent; // avoid creating new instance of html
file
extent = new ExtentReports();
extent.attachReporter(getHtmlReporter(filepath));
return extent;
}

private static ExtentHtmlReporter getHtmlReporter(String filepath) {
htmlReporter = new ExtentHtmlReporter(filepath);
htmlReporter.loadXMLConfig(configfilePath);
return htmlReporter;
}

}

Step 2: Create a PageObject Class

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.aventstack.extentreports.Status;
import com.svadmin.generics.Init;

public class pageObjectPageName extends Init {

WebDriver driver;
WebDriverWait wait;

public pageObjectPageName(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
wait = new WebDriverWait(driver, 10);
}

/* All text identification below */
@FindBy(xpath = “//td[@class=’dht’]”)
public WebElement pageHeader;

/* All Radio button identification below */
@FindBy(id = “ctl00_PageContent_RbnOutletMove”)
public WebElement rdbOnlyOutletMovement;

/* Following are the button identifications */
@FindBy(id = “Button”)
public WebElement btnImport;

// Method to Move Cards From One Outlet To Another using File
public void bulkCardMovementFromOutletToAnotherUsingFile(String values) {
try {
logger.log(Status.INFO, “start test for xxx feature”);

wait.until(ExpectedConditions.visibilityOf(btnImport));
btnImport.click();
// log status in report
logger.log(Status.DEBUG, “Debug in report”);
logger.log(Status.ERROR, “Error in report”);
logger.log(Status.FAIL, “Fail in report”);
logger.log(Status.FATAL, “Fatal error in report”);
logger.log(Status.PASS, “Pass in report”);
logger.log(Status.WARNING, “Warning in report”);
logger.log(Status.SKIP, “Skip in report”);
} catch (Exception e) {
logger.log(Status.FATAL, “Failed to complete xxx
functionality, Exception message-” + e.getMessage());
}
}

}

Step 3: Create TestObject class

public class Test_PageName extends Init {

@Test(enabled = true)
public void Test_PageName(String TemplateName) {
try {
logger = extent.createTest(“Validate xxx functionality
on PageName”);
pageObjectPageName pg = new pageObjectPageName(driver);
pg.bulkCardMovementFromOutletToAnotherUsingFile(“test
data”);
logger.log(Status.INFO, “Completed validation of xxx
functionality on PageName”);
} catch (Exception e) {
logger.log(Status.FATAL, “Exception during xxx
functionality”);
}
}

}