-
Changes in Version 3.6.2 (2021-01-22)
Changes in Version 3.5.0 (2020-06-04)
Changes in Version 3.4.2 (2020-03-26)
Changes in Version 3.3.1 (2019-08-22)
Changes in Version 3.3.0 (2019-01-09)
Changes in Version 3.2.1 (2018-12-21)
Changes in Version 3.2.0 (2018-08-27)
Changes in Version 3.1.1 (2018-06-29)
Changes in Version 3.1.0 (2018-03-27)
Changes in Version 3.0.0 (2018-02-20)
Attachment Listener
In a previous chapter we showed you how to implement custom execution listeners. You might have noted, that some of the listeners were responsible for creating attachments (screenshots, HTML content and so on). In this chapter we show you how to handle attachments during the test execution. The attachment listeners are part of tapir’s execution module.
<dependency>
<groupId>de.bmiag.tapir</groupId>
<artifactId>tapir-execution</artifactId>
</dependency>
Sending Attachments
Let’s first take a look at how to create and send attachments. The following snippet is a simplified excerpt from the ScreenshotService - a service responsible for making screenshots with the current web driver.
ScreenshotService.xtend
@Component("tapirScreenshotService")
class ScreenshotService {
@Autowired
WebDriver driver
@Autowired
extension AttachmentListenerNotifier attachmentListenerNotifier
@Autowired
extension ExecutionState executionState
def void takeScreenshot(String name) {
if(driver instanceof TakesScreenshot) {
val screenshot = new AShot().takeScreenshot(driver)
val imageByteArray = ImageTool.toByteArray(screenshot.image)
val attachment = Attachment.build [
it.name = name
mimeType = MimeTypeUtils.IMAGE_PNG
content = imageByteArray
]
notifyListeners[attachmentAdded(currentTestStep.get, attachment)]
}
}
}
The listener creates a screenshot with the class AShot and creates a new attachment instance (attachment is incidentally an immutable class). The mandatory fields of the attachment are the name, the mime type and, of course, the binary content. Once created, the listener uses the AttachmentListenerNotifierto send the attachment to the registered observers. You should always use this mechanism to propagate attachments to the listeners responsible for creating reports, because otherwise not all listeners might receive the data.
Receiving Attachments
If you want to be notified about new attachments, you have to create a class which implements the interface AttachmentListener. Furthermore, Spring must be aware of your component. The following excerpt is from the FilesystemAttachmentListener, which writes the attachments to a temporary folder on the file system.
@Component("tapirFilesystemAttachmentListener")
class FilesystemAttachmentListener implements AttachmentListener {
override attachmentAdded(TestStep testStep, Attachment attachment) {
val dirName = '''«testStep.parentTestClass.name».«testStep.name»'''
val directory = Files.createTempDirectory(dirName)
val fileExtension = switch (attachment.mimeType) {
case APPLICATION_JSON: "json"
case APPLICATION_XML: "xml"
case IMAGE_GIF: "gif"
case IMAGE_JPEG: "jpg"
case IMAGE_PNG: "png"
case TEXT_HTML: "html"
case TEXT_PLAIN: "txt"
case TEXT_XML: "xml"
default: "bin"
}
val attachmentFile = new File(directory.toFile(), '''«attachment.name».«fileExtension»''')
FileUtils.writeByteArrayToFile(attachmentFile, attachment.content)
}
}
The listener receives the attachment, determines the file extension and writes it into a temporary folder. In a similar manner you can write your own listener to handle attachments.
- Previous
- Next