From 4b2ba634fe35f2db2e51e437a7b2e3a6353e336b Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Wed, 8 Jun 2022 20:15:30 +0300 Subject: [PATCH] Tests --- ...ReportPortalSelenideEventListenerTest.java | 68 +++++++++++++------ 1 file changed, 49 insertions(+), 19 deletions(-) diff --git a/src/test/java/com/epam/reportportal/selenide/ReportPortalSelenideEventListenerTest.java b/src/test/java/com/epam/reportportal/selenide/ReportPortalSelenideEventListenerTest.java index dff15ff..930b034 100644 --- a/src/test/java/com/epam/reportportal/selenide/ReportPortalSelenideEventListenerTest.java +++ b/src/test/java/com/epam/reportportal/selenide/ReportPortalSelenideEventListenerTest.java @@ -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; @@ -10,7 +11,6 @@ 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; @@ -18,13 +18,16 @@ 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; @@ -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; @@ -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); @@ -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> 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(); @@ -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> 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 @@ -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 driverMockedStatic = Mockito.mockStatic(WebDriverRunner.class)) { + driverMockedStatic.when(WebDriverRunner::hasWebDriverStarted).thenReturn(true); + driverMockedStatic.when(WebDriverRunner::getWebDriver).thenReturn(webDriver); + + try (MockedStatic selenideMockedStatic = Mockito.mockStatic(Selenide.class)) { + selenideMockedStatic.when(() -> Selenide.getWebDriverLogs(same(logType), same(logLevel))) + .thenReturn(Collections.singletonList(BROWSER_LOG)); + List> 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))); + } + } + } }