Allure doesn't capture screenshot becouse event is after test. #2779
Replies: 3 comments
-
The solution https://github.com/biczomate/allure-testng7.5-attachment-example |
Beta Was this translation helpful? Give feedback.
-
I have tried to build the following code based on this example: import io.qameta.allure.Allure;
import io.qameta.allure.Attachment;
import io.qameta.allure.listener.TestLifecycleListener;
import io.qameta.allure.model.TestResult;
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.testng.ITestListener;
import static io.qameta.allure.model.Status.BROKEN;
import static io.qameta.allure.model.Status.FAILED;
import java.io.ByteArrayInputStream;
public class AllureTestLifecycleListener implements ITestListener, TestLifecycleListener {
private static final Logger log = LogManager.getLogger(AllureTestLifecycleListener.class);
private static WebDriver driver;
public AllureTestLifecycleListener() {
}
public static void setDriver(WebDriver driverInstance) {
driver = driverInstance;
}
private static String getTestMethodName(TestResult result) {
return result.getName();
}
@Attachment(value = "Page Screenshot", type = "image/png")
public byte[] saveScreenshot() {
return driver instanceof TakesScreenshot ? ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES)
: new byte[0];
}
@Attachment(value = "{0}", type = "text/plain")
public static String saveTextLog(String message) {
return message;
}
@Override
public void beforeTestStop(TestResult result) {
if (FAILED == result.getStatus() || BROKEN == result.getStatus()) {
if (driver != null) {
saveScreenshot();
log.info("SCREENSHOT IT'S MADE");
Allure.addAttachment(result.getName(), new ByteArrayInputStream(saveScreenshot()));
} else {
log.info("Driver is null.");
}
saveTextLog(getTestMethodName(result) + " failed");
Allure.addAttachment("test", "test");
}
}
} However, the method
Is there any other way, considering that when using the ITestListener implementation, test assertions finish before the screenshot is taken? |
Beta Was this translation helpful? Give feedback.
-
Is anyone able to help me? Is what @baev sent the only possibility? |
Beta Was this translation helpful? Give feedback.
-
and
testsuite.xml
I have error: [main] ERROR io.qameta.allure.AllureLifecycle - Could not add attachment: no test is running
I've been working on this for a few days now and I don't have a solution. Please help.
I want allure to upload a screenshot to the report immediately after a failed test.
JDK: 21, apache-maven-3.9.9, selenium: 4.25.0, allure: 2.29.0, testNG: 7.10.2, VSCode
Beta Was this translation helpful? Give feedback.
All reactions