Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Jun 8, 2022
1 parent 6a4e2b2 commit 4b2ba63
Showing 1 changed file with 49 additions and 19 deletions.
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 4b2ba63

Please sign in to comment.