BaseSteps.java : Extent report configuration & Screenshot code implementation
package com.automationexercise.automationUtilities;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.Properties;
public class BaseSteps {
private Properties prop;
private String browserName;
private String appurl;
public static WebDriver driver;
private Logger log;
public static ExtentSparkReporter sparkReporter;
public static ExtentReports extent;
public static ExtentTest test;
@BeforeMethod
public void setup() {
try {
log= LogManager.getLogger("com.automationexcercise.automationUtilities.BaseSteps");
prop = new Properties();
prop.load(BaseSteps.class.getClassLoader().getResourceAsStream("configuration.properties"));
browserName = prop.getProperty("browser");
log.info("browser from properties file is : " + browserName);
if (browserName.equalsIgnoreCase("chrome")) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
log.info("Initiated Chrome Driver");
} else if (browserName.equalsIgnoreCase("firefox")) {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
log.info("Initiated Firefox Driver");
} else if (browserName.equalsIgnoreCase("edge")) {
WebDriverManager.edgedriver().setup();
driver = new EdgeDriver();
log.info("Initiated Edge Driver");
} else {
driver = null;
}
if (driver != null) {
appurl = prop.getProperty("url");
log.info("url is : " + appurl);
driver.get(appurl);
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void initializeReport(){
sparkReporter = new ExtentSparkReporter(System.getProperty("user.dir")+"/Reports/extentSparkReport.html");
sparkReporter.config().setDocumentTitle("AutomationReport");
sparkReporter.config().setReportName("Automation Test Execution Report");
sparkReporter.config().setTheme(Theme.STANDARD);
sparkReporter.config().setTimeStampFormat("EEEE, MMMM dd, yyyy, hh:mm a '('zzz')'");
extent =new ExtentReports();
extent.attachReporter(sparkReporter);
}
public static String captureScreenshot(WebDriver driver) throws IOException {
String FileSeparator = System.getProperty("file.separator");
String Extent_report_path = "."+FileSeparator+"Reports";
String ScreenshotPath = Extent_report_path+FileSeparator+"screenshots";
File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
String screenshotName = "screenshot"+Math.random()+".png";
String screenshotpath = ScreenshotPath+FileSeparator+screenshotName;
FileUtils.copyFile(src,new File(screenshotpath));
return "."+FileSeparator+"screenshots"+FileSeparator+screenshotName;
}
@AfterMethod
public void tearDown(){
driver.quit();
log.info("quit driver");
}
}
Comments
Post a Comment