There's currently a WebDriverTestCase.loadPageWithAlerts2(String html) that tests behaviour of the passed html string for against expected alert()s.
This feature request is for a similar method, say WebDriverTestCase.loadPageWithJsLogs(Class<?> testClass, String resourceFileName), that tests behaviour of specified the html resource file against expected console.log()s.
Possible structure of test files is as follows:
src/test/java/com/gargoylesoftware/htmlunit/javascript/SomeEngineTest.javasrc/test/resources/WebDriverTestCase/javascript/SomeEngineTest/someBehaviour.htmlconsole.log() instead of alert().src/test/java/com/gargoylesoftware/htmlunit/javascript/SomeEngineTest.javapackage com.gargoylesoftware.htmlunit.javascript;
@RunWith(BrowserRunner.class)
public class SomeEngineTest {
@Test
@JsLogs({"foo", "bar"})
public void someBehaviour() throws Exception {
loadPageWithJsLogs("someBehaviour.html");
}
}
src/test/resources/WebDriverTestCase/javascript/SomeEngineTest/someBehaviour.html<!DOCTYPE html>
<html>
<head>
<script>
// "onload" property of <body> also attaches to this window
// see https://www.w3.org/TR/uievents/#event-type-load
window.addEventListener("load", function () { console.log('foo') })
</script>
</head>
<body onload="console.log('bar')">
</body>
</html>
protected final WebDriver loadPageWithJsLogs(String resourceFileName) {
// testClass is the implementing test class
return loadPageWithJsLogs(getClass(), resourceFileName);
}
protected final WebDriver loadPageWithJsLogs(Class<?> testClass, String resourceFileName) {
String testClassName = testClass.getName();
// This removes the excess "com.gargoylesoftware.htmlunit." prefix
String commonPrefix = StringUtils.getCommonPrefix(WebDriverTestCase.class.getName(), testClassName);
if (!commonPrefix.isEmpty()) {
testClassName = StringUtils.removeStart(testClassName, commonPrefix);
}
// Method classes get a '$' delimiter, change them to '.' (why? not strictly necessary)
String path = WebDriverTestCase.class.getSimpleName() + "/" + testClassName.replace('.', '/').replace('$', '.');
try (InputStream in = WebDriverTestCase.class.getClassLoader().getResourceAsStream(path)) {
String html = ...;
}
// take html and perform voodoo, use WebConsoleLogCapturer and capture console.log(), send WebConsoleLogCapturer.captured() to implemetation of @JsLogs
}
WebConsoleLogCapturer attached. ("INFO: " prefixing could be removed for beverity.)
e.g.
Last edit: Atsushi Nakagawa 2018-08-14