Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Ignore certain JS warnings; improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Feb 2, 2016
1 parent 527524b commit 2f72dd3
Showing 1 changed file with 43 additions and 8 deletions.
Expand Up @@ -22,7 +22,8 @@

import java.io.File;
import java.io.IOException;
import java.util.Optional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;

import org.openqa.selenium.WebDriver;
Expand Down Expand Up @@ -58,11 +59,29 @@
public enum WebDriverFactory {
INSTANCE;

private static final ThreadLocal<SimpleDateFormat> TIME_FORMAT =
new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("HH:mm:ss.SSS");
}
};

private volatile EventFiringWebDriver driver = createDriver();
private DriverService driverService;
private TestEventForScreenshotListener eventListener;
private int webdriverWait;

private final String[] ignoredLogPatterns = {
".*/org.richfaces/jquery.js .* " +
"'webkit(?:Cancel)?RequestAnimationFrame' is vendor-specific. " +
"Please use the standard " +
"'(?:request|cancel)AnimationFrame' instead.",
".*/org.richfaces/jquery.js .* " +
"'webkitMovement[XY]' is deprecated. " +
"Please use 'movement[XY]' instead.",
};

public WebDriver getDriver() {
return driver;
}
Expand Down Expand Up @@ -111,6 +130,21 @@ public LogEntries getLogs(String type) {
return getDriver().manage().logs().get(type);
}

private String toString(LogEntry logEntry) {
String time =
TIME_FORMAT.get().format(new Date(logEntry.getTimestamp()));
return time + " " + logEntry.getMessage();
}

private boolean ignorable(String message) {
for (String ignorable : ignoredLogPatterns) {
if (message.matches(ignorable)) {
return true;
}
}
return false;
}

/**
* Logs all the outstanding WebDriver logs of the specified type.
* @param type a log type from org.openqa.selenium.logging.LogType
Expand All @@ -125,22 +159,23 @@ private void logLogs(String type, boolean throwIfWarn) {
int logCount = 0;
for (LogEntry logEntry : getLogs(type)) {
++logCount;
String msg = logEntry.getMessage();
if (logEntry.getLevel().intValue() >= Level.SEVERE.intValue()) {
log.error(logEntry.toString());
log.error(toString(logEntry));
if (firstException == null || !firstException.isErrorLog()) {
firstException = new WebDriverLogException(logEntry.getLevel(),
logEntry.toString());
toString(logEntry));
}
} else if (logEntry.getLevel().intValue() >= Level.WARNING.intValue()) {
log.warn(logEntry.toString());
if (throwIfWarn && firstException == null) {
log.warn(toString(logEntry));
if (throwIfWarn && firstException == null && !ignorable(msg)) {
firstException = new WebDriverLogException(logEntry.getLevel(),
logEntry.toString());
toString(logEntry));
}
} else if (logEntry.getLevel().intValue() >= Level.INFO.intValue()) {
log.info(logEntry.toString());
log.info(toString(logEntry));
} else {
log.debug(logEntry.toString());
log.debug(toString(logEntry));
}
}
if (logCount == 0) {
Expand Down

0 comments on commit 2f72dd3

Please sign in to comment.