/** * Create an instance of {@link Attachment } * */ public Attachment createAttachment() { return new Attachment(); }
/** * Write throwable as attachment. * * @param throwable to write * @param title title of attachment * @return Created {@link ru.yandex.qatools.allure.model.Attachment} */ public static Attachment writeAttachmentWithErrorMessage(Throwable throwable, String title) { String message = throwable.getMessage(); try { return writeAttachment(message.getBytes(CONFIG.getAttachmentsEncoding()), title); } catch (Exception e) { e.addSuppressed(throwable); LOGGER.error(String.format("Can't write attachment \"%s\"", title), e); } return new Attachment(); }
/** * Write attachment with specified type. Generate attachment name uses * {@link #generateAttachmentName()}, attachment extension uses * {@link #getExtensionByMimeType(String)} * * @param attachment byte array with attachment * @param title attachment title * @param type valid mime-type of attachment * @return Created {@link ru.yandex.qatools.allure.model.Attachment} * @throws IOException if can't write attachment */ public static Attachment writeAttachment(byte[] attachment, String title, String type) throws IOException { String name = generateAttachmentName(); String extension = getExtensionByMimeType(type); String source = name + extension; File file = new File(getResultsDirectory(), source); synchronized (ATTACHMENTS_LOCK) { if (!file.exists()) { try (FileOutputStream fos = new FileOutputStream(file)) { fos.write(attachment); } } } return new Attachment().withTitle(title).withSource(source).withType(type); }
@Override public void process(Step step) { Attachment attachment = new Attachment(); String source = writeAttachment( getAttach(), getAttachmentType() ); attachment.setTitle(getTitle()); attachment.setType(getAttachmentType()); attachment.setSource(source); step.getAttachments().add(attachment); }