org.openqa.selenium.logging.Logs Java Examples

The following examples show how to use org.openqa.selenium.logging.Logs. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BrowserLogCleanningListenerTests.java    From vividus with Apache License 2.0 6 votes vote down vote up
@TestFactory
Stream<DynamicTest> testCleanBeforeNavigate()
{
    WebDriver driver = mock(WebDriver.class, withSettings().extraInterfaces(HasCapabilities.class));
    Consumer<Runnable> test = methodUnderTest ->
    {
        Options options = mock(Options.class);
        when(driver.manage()).thenReturn(options);
        Logs logs = mock(Logs.class);
        when(options.logs()).thenReturn(logs);
        when(logs.get(LogType.BROWSER)).thenReturn(mock(LogEntries.class));
        methodUnderTest.run();
    };
    return Stream.of(
            dynamicTest("beforeNavigateBack", () -> test.accept(() -> listener.beforeNavigateBack(driver))),
            dynamicTest("beforeNavigateForward", () -> test.accept(() -> listener.beforeNavigateForward(driver))),
            dynamicTest("beforeNavigateRefresh", () -> test.accept(() -> listener.beforeNavigateRefresh(driver))),
            dynamicTest("beforeNavigateTo", () -> test.accept(() -> listener.beforeNavigateTo("url", driver))));
}
 
Example #2
Source File: ConsoleTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@BuggyWebDriver
public void assertOnly() throws Exception {
    final String html
        = "<html>\n"
        + "<body>\n"
        + "<script>\n"
        + "  number = 1;\n"
        + "  console.assert(number % 2 === 0);\n"
        + "</script>\n"
        + "</body></html>";

    final WebDriver driver = loadPage2(html);

    final Logs logs = driver.manage().logs();
    final LogEntries logEntries = logs.get(LogType.BROWSER);
    final List<LogEntry> logEntryList = logEntries.getAll();

    assertEquals(1, logEntryList.size());

    final LogEntry logEntry = logEntryList.get(0);
    assertTrue(logEntry.getMessage(), logEntry.getMessage().contains("Assertion failed"));
}
 
Example #3
Source File: ConsoleTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@BuggyWebDriver
public void assertString() throws Exception {
    final String html
        = "<html>\n"
        + "<body>\n"
        + "<script>\n"
        + "  number = 1;\n"
        + "  console.assert(number % 2 === 0, 'the # is not even');\n"
        + "</script>\n"
        + "</body></html>";

    final WebDriver driver = loadPage2(html);

    final Logs logs = driver.manage().logs();
    final LogEntries logEntries = logs.get(LogType.BROWSER);
    final List<LogEntry> logEntryList = logEntries.getAll();

    assertEquals(1, logEntryList.size());

    final LogEntry logEntry = logEntryList.get(0);
    assertTrue(logEntry.getMessage(), logEntry.getMessage().contains("Assertion failed: the # is not even"));
}
 
Example #4
Source File: ConsoleTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@BuggyWebDriver
public void assertObject() throws Exception {
    final String html
        = "<html>\n"
        + "<body>\n"
        + "<script>\n"
        + "  number = 1;\n"
        + "  console.assert(number % 2 === 0, {number: number, errorMsg: 'the # is not even'});\n"
        + "</script>\n"
        + "</body></html>";

    final WebDriver driver = loadPage2(html);

    final Logs logs = driver.manage().logs();
    final LogEntries logEntries = logs.get(LogType.BROWSER);
    final List<LogEntry> logEntryList = logEntries.getAll();

    assertEquals(1, logEntryList.size());

    final LogEntry logEntry = logEntryList.get(0);
    assertTrue(logEntry.getMessage(), logEntry.getMessage()
            .contains("Assertion failed: ({number: 1.0, errorMsg: \"the # is not even\"})"));
}
 
Example #5
Source File: ConsoleTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@BuggyWebDriver
public void assertObjects() throws Exception {
    final String html
        = "<html>\n"
        + "<body>\n"
        + "<script>\n"
        + "  number = 1;\n"
        + "  console.assert(number % 2 === 0, {number: number}, {errorMsg: 'the # is not even'});\n"
        + "</script>\n"
        + "</body></html>";

    final WebDriver driver = loadPage2(html);

    final Logs logs = driver.manage().logs();
    final LogEntries logEntries = logs.get(LogType.BROWSER);
    final List<LogEntry> logEntryList = logEntries.getAll();

    assertEquals(1, logEntryList.size());

    final LogEntry logEntry = logEntryList.get(0);
    assertTrue(logEntry.getMessage(), logEntry.getMessage()
            .contains("Assertion failed: ({number: 1.0}) ({errorMsg: \"the # is not even\"})"));
}
 
Example #6
Source File: ConsoleTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@BuggyWebDriver
public void assertParams() throws Exception {
    final String html
        = "<html>\n"
        + "<body>\n"
        + "<script>\n"
        + "  console.assert(false, 'the word is %s', 'foo');\n"
        + "</script>\n"
        + "</body></html>";

    final WebDriver driver = loadPage2(html);

    final Logs logs = driver.manage().logs();
    final LogEntries logEntries = logs.get(LogType.BROWSER);
    final List<LogEntry> logEntryList = logEntries.getAll();

    assertEquals(1, logEntryList.size());

    final LogEntry logEntry = logEntryList.get(0);
    assertTrue(logEntry.getMessage(), logEntry.getMessage()
            .contains("Assertion failed: the word is foo"));
}
 
Example #7
Source File: WebDriverService.java    From cerberus-source with GNU General Public License v3.0 5 votes vote down vote up
@Override
public List<String> getSeleniumLog(Session session) {
    List<String> result = new ArrayList<>();
    Logs logs = session.getDriver().manage().logs();

    for (String logType : logs.getAvailableLogTypes()) {
        LogEntries logEntries = logs.get(logType);
        result.add("********************" + logType + "********************\n");
        for (LogEntry logEntry : logEntries) {
            result.add(new Date(logEntry.getTimestamp()) + " : " + logEntry.getLevel() + " : " + logEntry.getMessage() + "\n");
        }
    }

    return result;
}
 
Example #8
Source File: BrowserLogManagerTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
private static LogEntries mockLogRetrieval(WebDriver webDriver)
{
    Options options = mock(Options.class);
    when(webDriver.manage()).thenReturn(options);
    Logs logs = mock(Logs.class);
    when(options.logs()).thenReturn(logs);
    when(options.logs()).thenReturn(logs);
    LogEntry severeEntry = new LogEntry(Level.SEVERE, 1L, ERROR_MESSAGE);
    LogEntry infoEntry = new LogEntry(Level.INFO, 1L, ERROR_MESSAGE);
    LogEntries logEntries = new LogEntries(Arrays.asList(severeEntry, infoEntry));
    when(logs.get(LogType.BROWSER)).thenReturn(logEntries);
    return logEntries;
}
 
Example #9
Source File: SeleniumException.java    From gatf with Apache License 2.0 5 votes vote down vote up
public SeleniumException(WebDriver d, Throwable cause, SeleniumTest test) {
	super(cause);
	Map<String, SerializableLogEntries> lg = new HashMap<String, SerializableLogEntries>();
       Logs logs = d.manage().logs();
       for (String s : d.manage().logs().getAvailableLogTypes()) {
           LogEntries logEntries = logs.get(s);
           if(!logEntries.getAll().isEmpty()) {
               lg.put(s, new SerializableLogEntries(logEntries.getAll())); 
           }
       }
       List<LogEntry> entries = new ArrayList<LogEntry>();
       entries.add(new LogEntry(Level.ALL, new Date().getTime(), cause.getMessage()));
       entries.add(new LogEntry(Level.ALL, new Date().getTime(), ExceptionUtils.getStackTrace(cause)));
       lg.put("gatf", new SerializableLogEntries(entries));
}
 
Example #10
Source File: ConsoleTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@BuggyWebDriver
public void simpleString() throws Exception {
    final String html
        = "<html>\n"
        + "<body>\n"
        + "<script>\n"
        + "  for (i = 0; i < 4; i++) {\n"
        + "    console.log('test log ' + i);\n"
        + "  }\n"
        + "</script>\n"
        + "</body></html>";

    final WebDriver driver = loadPage2(html);

    final Logs logs = driver.manage().logs();
    final LogEntries logEntries = logs.get(LogType.BROWSER);
    final List<LogEntry> logEntryList = logEntries.getAll();

    final int count = logEntryList.size();
    assertTrue(count > 0);

    long timestamp = 0;
    for (int i = 0; i < 4; i++) {
        final LogEntry logEntry = logEntryList.get(i);
        assertTrue(logEntry.getMessage(), logEntry.getMessage().contains("test log " + i));
        assertTrue(logEntry.getTimestamp() >= timestamp);
        timestamp = logEntry.getTimestamp();
    }
}
 
Example #11
Source File: JsValidationStepsTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
private LogEntry mockGetLogEntry(String logErrorMessage)
{
    WebDriver webDriver = mock(WebDriver.class, withSettings().extraInterfaces(HasCapabilities.class));
    when(webDriverProvider.get()).thenReturn(webDriver);
    when(webDriver.getCurrentUrl()).thenReturn(URL);
    Options options = mock(Options.class);
    when(webDriver.manage()).thenReturn(options);
    Logs logs = mock(Logs.class);
    when(options.logs()).thenReturn(logs);
    LogEntry severeEntry = new LogEntry(Level.SEVERE, 1L, logErrorMessage);
    LogEntry infoEntry = new LogEntry(Level.INFO, 1L, logErrorMessage);
    LogEntries logEntries = new LogEntries(Arrays.asList(severeEntry, infoEntry));
    when(logs.get(LogType.BROWSER)).thenReturn(logEntries);
    return severeEntry;
}
 
Example #12
Source File: JavaDriver.java    From marathonv5 with Apache License 2.0 4 votes vote down vote up
public void clearlogs(String logType) {
    Logs logs = manage().logs();
    logs.get(logType);
}
 
Example #13
Source File: AllureSelenideTest.java    From allure-java with Apache License 2.0 4 votes vote down vote up
@AllureFeatures.Attachments
@Test
void shouldSaveLogs() {
    final LogEntry logEntry = new LogEntry(Level.ALL, 10, "SIMPLE LOG");
    final LogEntries logEntries = new LogEntries(Collections.singletonList(logEntry));
    final ChromeDriver wdMock = mock(ChromeDriver.class);
    final Logs logsMock = mock(Logs.class);
    final Options optionsMock = mock(Options.class);

    WebDriverRunner.setWebDriver(wdMock);

    doReturn(optionsMock).when(wdMock).manage();
    doReturn(logsMock).when(optionsMock).logs();
    doReturn(logEntries).when(logsMock).get(LogType.BROWSER.toString());

    final AllureResults results = runWithinTestContext(() -> {
        final AllureSelenide selenide = new AllureSelenide()
                .enableLogs(LogType.BROWSER, Level.ALL)
                .savePageSource(false)
                .screenshots(false);

        SelenideLogger.addListener(UUID.randomUUID().toString(), selenide);

        final SelenideLog log = SelenideLogger.beginStep(
            "dummy source",
            "dummyMethod()",
            "param1",
            "param2");

        SelenideLogger.commitStep(log, new Exception("something went wrong"));
    });

    final StepResult selenideStep = extractStepFromResults(results);
    final Attachment attachment = selenideStep.getAttachments().iterator().next();
    final String attachmentContent = new String(
        results.getAttachments().get(attachment.getSource()),
        StandardCharsets.UTF_8
    );

    assertThat(selenideStep.getAttachments()).hasSize(1);
    assertThat(results.getAttachments()).containsKey(attachment.getSource());
    assertThat(attachmentContent).isEqualTo(logEntry.toString());
}
 
Example #14
Source File: DeviceWebDriver.java    From xframium-java with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Logs logs()
{
    return options.logs();
}
 
Example #15
Source File: EmptyWebDriver.java    From java-client with Apache License 2.0 4 votes vote down vote up
@Override public Logs logs() {
    return null;
}
 
Example #16
Source File: EventFiringWebDriver.java    From selenium with Apache License 2.0 4 votes vote down vote up
@Override
public Logs logs() {
  return options.logs();
}
 
Example #17
Source File: RemoteWebDriver.java    From selenium with Apache License 2.0 4 votes vote down vote up
@Override
@Beta
public Logs logs() {
  return remoteLogs;
}
 
Example #18
Source File: StubDriver.java    From selenium with Apache License 2.0 4 votes vote down vote up
public Logs logs() {
  throw new UnsupportedOperationException("logs");
}
 
Example #19
Source File: WebDriver.java    From java-client with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the {@link Logs} interface used to fetch different types of logs.
 *
 * <p>To set the logging preferences {@link LoggingPreferences}.
 *
 * @return A Logs interface.
 */
@Beta
Logs logs();
 
Example #20
Source File: WebDriver.java    From selenium with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the {@link Logs} interface used to fetch different types of logs.
 * <p>
 * To set the logging preferences {@link LoggingPreferences}.
 *
 * @return A Logs interface.
 */
@Beta
Logs logs();