use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class XMLHttpRequest3Test method ajaxInfluencesSubmitHeaders.
/**
* Test for a strange error we found: An ajax running
* in parallel shares the additional headers with a form
* submit.
*
* @throws Exception if an error occurs
*/
@Test
public void ajaxInfluencesSubmitHeaders() throws Exception {
final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
servlets.put("/content.html", ContentServlet.class);
servlets.put("/ajax_headers.html", AjaxHeaderServlet.class);
servlets.put("/form_headers.html", FormHeaderServlet.class);
startWebServer("./", null, servlets);
collectedHeaders_.clear();
XMLHttpRequest3Test.STATE_ = 0;
final WebClient client = getWebClient();
final List<String> collectedAlerts = Collections.synchronizedList(new ArrayList<String>());
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final HtmlPage page = client.getPage(URL_FIRST + "content.html");
final DomElement elem = page.getElementById("doIt");
while (STATE_ < 1) {
Thread.sleep(42);
}
((HtmlSubmitInput) elem).click();
client.waitForBackgroundJavaScript(DEFAULT_WAIT_TIME);
assertEquals(collectedHeaders_.toString(), 2, collectedHeaders_.size());
String headers = collectedHeaders_.get(0);
if (!headers.startsWith("Form: ")) {
headers = collectedHeaders_.get(1);
}
assertTrue(headers, headers.startsWith("Form: "));
assertFalse(headers, headers.contains("Html-Unit=is great,;"));
headers = collectedHeaders_.get(0);
if (!headers.startsWith("Ajax: ")) {
headers = collectedHeaders_.get(1);
}
assertTrue(headers, headers.startsWith("Ajax: "));
assertTrue(headers, headers.contains("Html-Unit=is great,;"));
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class XMLHttpRequest3Test method asyncUseWithNetworkConnectionFailure.
/**
* Tests asynchronous use of XMLHttpRequest, where the XHR request fails due to IOException (Connection refused).
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = { "0", "1", "4", MSG_NO_CONTENT, MSG_PROCESSING_ERROR }, IE = { "0", "1", "1", "4", MSG_NO_CONTENT, MSG_PROCESSING_ERROR })
public void asyncUseWithNetworkConnectionFailure() throws Exception {
final String html = "<html>\n" + "<head>\n" + "<title>XMLHttpRequest Test</title>\n" + "<script>\n" + "var request;\n" + "function testAsync() {\n" + " request = new XMLHttpRequest();\n" + " request.onreadystatechange = onReadyStateChange;\n" + " request.onerror = onError;\n" + " alert(request.readyState);\n" + " request.open('GET', '" + URL_SECOND + "', true);\n" + " request.send('');\n" + "}\n" + "function onError() {\n" + " alert('" + MSG_PROCESSING_ERROR + "');\n" + "}\n" + "function onReadyStateChange() {\n" + " alert(request.readyState);\n" + " if (request.readyState == 4) {\n" + " if (request.responseText == null)\n" + " alert('" + MSG_NO_CONTENT + "');\n" + " else\n" + " throw 'Unexpected content, should be zero length but is: \"' + request.responseText + '\"';\n" + " }\n" + "}\n" + "</script>\n" + "</head>\n" + "<body onload='testAsync()'>\n" + "</body>\n" + "</html>";
final WebClient client = getWebClient();
final List<String> collectedAlerts = Collections.synchronizedList(new ArrayList<String>());
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection conn = new DisconnectedMockWebConnection();
conn.setResponse(URL_FIRST, html);
client.setWebConnection(conn);
client.getPage(URL_FIRST);
assertEquals(0, client.waitForBackgroundJavaScriptStartingBefore(1000));
assertEquals(getExpectedAlerts(), collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class XMLHttpRequest3Test method thisValueInHandler.
/**
* Tests the value of "this" in handler.
* @throws Exception if the test fails
*/
@Test
@Alerts("this == request")
public void thisValueInHandler() throws Exception {
final String html = "<html>\n" + " <head>\n" + " <title>XMLHttpRequest Test</title>\n" + " <script>\n" + " var request;\n" + " function testAsync() {\n" + " request = new XMLHttpRequest();\n" + " request.onreadystatechange = onReadyStateChange;\n" + " request.open('GET', 'foo.xml', true);\n" + " request.send('');\n" + " }\n" + " function onReadyStateChange() {\n" + " if (request.readyState == 4) {\n" + " if (this == request)\n" + " alert('this == request');\n" + " else if (this == onReadyStateChange)\n" + " alert('this == handler');\n" + " else alert('not expected: ' + this)\n" + " }\n" + " }\n" + " </script>\n" + " </head>\n" + " <body onload='testAsync()'>\n" + " </body>\n" + "</html>";
final WebClient client = getWebClient();
final List<String> collectedAlerts = Collections.synchronizedList(new ArrayList<String>());
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection conn = new MockWebConnection();
conn.setResponse(URL_FIRST, html);
conn.setDefaultResponse("");
client.setWebConnection(conn);
client.getPage(URL_FIRST);
assertEquals(0, client.waitForBackgroundJavaScriptStartingBefore(1000));
assertEquals(getExpectedAlerts(), collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project maven-doxia-sitetools by apache.
the class JavascriptVerifier method verify.
/**
* Verifies a HtmlPage.
*
* @param file the file to verify.
*
* @throws Exception if something goes wrong.
*/
public void verify(String file) throws Exception {
File jsTest = getTestFile("target/output/javascript.html");
assertNotNull(jsTest);
assertTrue(jsTest.exists());
// HtmlUnit
try (WebClient webClient = new WebClient()) {
webClient.getOptions().setCssEnabled(false);
final List<String> collectedAlerts = new ArrayList<String>(4);
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
HtmlPage page = (HtmlPage) webClient.getPage(jsTest.toURI().toURL());
assertNotNull(page);
HtmlElement element = page.getHtmlElementById("contentBox");
assertNotNull(element);
HtmlDivision division = (HtmlDivision) element;
assertNotNull(division);
Iterator<HtmlElement> elementIterator = division.getHtmlElementDescendants().iterator();
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
HtmlSection section = (HtmlSection) elementIterator.next();
assertNotNull(section);
HtmlHeading2 h2 = (HtmlHeading2) elementIterator.next();
assertNotNull(h2);
assertEquals("Test", h2.asText().trim());
HtmlAnchor a = (HtmlAnchor) elementIterator.next();
assertNotNull(a);
assertEquals("Test", a.getAttribute("name"));
HtmlParagraph p = (HtmlParagraph) elementIterator.next();
assertNotNull(p);
assertEquals("You should see a JavaScript alert...", p.asText().trim());
HtmlScript script = (HtmlScript) elementIterator.next();
assertNotNull(script);
assertEquals("text/javascript", script.getAttribute("type"));
assertEquals("", script.asText().trim());
List<String> expectedAlerts = Collections.singletonList("Hello!");
assertEquals(expectedAlerts, collectedAlerts);
}
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class WindowConcurrencyTest method setTimeoutStopped.
/**
* Test that a script started by a timer is stopped if the page that started it
* is not loaded anymore.
* @throws Exception if the test fails
*/
@Test
public void setTimeoutStopped() throws Exception {
final String firstContent = "<html><head>\n" + "<script language='JavaScript'>window.setTimeout('alert(\"Yo!\")', 10000);</script>\n" + "</head><body onload='document.location.replace(\"" + URL_SECOND + "\")'></body></html>";
final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
final List<String> collectedAlerts = Collections.synchronizedList(new ArrayList<String>());
client_.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setResponse(URL_FIRST, firstContent);
webConnection.setResponse(URL_SECOND, secondContent);
client_.setWebConnection(webConnection);
final HtmlPage page = client_.getPage(URL_FIRST);
assertEquals(0, page.getWebClient().waitForBackgroundJavaScript(2000));
assertEquals("Second", page.getTitleText());
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
}
Aggregations