allure-java: Attachments not working for Configuration Methods using IInvokedMethodListener (Allure-TestNG

I’m submitting a …

- [X] bug report

  • feature request
  • support request => Please do not submit support request here, see note at the top of this template.

What is the current behavior?

Issue description:

  • I’m implementing the IInvokedMethodListener for TestNG
  • Inside it I have Overridden the afterInvocation method
  • I use this to create a log for the current test method Reporter.getOutput(testResult)
  • Then I try to attach this log file using this code Allure.addAttachment(attachmentDescription, "text/plain", attachmentContent, ".txt");
  • This works as expected for all @Test methods, but it doesn’t work for configuration methods like @BeforeMethod and @AfterMethod
  • This error is shown in the console [main] ERROR io.qameta.allure.AllureLifecycle - Could not add attachment: no test is running

Debugging Information

Upon debugging this issue I traced it to this method String io.qameta.allure.AllureLifecycle.prepareAttachment(String name, String type, String fileExtension)

Apparently threadContext.getCurrent(); is returning empty instead of returning the UUID of the current configuration method.

This behavior is shown if I’m running any test, but if I’m debugging it becomes intermittent, and sometimes works as expected!!!

What is the expected behavior?

The attachment should be added successfully, and this error should not happen.

What is the motivation / use case for changing the behavior?

This impacts reporting attachments for any configuration method.

Please tell us about your environment:

| Test framework | testng@7.0.0 | | Allure integration | allure-testng@2.13.1 | | AspectJ Weaver | aspectjweaver | 1.9.5 | | Java run-time environment | JDK | 13 | | IDE | Eclipse | Build id: 20191212-1212 |

Other information

  • I couldn’t downgrade to acpectj 1.8.0 because it’s not working with JDK 13
  • Tried using a different listener ‘IConfigurationListener’ but the same issue persisted

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 24 (3 by maintainers)

Most upvoted comments

@baev I’ve manage to do it ! Thanks for help.

How?

  1. I’ve created file io.qameta.allure.listener.StepLifecycleListener in the resource folder in location src/main/resources/META-INF/services/
  2. In the file you need to specify the full package to your new Allure Step Listener class. (in my case testautomation.tests.util.AllureStepListener)
  3. Create class AllureStepListener that implements StepLifecycleListener and add screenshot creation in it(my class below).

That’s all - it works for me.

public class AllureStepListener implements StepLifecycleListener {
	
	protected static Logger logger = LogManager.getLogger(AllureStepListener.class);
	
	@Override
    public void beforeStepStop(final StepResult result) {
			
		if(result.getStatus().equals(Status.FAILED) || result.getStatus().equals(Status.BROKEN)) {
			screenshot();
			result.setStatus(Status.FAILED);
	    }
		
	}

		 @Attachment(value = "Screenshot", type = "image/png")
		 public byte[] screenshot() {
				List<IWebDriver> driverInstances = WebDriverFactory.getInstance().getOpenDriverInstances();
				byte[] screenshotByte = null;
				for (IWebDriver driverInstance : driverInstances) {
					 screenshotByte = ((TakesScreenshot) driverInstance).getScreenshotAs(OutputType.BYTES);
					}
			
				return screenshotByte;
		   
		 }
	}

@PePeLM I tried both and neither of them worked for me.

I can suggest using Allure Lifecycle listeners like io.qameta.allure.listener.StepLifecycleListener or io.qameta.allure.listener.TestLifecycleListener

For me it’s working randomly once it’s adding attachments, once it’s not…

yeah, that’s because Allure is using the same API to process TestNG lifecycle events, and there is no guarantee in what order this hooks are executed. So basically if your hook goes first, you’ll see the attachment in the report, otherwise you’ll not.