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

Commit

Permalink
Add ability to fail test in case of JS warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Nov 27, 2015
1 parent 611e1ff commit b6b700f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
Expand Up @@ -22,6 +22,7 @@

import java.io.File;
import java.io.IOException;
import java.util.Optional;
import java.util.logging.Level;

import org.openqa.selenium.WebDriver;
Expand All @@ -47,6 +48,8 @@
import org.zanata.util.ScreenshotDirForTest;
import org.zanata.util.TestEventForScreenshotListener;

import javax.annotation.Nullable;

import static org.zanata.util.Constants.webDriverType;
import static org.zanata.util.Constants.webDriverWait;
import static org.zanata.util.Constants.zanataInstance;
Expand Down Expand Up @@ -112,29 +115,57 @@ public LogEntries getLogs(String type) {
* Logs all the outstanding WebDriver logs of the specified type.
* @param type a log type from org.openqa.selenium.logging.LogType
* (but they don't all seem to work)
* @throws WebDriverLogException exception containing the first warning/error message, if any
*/
public void logLogs(String type) {
private void logLogs(String type, boolean throwIfWarn) {
@Nullable
WebDriverLogException firstException = null;
String logName = WebDriverFactory.class.getName() + "." + type;
Logger log = LoggerFactory.getLogger(logName);
int logCount = 0;
for (LogEntry logEntry : getLogs(type)) {
++logCount;
if (logEntry.getLevel().intValue() >= Level.SEVERE.intValue()) {
log.error(logEntry.toString());
if (throwIfWarn && firstException == null) {
firstException = new WebDriverLogException(logEntry.getLevel(),
logEntry.toString());
}
} else if (logEntry.getLevel().intValue() >= Level.WARNING.intValue()) {
log.warn(logEntry.toString());
} else {
if (throwIfWarn && firstException == null) {
firstException = new WebDriverLogException(logEntry.getLevel(),
logEntry.toString());
}
} else if (logEntry.getLevel().intValue() >= Level.INFO.intValue()) {
log.info(logEntry.toString());
} else {
log.debug(logEntry.toString());
}
}
if (logCount == 0) {
log.info("no messages found for LogType.{}", type);
}
if (throwIfWarn && firstException != null) {
throw firstException;
}
}

/**
* Dump any outstanding browser logs to the main log.
*
* @throws WebDriverLogException exception containing the first warning/error message, if any
*/
public void logLogs() {
// TODO always throw, once we fix our tests
logLogs(false);
// logLogs(true);
}

@Deprecated
public void logLogs(boolean throwIfWarn) {
for (String type : getLogTypes()) {
logLogs(type);
logLogs(type, throwIfWarn);
}
}

Expand Down
@@ -0,0 +1,32 @@
/*
* Copyright 2015, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.page;

import java.util.logging.Level;

/**
* @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*/
public class WebDriverLogException extends RuntimeException {
public WebDriverLogException(Level logLevel, String logMessage) {
super(logLevel + ": " + logMessage);
}
}
Expand Up @@ -78,6 +78,7 @@ public void testEntry() {

@After
public void testExit() {
WebDriverFactory.INSTANCE.logLogs();
Duration duration = new Duration(testFunctionStart, new DateTime());
PeriodFormatter periodFormatter = new PeriodFormatterBuilder()
.appendLiteral("Finished "
Expand All @@ -91,7 +92,6 @@ public void testExit() {
.appendSuffix("ms")
.toFormatter();
log.info(periodFormatter.print(duration.toPeriod()));
WebDriverFactory.INSTANCE.logLogs();
}

}

0 comments on commit b6b700f

Please sign in to comment.