Forum Discussion
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.
Thanks for sharing your code.
Your code doesn't seem to use Cucumber, so I'm a bit puzzled you're asking in this forum. We only offer support for Cucumber, not other testing tools.
- mortelee4 years agoOccasional Contributor
Hi
Just stumbled on this thread. I had the same problem
See code below.
When I use the Then function without settting a timeout, the test stops with a message that timeout of 5000ms has been reached. If I add the timeout {timeout: 60000} then it works.
Looks like adding the screenshots with world.attach takes too long (taking the screenshot is instantly)??
I'm using Cucumber v7.0.0
'''
Then('patient info shows patient name {string}', async function (this: CustomWorld, name: string😞 Promise<void> {const actualPatientName = await getPatientName();expect(actualPatientName).to.be.equal(name);await ActualResultWithScreenshot(`Patient info shows patient name "${actualPatientName}"`, this);});'''
'''
export async function ActualResultWithScreenshot(message: string | null | undefined,world: CustomWorld😞 Promise<void> {if (message) {ActualResult(message, world);}const screenshot = await world.cath.page?.screenshot(); // Take screenshot from playwright pageif (screenshot) {world.attach(screenshot, 'image/png');}}'''- aurelien-reeves4 years agoStaff
Would you have the possibility to add some benchmarks to measure how longs take world.attach, but also other methods in your tests?
Related Content
- 2 years ago