Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#128: Implemented evidence attachment support #135

Merged
merged 1 commit into from
Mar 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ ReportIntegrationConfig.get().usePreset(preset);
```
- `Attachment.screenshots()` extracts screenshots if present. It simply retrieves all available step's screenshots, so screenshot strategy is configured on Serenity level.
- `Attachment.htmlSources()` extracts page source if available. Work in the same way as screenshots attachment.
- `Attachment.evidences()` logs evidences provided by Serenity. Attach each evidence as step attachment.
```
// Evidence with manually specified content
Serenity.recordReportData().asEvidence().withTitle("Title").andContents("My Content");
// Evidence from file
Serenity.recordReportData().asEvidence().withTitle("My Title").downloadable().fromFile(Paths.get("/Path/to/file"));
```
> **Notice**
>
> All evidences must be attached inside step method, otherwise they won't be send to RP.

- `Selenium.allLogs()` retrieves all logs supplied by Selenium. Suitable only for UI tests, when web driver supply some logs. Selenium logs works in conjunction with Selenium logs harvesting feature.
- `Selenium.filteredLogs(...)` retrieves logs supplied by Selenium, but filtered by passed predicate.
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public Function<TestStep, Collection<SaveLogRQ>>[] logUnits() {
return new Function[]{
Attachment.screenshots(),
Rest.restQuery(),
Error.basic()
Error.basic(),
Attachment.evidences()
};
}
},
Expand All @@ -40,6 +41,7 @@ public Function<TestStep, Collection<SaveLogRQ>>[] logUnits() {
Rest.restQuery(),
Error.basic(),
Attachment.htmlSources(),
Attachment.evidences(),
Selenium.allLogs()
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package com.github.invictum.reportportal.log.unit;

import com.epam.ta.reportportal.ws.model.log.SaveLogRQ;
import com.github.invictum.reportportal.LogLevel;
import com.github.invictum.reportportal.Utils;
import net.serenitybdd.core.environment.ConfiguredEnvironment;
import net.thucydides.core.model.ReportData;
import net.thucydides.core.model.TestStep;
import net.thucydides.core.screenshots.ScreenshotAndHtmlSource;
import org.apache.tika.Tika;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Attachment {

private final static Logger LOG = LoggerFactory.getLogger(Attachment.class);
private final static Tika TIKA = new Tika();

/**
* Logs screenshots from passed {@link TestStep} if present
Expand Down Expand Up @@ -86,4 +94,49 @@ public static Function<TestStep, Collection<SaveLogRQ>> htmlSources() {
return sources;
};
}

/**
* Logs all evidences in a test step as attachment files in RP
*/
public static Function<TestStep, Collection<SaveLogRQ>> evidences() {
return step -> step.getReportEvidence().stream()
.filter(ReportData::isEvidence)
.map(report -> report.getPath() == null ? fromContent(report) : fromFile(report))
.filter(Objects::nonNull)
.peek(log -> log.setLogTime(Utils.stepEndDate(step)))
.collect(Collectors.toSet());
}

private static SaveLogRQ fromFile(ReportData data) {
SaveLogRQ.File file = new SaveLogRQ.File();
file.setName(data.getId());
try {
String reportRoot = ConfiguredEnvironment.getConfiguration().getOutputDirectory().toString();
Path path = Paths.get(reportRoot, data.getPath());
byte[] content = Files.readAllBytes(path);
file.setContent(content);
String mime = TIKA.detect(content, data.getPath());
file.setContentType(mime);
} catch (IOException e) {
LOG.error("Unable to attach evidence", e);
return null;
}
SaveLogRQ log = new SaveLogRQ();
log.setMessage(data.getTitle());
log.setFile(file);
log.setLevel(LogLevel.DEBUG.toString());
return log;
}

private static SaveLogRQ fromContent(ReportData data) {
SaveLogRQ.File file = new SaveLogRQ.File();
file.setName(data.getId());
file.setContentType("application/txt");
file.setContent(data.getContents().getBytes());
SaveLogRQ log = new SaveLogRQ();
log.setFile(file);
log.setMessage(data.getTitle());
log.setLevel(LogLevel.DEBUG.toString());
return log;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class LogsPresetTest {
@Test
public void fullPreset() {
int actual = LogsPreset.FULL.logUnits().length;
Assert.assertEquals(5, actual);
Assert.assertEquals(6, actual);
}

@Test(expected = UnsupportedOperationException.class)
Expand All @@ -23,7 +23,7 @@ public void fullPresetCustomization() {
@Test
public void defaultPreset() {
int actual = LogsPreset.DEFAULT.logUnits().length;
Assert.assertEquals(3, actual);
Assert.assertEquals(4, actual);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.epam.ta.reportportal.ws.model.log.SaveLogRQ;
import com.github.invictum.reportportal.LogLevel;
import net.serenitybdd.core.environment.ConfiguredEnvironment;
import net.thucydides.core.model.ReportData;
import net.thucydides.core.model.TestResult;
import net.thucydides.core.model.TestStep;
import net.thucydides.core.screenshots.ScreenshotAndHtmlSource;
Expand All @@ -16,6 +18,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -69,4 +72,39 @@ public void sourcesPresent() throws IOException {
Assert.assertEquals("HTML Source", actual.getMessage());
Assert.assertEquals(LogLevel.WARN.toString(), actual.getLevel());
}

@Test
public void noEvidences() {
Collection<SaveLogRQ> logs = Attachment.evidences().apply(stepMock);
Assert.assertTrue(logs.isEmpty());
}

@Test
public void evidenceFromContents() {
ReportData reportData = new ReportData("title", "content", null, true);
Mockito.when(stepMock.getReportEvidence()).thenReturn(Collections.singletonList(reportData));
Mockito.when(stepMock.getStartTime()).thenReturn(ZonedDateTime.now());
Collection<SaveLogRQ> logs = Attachment.evidences().apply(stepMock);
Assert.assertEquals(1, logs.size());
SaveLogRQ actual = logs.iterator().next();
Assert.assertEquals("title", actual.getMessage());
Assert.assertEquals("content", new String(actual.getFile().getContent()));
Assert.assertEquals(LogLevel.DEBUG.toString(), actual.getLevel());
}

@Test
public void evidenceFromFile() throws IOException {
File evidence = folder.newFile("note.txt");
Files.write(evidence.toPath(), Collections.singleton("content"));
ReportData reportData = new ReportData("note", null, "note.txt", true);
Mockito.when(stepMock.getReportEvidence()).thenReturn(Collections.singletonList(reportData));
Mockito.when(stepMock.getStartTime()).thenReturn(ZonedDateTime.now());
ConfiguredEnvironment.getConfiguration().setOutputDirectory(folder.getRoot());
Collection<SaveLogRQ> logs = Attachment.evidences().apply(stepMock);
Assert.assertEquals(1, logs.size());
SaveLogRQ actual = logs.iterator().next();
Assert.assertEquals("note", actual.getMessage());
Assert.assertEquals("content" + System.lineSeparator(), new String(actual.getFile().getContent()));
Assert.assertEquals(LogLevel.DEBUG.toString(), actual.getLevel());
}
}