use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class Selection2Test method test.
private void test(final String action, final String x, final String alert) throws Exception {
final String html = "<html>\n" + "<body onload='test()'>\n" + " <span id='s1'>abc</span><span id='s2'>xyz</span><span id='s3'>foo</span>\n" + " <input type='button' id='b' onclick=\"" + action + ";test();\" value='click'></input>\n" + "<script>\n" + " var selection = document.selection; // IE\n" + " if(!selection) selection = window.getSelection(); // FF\n" + " var s1 = document.getElementById('s1');\n" + " var s2 = document.getElementById('s2');\n" + " function test() {\n" + " try {\n" + " var x = " + x + ";\n" + " alert(" + alert + ");\n" + " } catch (e) {\n" + " alert('unsupported action');\n" + " }\n" + " }\n" + "</script>\n" + "</body></html>";
final WebClient client = getWebClient();
final MockWebConnection conn = new MockWebConnection();
conn.setDefaultResponse(html);
client.setWebConnection(conn);
final List<String> actual = new ArrayList<>();
final AlertHandler alertHandler = new CollectingAlertHandler(actual);
client.setAlertHandler(alertHandler);
final HtmlPage page = client.getPage(URL_FIRST);
final DomNode s1Text = page.getHtmlElementById("s1").getFirstChild();
final DomNode s2Text = page.getHtmlElementById("s2").getFirstChild();
final HtmlInput input = page.getHtmlElementById("b");
final org.w3c.dom.ranges.Range range = new SimpleRange();
range.setStart(s1Text, 2);
range.setEnd(s2Text, 1);
page.setSelectionRange(range);
input.click();
assertEquals(getExpectedAlerts(), actual);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class FalsifyingWebConnectionTest method blockSomeRequests.
/**
* @throws Exception if the test fails
*/
@Test
public void blockSomeRequests() throws Exception {
final WebClient webClient = getWebClient();
final String html = "<html><head>\n" + "<script src='http://www.google-analytics.com/ga.js'></script>\n" + "<script src='myJs.js'></script>\n" + "</head><body>\n" + "hello world!" + "<body></html>";
final MockWebConnection mockConnection = new MockWebConnection();
mockConnection.setResponse(URL_FIRST, html);
mockConnection.setResponse(new URL(URL_FIRST, "myJs.js"), "alert('hello');");
webClient.setWebConnection(mockConnection);
final List<String> collectedAlerts = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
// c'tor configures connection on the web client
try (FalsifyingWebConnection connection = new FalsifyingWebConnection(webClient) {
@Override
public WebResponse getResponse(final WebRequest request) throws IOException {
if ("www.google-analytics.com".equals(request.getUrl().getHost())) {
// -> empty script
return createWebResponse(request, "", MimeType.APPLICATION_JAVASCRIPT);
}
return super.getResponse(request);
}
}) {
webClient.getPage(URL_FIRST);
assertEquals(2, mockConnection.getRequestCount());
final String[] expectedAlerts = { "hello" };
assertEquals(expectedAlerts, collectedAlerts);
}
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class FalsifyingWebConnectionTest method simulateHttpError.
/**
* @throws Exception if the test fails
*/
@Test
public void simulateHttpError() throws Exception {
final WebClient webClient = getWebClient();
final String html = "<html><head>\n" + "<script src='myJs.js'></script>\n" + "</head><body>\n" + "hello world!" + "<body></html>";
final MockWebConnection mockConnection = new MockWebConnection();
mockConnection.setResponse(URL_FIRST, html);
mockConnection.setResponse(new URL(URL_FIRST, "myJs.js"), "alert('hello');");
webClient.setWebConnection(mockConnection);
final List<String> collectedAlerts = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
// first test this "site" when everything is ok
webClient.getPage(URL_FIRST);
final String[] expectedAlerts = { "hello" };
assertEquals(expectedAlerts, collectedAlerts);
// c'tor configures connection on the web client
try (FalsifyingWebConnection connection = new FalsifyingWebConnection(webClient) {
@Override
public WebResponse getResponse(final WebRequest request) throws IOException {
if (request.getUrl().getPath().endsWith(".js")) {
return createWebResponse(request, "", MimeType.TEXT_HTML, 500, "Application Error");
}
return super.getResponse(request);
}
}) {
try {
webClient.getPage(URL_FIRST);
fail("HTTP Exception expected!");
} catch (final FailingHttpStatusCodeException e) {
// that's fine
}
}
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlFormTest method submit_javascriptActionLeadingWhitespace.
/**
* @throws Exception if the test fails
*/
@Test
public void submit_javascriptActionLeadingWhitespace() throws Exception {
final String firstHtml = "<html><head><title>First</title></head><body>\n" + "<form method='get' action=' javascript:alert(\"clicked\")'>\n" + "<input name='button' type='submit' value='PushMe' id='button'/></form>\n" + "</body></html>";
final String secondHtml = "<html><head><title>Second</title></head><body></body></html>";
final WebClient client = getWebClientWithMockWebConnection();
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = getMockWebConnection();
webConnection.setResponse(URL_FIRST, firstHtml);
webConnection.setResponse(URL_SECOND, secondHtml);
final HtmlPage firstPage = client.getPage(URL_FIRST);
final HtmlSubmitInput button = firstPage.getHtmlElementById("button");
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
final HtmlPage secondPage = button.click();
assertEquals(firstPage.getTitleText(), secondPage.getTitleText());
assertEquals(new String[] { "clicked" }, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlFormTest method submit_onSubmitHandler_preventDefaultOnly.
/**
* Testcase for 3072010.
*
* @throws Exception if the test fails
*/
@Test
@Alerts("clicked")
public void submit_onSubmitHandler_preventDefaultOnly() throws Exception {
final String firstHtml = "<html><body>\n" + "<form method='post' action='/foo' >\n" + "<input type='submit' id='button'/>\n" + "</form>\n" + "<script>\n" + "function foo(e) {\n" + " alert('clicked');\n" + " e.returnValue = false;\n" + " if (e.preventDefault) {\n" + " e.preventDefault();\n" + " }\n" + "}\n" + "var oForm = document.forms[0];\n" + "oForm.addEventListener('submit', foo, false);\n" + "</script>\n" + "</body></html>";
final WebClient client = getWebClientWithMockWebConnection();
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
getMockWebConnection().setResponse(URL_FIRST, firstHtml);
getMockWebConnection().setDefaultResponse("");
final HtmlPage firstPage = client.getPage(URL_FIRST);
final HtmlSubmitInput button = firstPage.getHtmlElementById("button");
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
final HtmlPage secondPage = button.click();
assertSame(firstPage, secondPage);
assertEquals(getExpectedAlerts(), collectedAlerts);
}
Aggregations