Hi ,Thanks for the acknowledgement. PFB the code . FYI i am using Testng runner in my project and i am using a listener to implement the screenshot functionality. Below is the Listerner class which would capture the screenshot when a scenario fails.
package com.myproject.automation.listeners;
import io.qameta.allure.Allure;
import io.qameta.allure.Attachment;
import io.qameta.allure.listener.StepLifecycleListener;
import io.qameta.allure.model.StepResult;
import java.io.ByteArrayInputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
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.myproject.automation.baseTest.*;
public class TestAllureListener implements ITestListener {
private static String getTestMethodName(ITestResult iTestResult) {
return iTestResult.getMethod().getConstructorOrMethod().getName();
}
// Text attachments for Allure
@Attachment(value = "Page screenshot", type = "image/png")
public byte[] saveScreenshotPNG(WebDriver driver) {
System.out.println("saveScreenshotPNG method called");
//Allure.addAttachment("error screenshot", new ByteArrayInputStream(((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES)));
return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
}
// Text attachments for Allure
@Attachment(value = "{0}", type = "text/plain")
public static String saveTextLog(String message) {
return message;
}
// HTML attachments for Allure
@Attachment(value = "{0}", type = "text/html")
public static String attachHtml(String html) {
return html;
}
@Override
public void onStart(ITestContext iTestContext) {
System.out.println("I am in onStart method " + iTestContext.getName());
iTestContext.setAttribute("WebDriver", BaseTest.getDriver());
}
@Override
public void onFinish(ITestContext iTestContext) {
System.out.println("I am in onFinish method " + iTestContext.getName());
}
@Override
public void onTestStart(ITestResult iTestResult) {
System.out.println("I am in onTestStart method " + getTestMethodName(iTestResult) + " start");
}
@Override
public void onTestSuccess(ITestResult iTestResult) {
System.out.println("I am in onTestSuccess method " + getTestMethodName(iTestResult) + " succeed");
}
@Override
public void onTestFailure(ITestResult iTestResult) {
System.out.println("I am in onTestFailure method " + getTestMethodName(iTestResult) + " failed");
Object testClass = iTestResult.getInstance();
WebDriver driver = BaseTest.getDriver();
// Allure ScreenShotRobot and SaveTestLog
if (driver instanceof WebDriver) {
System.out.println("Screenshot captured for test case:" + getTestMethodName(iTestResult));
//Allure.addAttachment("error screenshot", new ByteArrayInputStream(((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES)));
saveScreenshotPNG(driver);
}
// Save a log on allure.
saveTextLog(getTestMethodName(iTestResult) + " failed and screenshot taken!");
}
@Override
public void onTestSkipped(ITestResult iTestResult) {
System.out.println("I am in onTestSkipped method " + getTestMethodName(iTestResult) + " skipped");
}
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {
System.out.println("Test failed but it is in defined success ratio " + getTestMethodName(iTestResult));
}
}
Do let me know in case of any queries.