Skip to content

Commit

Permalink
Merge pull request #2 from reportportal/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
HardNorth authored Jun 8, 2022
2 parents ffdf7b9 + 40a0a61 commit 5e69142
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

## [Unreleased]
### Changed
- Selenium log types are now `text/plain`, by @HardNorth
### Removed
- Stack Trace logging in nested steps on failure, since it should be logged on higher level, by @HardNorth

## [5.1.0]
### Added
Expand Down
16 changes: 16 additions & 0 deletions README_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,19 @@ dependencies {
```

### Selenide configuration

To start getting Selenide step logging in Report Portal you need to add the logger to `SelenideLogger` listeners. The best
place for it is one which will be initialized at the earliest moment once during the test execution. E.G. a static initialization block in a
base class for all tests:

```java
public class BaseTest {
static {
SelenideLogger.addListener("ReportPortal logger", new ReportPortalSelenideEventListener());
}
}
```

### Logger configuration

TBD
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.epam.reportportal.service.Launch;
import com.epam.reportportal.service.ReportPortal;
import com.google.common.io.ByteSource;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriverException;
Expand All @@ -50,7 +49,7 @@ public class ReportPortalSelenideEventListener implements LogEventListener {
private static final String SELENIUM_LOG_MESSAGE_PATTERN = "WebDriver logs of '%s' type";
private static final String SELENIUM_SCREENSHOT_TYPE = "image/png";
private static final String SELENIUM_PAGE_SOURCE_TYPE = "text/html";
private static final String SELENIUM_LOG_TYPE = "application/json";
private static final String SELENIUM_LOG_TYPE = "text/plain";

private final String logLevel;

Expand Down Expand Up @@ -166,10 +165,6 @@ public void afterEvent(@Nonnull LogEvent currentLog) {
return;
}
if (LogEvent.EventStatus.FAIL.equals(currentLog.getStatus())) {
ReportPortal.emitLog(ExceptionUtils.getStackTrace(currentLog.getError()),
LogLevel.ERROR.name(),
Calendar.getInstance().getTime()
);
if (screenshots) {
logScreenshot();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.epam.reportportal.selenide;

import com.codeborne.selenide.Selenide;
import com.codeborne.selenide.WebDriverRunner;
import com.codeborne.selenide.logevents.LogEvent;
import com.codeborne.selenide.logevents.SelenideLog;
Expand All @@ -10,21 +11,23 @@
import com.epam.reportportal.service.step.StepReporter;
import com.epam.reportportal.utils.files.Utils;
import com.epam.ta.reportportal.ws.model.log.SaveLogRQ;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.logging.Level;

import static java.util.Optional.ofNullable;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -35,11 +38,11 @@

public class ReportPortalSelenideEventListenerTest {

private static final String BROWSER_LOG = "[2022-06-08T15:34:59.131Z] [SEVERE] https://www.example.com/favicon.ico - Failed to load resource: the server responded with a status of 404 ()";

private static final String SELENIDE_LOG_STRING = "$(\"By.xpath: //*[text()=\"Login with Google\"]\") should have(visible)";
private static final String IMAGE = "pug/lucky.jpg";
private static final String PAGE = "html/rp_login_page.html";
private static final Throwable TEST_ERROR = new RuntimeException("error");
private static final String TEST_STACK_TRACE = ExceptionUtils.getStackTrace(TEST_ERROR);

@Mock
private Launch launch;
Expand Down Expand Up @@ -111,9 +114,9 @@ private byte[] getResource(String imagePath) {
public void test_step_logging_failed() {
LogEvent logEvent = mock(SelenideLog.class);
when(logEvent.toString()).thenReturn(SELENIDE_LOG_STRING);
RemoteWebDriver webDriver = mock(RemoteWebDriver.class);
byte[] image = getResource(IMAGE);
String page = new String(getResource(PAGE), StandardCharsets.UTF_8);
RemoteWebDriver webDriver = mock(RemoteWebDriver.class);
when(webDriver.getScreenshotAs(eq(OutputType.BYTES))).thenReturn(image);
when(webDriver.getPageSource()).thenReturn(page);

Expand All @@ -128,24 +131,19 @@ public void test_step_logging_failed() {
verify(stepReporter).sendStep(eq(ItemStatus.INFO), eq(SELENIDE_LOG_STRING));

when(logEvent.getStatus()).thenReturn(LogEvent.EventStatus.FAIL);
when(logEvent.getError()).thenReturn(TEST_ERROR);

List<Function<String, SaveLogRQ>> logs = runEventCapture(listener::afterEvent, logEvent);
verify(stepReporter).finishPreviousStep(eq(ItemStatus.FAILED));
assertThat(logs, hasSize(3));

SaveLogRQ stackTraceLog = logs.get(0).apply("test");
assertThat(stackTraceLog.getMessage(), equalTo(TEST_STACK_TRACE));
assertThat(stackTraceLog.getLevel(), equalTo(LogLevel.ERROR.name()));
assertThat(logs, hasSize(2));

SaveLogRQ screenshotLog = logs.get(1).apply("test");
SaveLogRQ screenshotLog = logs.get(0).apply("test");
assertThat(screenshotLog.getLevel(), equalTo(LogLevel.INFO.name()));
assertThat(screenshotLog.getFile(), notNullValue());
SaveLogRQ.File file = screenshotLog.getFile();
assertThat(file.getContent(), equalTo(image));
assertThat(file.getContentType(), equalTo("image/png"));

SaveLogRQ pageLog = logs.get(2).apply("test");
SaveLogRQ pageLog = logs.get(1).apply("test");
assertThat(pageLog.getLevel(), equalTo(LogLevel.INFO.name()));
assertThat(pageLog.getFile(), notNullValue());
file = pageLog.getFile();
Expand All @@ -165,15 +163,10 @@ public void test_step_logging_failed_no_screenshots_sources() {
verify(stepReporter).sendStep(eq(ItemStatus.INFO), eq(SELENIDE_LOG_STRING));

when(logEvent.getStatus()).thenReturn(LogEvent.EventStatus.FAIL);
when(logEvent.getError()).thenReturn(TEST_ERROR);

List<Function<String, SaveLogRQ>> logs = runEventCapture(listener::afterEvent, logEvent);
runEvent(listener::afterEvent, logEvent);
verify(stepReporter).finishPreviousStep(eq(ItemStatus.FAILED));
assertThat(logs, hasSize(1));

SaveLogRQ stackTraceLog = logs.get(0).apply("test");
assertThat(stackTraceLog.getMessage(), equalTo(TEST_STACK_TRACE));
assertThat(stackTraceLog.getLevel(), equalTo(LogLevel.ERROR.name()));
verify(context, noInteractions()).emit(any(Function.class));
}

@Test
Expand All @@ -197,4 +190,41 @@ public void test_step_logging_invalid_status() {
assertThat(warningLog.getLevel(), equalTo(LogLevel.WARN.name()));
assertThat(warningLog.getFile(), nullValue());
}

@Test
@SuppressWarnings({ "ResultOfMethodCallIgnored", "unchecked" })
public void test_step_logging_failed_browser_logs() {
String logType = LogType.BROWSER;
Level logLevel = Level.FINER;
LogEvent logEvent = mock(SelenideLog.class);
when(logEvent.toString()).thenReturn(SELENIDE_LOG_STRING);
ReportPortalSelenideEventListener listener = new ReportPortalSelenideEventListener().logScreenshots(false)
.logPageSources(false)
.enableSeleniumLogs(logType, logLevel);
runEvent(listener::beforeEvent, logEvent);
verify(context, noInteractions()).emit(any(Function.class));
verify(stepReporter).sendStep(eq(ItemStatus.INFO), eq(SELENIDE_LOG_STRING));

when(logEvent.getStatus()).thenReturn(LogEvent.EventStatus.FAIL);

RemoteWebDriver webDriver = mock(RemoteWebDriver.class);
try (MockedStatic<WebDriverRunner> driverMockedStatic = Mockito.mockStatic(WebDriverRunner.class)) {
driverMockedStatic.when(WebDriverRunner::hasWebDriverStarted).thenReturn(true);
driverMockedStatic.when(WebDriverRunner::getWebDriver).thenReturn(webDriver);

try (MockedStatic<Selenide> selenideMockedStatic = Mockito.mockStatic(Selenide.class)) {
selenideMockedStatic.when(() -> Selenide.getWebDriverLogs(same(logType), same(logLevel)))
.thenReturn(Collections.singletonList(BROWSER_LOG));
List<Function<String, SaveLogRQ>> logRequests = runEventCapture(listener::afterEvent, logEvent);
verify(stepReporter).finishPreviousStep(eq(ItemStatus.FAILED));
assertThat(logRequests, hasSize(1));

SaveLogRQ browserLog = logRequests.get(0).apply("test");
assertThat(browserLog.getMessage(), equalTo("WebDriver logs of 'browser' type"));
assertThat(browserLog.getLevel(), equalTo(LogLevel.INFO.name()));
assertThat(browserLog.getFile().getContentType(), equalTo("text/plain"));
assertThat(browserLog.getFile().getContent(), equalTo(BROWSER_LOG.getBytes(StandardCharsets.UTF_8)));
}
}
}
}

0 comments on commit 5e69142

Please sign in to comment.