I am working on Java Selenium TestNG. Selenium 4.10.0 TestNG 7.8.0
I am trying to generate Extent Reports, but the CSS properties are not loaded, probably because of the internet restrictions I have. The github CDN from which the CSS are taken are blocked in my organization, and would not be whitelisted. Is there a way where I can get the reports working good without internet? Any jars for css,etc are available? Or is there any alternative? Please help!
Note: I cannot use Maven, so all jars must be downloaded and then loaded into my IDE (IntelliJ)
Extent Report dependencies (jar files): extentreports-4.0.9 freemarker-2.3.23 bson-3.3.0 gson-2.8.5 httpclient-4.5.2 httpcore-4.4.4 httpmime-4.5.2 mongodb-driver-3.3.0 mongodb-driver-core-3.3.0 commons-codec-1.9.0 commons-logging-1.2
Code I tried: TestBase.java (Base Class)
package tests;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.ITestContext;
import org.testng.annotations.Listeners;
import utils.ExtentManager;
import utils.ExtentTestManager;
import utils.TestListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;
@Listeners(TestListener.class)
public class TestBase {
public static WebDriver driver;
public static Properties prop;
public static int TIMEOUT = 60;
public TestBase() {
try {
prop = new Properties();
FileInputStream fis = new FileInputStream(System.getProperty("user.dir") + "/src/testDAta/config.properties");
prop.load(fis);
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
public void setup(ITestContext context) {
System.setProperty("webdriver.edge.driver", System.getProperty("user.dir") + prop.getProperty("edge.path"));
driver = new EdgeDriver();
driver.manage().window().maximize();
context.setAttribute("driver", driver);
}
public void getURL(String url){
if (url.equalsIgnoreCase("google")) {
driver.get(prop.getProperty("web.urlG"));
} else if (url.equalsIgnoreCase("yahoo")) {
driver.get(prop.getProperty("web.urlY"));
} else {
System.out.println("none!");
}
}
public void waitAndClickElement(WebElement element) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(TIMEOUT));
wait.until(ExpectedConditions.elementToBeClickable(element)).click();
}
public void waitForElementToAppear(WebElement element){
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(TIMEOUT));
wait.until(ExpectedConditions.visibilityOf(element));
}
public void switchWindow(int idx){
Set<String> allHandles = driver.getWindowHandles();
List<String> handlesList = new ArrayList<>(allHandles);
System.out.println("Handles: " + handlesList);
driver.switchTo().window(handlesList.get(idx));
}
public void captureScreenshot(String screenshotName){
try{
TakesScreenshot ts = (TakesScreenshot) driver;
File srcFile = ts.getScreenshotAs(OutputType.FILE);
String destPath = System.getProperty("user.dir") + "/screenshots/" + screenshotName + ".png";
File destFile = new File(destPath);
FileUtils.copyFile(srcFile, destFile);
}catch (IOException e){
e.printStackTrace();
}
}
}
HomePage.java (PageObject file)
package pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import tests.TestBase;
public class HomePage extends TestBase {
public WebDriver driver;
public HomePage(WebDriver driver){
// this.driver = driver;
PageFactory.initElements(driver, this);
}
@FindBy(id = "getwebsitebtn")
WebElement txtLink;
@FindBy(xpath = "//*[text()='Get Started for Free']")
WebElement getFreeBtn;
public void linkTest(){
waitAndClickElement(txtLink);
switchWindow(1);
waitAndClickElement(getFreeBtn);
}
}
TestFile.java (Test file)
package tests;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import org.testng.ITestContext;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import pages.HomePage;
import utils.ExtentTestManager;
import java.io.IOException;
public class TestFile extends TestBase{
@BeforeClass
public void setItUp(ITestContext context){
setup(context);
}
public static ExtentTest extentTest;
@Test
public void urlTest() throws IOException {
// extentTest = ExtentTestManager.getTest();
// Call the setup method to initialize the driver
// getURL("Yahoo"); // You can change this to "Google" or other URLs as needed
extentTest = ExtentTestManager.getTest();
getURL("Yahoo");
HomePage hp = new HomePage(driver); // Initialize the HomePage object after the driver is set up
hp.linkTest();
extentTest.log(Status.INFO, "Clicked on Links");
captureScreenshot("snap1");
System.out.println(System.getProperty("user.dir"));
extentTest.addScreenCaptureFromPath(System.getProperty("user.dir") + "/screenshots/snap1.png", "snapshot");
ExtentTestManager.endTest();
}
}
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
<test verbose="2" preserve-order="true" name="E:/MyCode/frmwrk">
<classes><class name="tests.TestFile">
<methods><include name="urlTest"/>
</methods>
</class>
</classes>
</test>
</suite>
ExtentManager.java
package utils;
import java.io.File;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;
public class ExtentManager {
private static ExtentReports extent;
private static String reportFileName = "Test-Automaton-Report" + ".html";
private static String fileSeperator = System.getProperty("file.separator");
private static String reportFilepath = System.getProperty("user.dir") + fileSeperator + "TestReport";
private static String reportFileLocation = reportFilepath + fileSeperator + reportFileName;
public static ExtentReports getInstance() {
if (extent == null)
createInstance();
return extent;
}
// Create an extent report instance
public static ExtentReports createInstance() {
String fileName = getReportPath(reportFilepath);
ExtentSparkReporter htmlReporter = new ExtentSparkReporter(fileName);
htmlReporter.config().setTheme(Theme.DARK);
htmlReporter.config().setDocumentTitle(reportFileName);
htmlReporter.config().setReportName(reportFileName);
htmlReporter.config().setTimeStampFormat("MMM dd, yyyy HH:mm:ss");
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
return extent;
}
// Create the report path
private static String getReportPath(String path) {
File testDirectory = new File(path);
if (!testDirectory.exists()) {
if (testDirectory.mkdir()) {
System.out.println("Directory: " + path + " is created!");
return reportFileLocation;
} else {
System.out.println("Failed to create directory: " + path);
return System.getProperty("user.dir");
}
} else {
System.out.println("Directory already exists: " + path);
}
return reportFileLocation;
}
}
ExtentTestManager.java
package utils;
import java.util.HashMap;
import java.util.Map;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
public class ExtentTestManager {
static Map<Long, ExtentTest> extentTestMap = new HashMap<>();
static ExtentReports extent = ExtentManager.getInstance();
public static synchronized ExtentTest getTest() {
return extentTestMap.get(Thread.currentThread().getId());
}
public static synchronized void endTest() {
extent.flush();
}
public static synchronized ExtentTest startTest(String testName) {
ExtentTest test = extent.createTest(testName);
extentTestMap.put(Thread.currentThread().getId(), test);
return test;
}
}
TestListener.java
package utils;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import com.aventstack.extentreports.Status;
import tests.TestBase;
import utils.ExtentManager;
import utils.ExtentTestManager;
public class TestListener extends TestBase implements ITestListener {
public void onStart(ITestContext context) {
System.out.println("*** Test Suite " + context.getName() + " started ***");
System.setProperty("org.freemarker.loggerLibrary", "none");
}
public void onFinish(ITestContext context) {
System.out.println(("*** Test Suite " + context.getName() + " ending ***"));
ExtentTestManager.endTest();
ExtentManager.getInstance().flush();
}
public void onTestStart(ITestResult result) {
System.out.println(("*** Running test method " + result.getMethod().getMethodName() + "..."));
ExtentTestManager.startTest(result.getMethod().getMethodName());
}
public void onTestSuccess(ITestResult result) {
System.out.println("*** Executed " + result.getMethod().getMethodName() + " test successfully...");
ExtentTestManager.getTest().log(Status.PASS, "Test passed");
}
public void onTestFailure(ITestResult result) {
System.out.println("*** Executed " + result.getMethod().getMethodName() + " test FAILED...");
ExtentTestManager.getTest().log(Status.FAIL, "Test Failed");
ExtentTestManager.getTest().fail(result.getThrowable());
StringWriter sw = new StringWriter();
result.getThrowable().printStackTrace(new PrintWriter(sw));
sw = null;
WebDriver driver = (WebDriver) result.getTestContext().getAttribute("driver");
String root = System.getProperty("user.dir");
TakesScreenshot ts = (TakesScreenshot) driver;
File ss = ts.getScreenshotAs(OutputType.FILE);
long random = System.currentTimeMillis();
String des = root + "//target//screenshots//ss" + result.getMethod().getMethodName() + random + ".png";
try {
FileUtils.copyFile(ss, new File(des));
} catch (IOException e) {
e.printStackTrace();
}
ExtentTestManager.getTest().fail("Test skipped");
try {
ExtentTestManager.getTest().addScreenCaptureFromPath(des);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void onTestSkipped(ITestResult result) {
System.out.println("*** Test " + result.getMethod().getMethodName() + " skipped...");
ExtentTestManager.getTest().log(Status.SKIP, "Test Skipped");
}
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
System.out.println("*** Test failed but within percentage % " + result.getMethod().getMethodName());
}
}