Skip to content

Commit

Permalink
Add support for viewing logs on expansion (#389)
Browse files Browse the repository at this point in the history
* Add support for viewing logs on expansion

* Code Cleanup

* Code Cleanup
  • Loading branch information
RameshBabuPrudhvi authored Oct 12, 2024
1 parent 618ff27 commit cf83111
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class SeleniumService {
* @throws DriverConnectionException if the server is already started or
* fails to start
*/
@SuppressWarnings("java:S106")
@SuppressWarnings("all")
public SeleniumService start(String mode, String... extraFlags) {
if (process != null) {
throw new DriverConnectionException("Server already started");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,27 @@
import java.util.logging.LogRecord;
import java.util.stream.Collectors;

import static io.github.selcukes.collections.StringHelper.isEmpty;
import static io.github.selcukes.collections.StringHelper.isNonEmpty;

public class Reporter {
private static final SingletonContext<Reporter> REPORTER_CONTEXT = SingletonContext.with(Reporter::new);
private static final String EMPTY_LOGS = "<ul><li> </li></ul><br/>";
private static final String OPEN_TAG = "<ul><li> ";
private static final String CLOSE_TAG = "</li></ul><br/>";
private static final String LOGS_DETAILS = "<details><summary>View Logs</summary><div>%s</div></details><br/>";

private Snapshot snapshot;
private LogRecordListener logRecordListener;

public static void log(String message) {
if (!isEmpty(message)) {
SelcukesExtentAdapter.addTestStepLog(message);
}
SelcukesExtentAdapter.addTestStepLog(message);
}

public static Reporter getReporter() {
return REPORTER_CONTEXT.get();
}

Reporter start() {
protected Reporter start() {
logRecordListener = new LogRecordListener();
LoggerFactory.addListener(logRecordListener);
return this;
Expand All @@ -60,9 +63,9 @@ public String getLogRecords() {
if (logRecordListener != null) {
return logRecordListener.getLogRecords()
.filter(logRecord -> logRecord.getLevel() == Level.INFO || logRecord.getLevel() == Level.SEVERE)
.filter(logRecord -> !isEmpty(logRecord.getMessage()))
.filter(logRecord -> isNonEmpty(logRecord.getMessage()))
.map(this::mapLogMessage)
.collect(Collectors.joining("</li><li>", "<ul><li> ", "</li></ul><br/>"));
.collect(Collectors.joining("</li><li>", OPEN_TAG, CLOSE_TAG));
}
return "";
}
Expand All @@ -74,8 +77,8 @@ private String mapLogMessage(LogRecord logRecord) {

private Reporter attachLog() {
String infoLogs = getLogRecords();
if (!infoLogs.equalsIgnoreCase("<ul><li> </li></ul><br/>")) {
Reporter.log(infoLogs);
if (!infoLogs.equalsIgnoreCase(EMPTY_LOGS)) {
Reporter.log(String.format(LOGS_DETAILS, infoLogs));
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import static io.github.selcukes.collections.StringHelper.isNonEmpty;
import static io.github.selcukes.extent.report.Reporter.getReporter;

public class SelcukesExtentAdapter implements ConcurrentEventListener {
Expand Down Expand Up @@ -107,13 +108,18 @@ public SelcukesExtentAdapter() {
}

public static synchronized void addTestStepLog(final String message) {
stepTestThreadLocal.get().info(message);
if (isNonEmpty(message)) {
stepTestThreadLocal.get().info(message);
}
}

public static synchronized void attachVideo(byte[] video) {
stepTestThreadLocal.get().addVideoFromBase64String(Base64.getEncoder().encodeToString(video));
}

public static void attachScreenshot(byte[] screenshot) {
stepTestThreadLocal.get().info("", MediaEntityBuilder
.createScreenCaptureFromBase64String(Base64.getEncoder().encodeToString(screenshot))
.build());

stepTestThreadLocal.get().addScreenCaptureFromBase64String(Base64.getEncoder().encodeToString(screenshot));
}

@Override
Expand Down Expand Up @@ -165,41 +171,56 @@ private synchronized void handleTestStepFinished(TestStepFinished event) {

private synchronized void updateResult(Result result) {
Test test = stepTestThreadLocal.get().getModel();
switch (result.getStatus().name().toLowerCase()) {
case "failed", "pending" -> stepTestThreadLocal.get().fail(result.getError());
case "undefined" -> stepTestThreadLocal.get().fail("Step undefined");
case "skipped" -> {
if (isHookThreadLocal.get().equals(Boolean.TRUE)) {
extentService.getExtentReports().removeTest(stepTestThreadLocal.get());
break;
}
boolean currentEndingEventSkipped = test.hasLog()
&& test.getLogs().get(test.getLogs().size() - 1).getStatus() == Status.SKIP;
if (result.getError() != null) {
stepTestThreadLocal.get().skip(result.getError());
}
if (!currentEndingEventSkipped) {
String details = result.getError() == null ? "Step skipped" : result.getError().getMessage();
stepTestThreadLocal.get().skip(details);
}
}
case "passed" -> {
if (stepTestThreadLocal.get() != null) {
if (isHookThreadLocal.get().equals(Boolean.TRUE)) {
boolean mediaLogs = test.getLogs().stream().anyMatch(l -> l.getMedia() != null);
if (!test.hasLog() && !mediaLogs) {
extentService.getExtentReports().removeTest(stepTestThreadLocal.get());
}
}
stepTestThreadLocal.get().pass("");
}
}
String status = result.getStatus().name().toLowerCase();

switch (status) {
case "failed", "pending" -> handleFailedOrPendingResult(result);
case "undefined" -> handleUndefinedResult();
case "skipped" -> handleSkippedResult(result, test);
case "passed" -> handlePassedResult(test);
default -> {
// do nothing
}
}
}

private void handleFailedOrPendingResult(Result result) {
stepTestThreadLocal.get().fail(result.getError());
}

private void handleUndefinedResult() {
stepTestThreadLocal.get().fail("Step undefined");
}

private void handleSkippedResult(Result result, Test test) {
if (isHookThreadLocal.get().equals(Boolean.TRUE)) {
extentService.getExtentReports().removeTest(stepTestThreadLocal.get());
return;
}

boolean currentEndingEventSkipped = test.hasLog()
&& test.getLogs().get(test.getLogs().size() - 1).getStatus() == Status.SKIP;
if (result.getError() != null) {
stepTestThreadLocal.get().skip(result.getError());
}
if (!currentEndingEventSkipped) {
String details = result.getError() == null ? "Step skipped" : result.getError().getMessage();
stepTestThreadLocal.get().skip(details);
}
}

private void handlePassedResult(Test test) {
if (stepTestThreadLocal.get() != null) {
if (isHookThreadLocal.get().equals(Boolean.TRUE)) {
boolean mediaLogs = test.getLogs().stream().anyMatch(l -> l.getMedia() != null);
if (!test.hasLog() && !mediaLogs) {
extentService.getExtentReports().removeTest(stepTestThreadLocal.get());
}
}
stepTestThreadLocal.get().pass("");
}
}

private synchronized void handleEmbed(EmbedEvent event) {

String mimeType = event.getMediaType();
Expand Down

0 comments on commit cf83111

Please sign in to comment.