Skip to content

Instantly share code, notes, and snippets.

@SarahElson
Created June 14, 2024 16:15
Show Gist options
  • Save SarahElson/ff93619c5e2b4974bbc7c57f69881ca0 to your computer and use it in GitHub Desktop.
Save SarahElson/ff93619c5e2b4974bbc7c57f69881ca0 to your computer and use it in GitHub Desktop.
How To Generate Extent Reports In Selenium
package CloudGrid;
import java.net.*;
import java.util.HashMap;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.ITestResult;
import org.testng.annotations.*;
import com.aventstack.extentreports.*;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;
public class BaseTest {
public static ExtentSparkReporter extentSparkReporter;
public static ExtentReports extentReports;
public static ExtentTest extentTest;
public RemoteWebDriver driver = null;
String username = System.getenv("LT_USERNAME") == null ? "<lambdatest_username>" : System.getenv("LT_USERNAME");
String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "<lambdatest_accesskey>" : System.getenv("LT_ACCESS_KEY");
String status = "failed";
@BeforeTest
public void startReporter()
{
extentSparkReporter = new ExtentSparkReporter(
System.getProperty("user.dir") + "/test-output/extentReport.html");
extentReports = new ExtentReports();
extentReports.attachReporter(extentSparkReporter);
// configuration items to change the look and feel
// add content, manage tests etc
extentSparkReporter.config().setDocumentTitle("Simple Automation Report");
extentSparkReporter.config().setReportName("Test Report");
extentSparkReporter.config().setTheme(Theme.STANDARD);
extentSparkReporter.config().setTimeStampFormat("EEEE, MMMM dd, yyyy, hh:mm a '('zzz')'");
}
@BeforeMethod
public void setUp()
{
try {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setPlatformName("Windows 10");
chromeOptions.setBrowserVersion("124.0");
HashMap<String, Object> ltOptions = new HashMap<String, Object>();
ltOptions.put("build", "Extent Reports using Selenium Java");
ltOptions.put("name", "Extent Reports using Selenium Java");
ltOptions.put("w3c", true);
chromeOptions.setCapability("LT:Options", ltOptions);
driver = new RemoteWebDriver(
new URL("https://" + username + ":" + accessKey + "@hub.lambdatest.com/wd/hub"), chromeOptions);
driver.get("https://www.lambdatest.com/selenium-playground/");
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
@AfterMethod
public void updateResultAndcloseDriver(ITestResult result)
{
if(result.getStatus() == ITestResult.FAILURE) {
extentTest.log(Status.FAIL,result.getThrowable());
}
else if(result.getStatus() == ITestResult.SUCCESS) {
extentTest.log(Status.PASS, result.getTestName());
}
else {
extentTest.log(Status.SKIP, result.getTestName());
}
driver.executeScript("lambda-status=" + status);
driver.quit();
}
@AfterTest
public void endReport()
{
extentReports.flush();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment