Created
February 18, 2021 10:58
-
-
Save asolntsev/ef193d8e19afaa1cc5b447ed0e1f3589 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.rules.TestWatcher; | |
import org.junit.runner.Description; | |
import org.openqa.selenium.logging.LogEntries; | |
import org.openqa.selenium.logging.LogEntry; | |
import org.openqa.selenium.logging.LogType; | |
import org.openqa.selenium.logging.Logs; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.util.Date; | |
import static com.codeborne.selenide.WebDriverRunner.getWebDriver; | |
public class LogBrowserLogs extends TestWatcher { | |
private static final Logger logger = LoggerFactory.getLogger(LogBrowserLogs.class); | |
@Override | |
protected void starting(Description description) { | |
logger.info("starting {}", description); | |
} | |
@Override | |
protected void succeeded(Description description) { | |
logger.info("succeeded {}", description); | |
} | |
@Override | |
protected void failed(Throwable e, Description description) { | |
logger.info("failed {}", description); | |
} | |
@Override | |
protected void finished(Description description) { | |
try { | |
Logs logs = getWebDriver().manage().logs(); | |
printLogs(logs.get(LogType.BROWSER)); | |
} | |
catch (RuntimeException e) { | |
logger.error("Failed to print browser logs", e); | |
} | |
} | |
private void printLogs(LogEntries entries) { | |
logger.info("{} log entries found", entries.getAll().size()); | |
for (LogEntry entry : entries) { | |
logger.info("{} {} {}", | |
new Date(entry.getTimestamp()), entry.getLevel(), entry.getMessage() | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment