Saturday, March 28, 2020

Add Extent report to selenium TestNG and attache screenshots

package testngpractice;
import java.io.File;
import java.io.IOException;
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.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
public class BaseClassDemo {
static ExtentTest test;
static ExtentReports report;
@BeforeClass
public static void startTest() {
report = new ExtentReports(System.getProperty("user.dir") + "\\ExtentReportResults.html");
test = report.startTest("ExtentDemo");
}
@AfterClass
public static void endTest() {
report.endTest(test);
report.flush();
}
public static String capture(WebDriver driver) throws IOException {
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
File Dest = new File("src/../Images/" + System.currentTimeMillis() + ".png");
String errflpath = Dest.getAbsolutePath();
FileUtils.copyFile(scrFile, Dest);
return errflpath;
}
}
package testngpractice;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.LogStatus;
public class ExtentTest extends BaseClassDemo {
@Test
public void extentReportsDemo() throws IOException {
System.setProperty("webdriver.chrome.driver","C:\\Users\\diya-mutturajh\\Desktop\\chromedriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.co.in");
if (driver.getTitle().equals("Goog")) {
test.log(LogStatus.PASS, "Navigated to the specified URL");
} else {
test.log(LogStatus.FAIL, "Test Failed");
test.log(LogStatus.FAIL,test.addScreenCapture(capture(driver))+ "Test Failed");
}
driver.close();
driver.quit();
}
}
view raw ExtentTest.java hosted with ❤ by GitHub
<!-- https://mvnrepository.com/artifact/com.relevantcodes/extentreports -->
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.2</version>
</dependency>
view raw pom.xml hosted with ❤ by GitHub

No comments:

Post a Comment