use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlFormTest method submit_javascriptAction_javascriptDisabled.
/**
* @throws Exception if the test fails
*/
@Test
public void submit_javascriptAction_javascriptDisabled() throws Exception {
final String html = "<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 WebClient client = getWebClientWithMockWebConnection();
client.getOptions().setJavaScriptEnabled(false);
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = getMockWebConnection();
webConnection.setResponse(URL_FIRST, html);
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);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlFormTest method submitWithOnClickThatReturnsFalse.
/**
* Regression test for bug #536.
* @throws Exception if the test fails
*/
@Test
public void submitWithOnClickThatReturnsFalse() throws Exception {
final String firstHtml = "<html><head><title>foo</title></head><body>\n" + "<form action='" + URL_SECOND + "' method='post'>\n" + " <input type='submit' name='mySubmit' onClick='document.forms[0].submit(); return false;'>\n" + "</form></body></html>";
final String secondHtml = "<html><head><title>foo</title><script>\n" + " Number.prototype.gn = false;\n" + " function test() {\n" + " var v = 0;\n" + " alert(typeof v);\n" + " alert(v.gn);\n" + " }\n" + "</script></head><body onload='test()'>\n" + "</body></html>";
final String[] expectedAlerts = { "number", "false" };
final List<String> collectedAlerts = new ArrayList<>();
final WebClient client = getWebClientWithMockWebConnection();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection conn = getMockWebConnection();
conn.setResponse(URL_FIRST, firstHtml);
conn.setResponse(URL_SECOND, secondHtml);
final HtmlPage page = client.getPage(URL_FIRST);
final HtmlForm form = page.getForms().get(0);
final HtmlSubmitInput submit = form.getInputByName("mySubmit");
submit.click();
assertEquals(expectedAlerts, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlFormTest method submit_AnchorCausesSubmit_onSubmitHandler_returnFalse.
/**
* <p>Simulate a bug report where an anchor contained JavaScript that caused a form submit.
* According to the bug report, the form would be submitted even though the onsubmit
* handler would return false. This wasn't reproducible but I added a test for it anyway.</p>
*
* <p>UPDATE: If the form submit is triggered by JavaScript then the onsubmit handler is not
* supposed to be called so it doesn't matter what value it returns.</p>
* @throws Exception if the test fails
*/
@Test
public void submit_AnchorCausesSubmit_onSubmitHandler_returnFalse() throws Exception {
final String firstHtml = "<html><head><title>First</title></head>\n" + "<script>function doalert(message){alert(message);}</script>\n" + "<body><form name='form1' method='get' action='" + URL_SECOND + "' " + "onSubmit='doalert(\"clicked\");return false;'>\n" + "<input name='button' type='submit' value='PushMe' id='button'/></form>\n" + "<a id='link1' href='javascript:document.form1.submit()'>Click me</a>\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.setDefaultResponse(secondHtml);
final HtmlPage firstPage = client.getPage(URL_FIRST);
final HtmlAnchor anchor = firstPage.getHtmlElementById("link1");
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
final HtmlPage secondPage = anchor.click();
assertEquals("Second", secondPage.getTitleText());
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlFrameSetTest method scriptUnderNoFrames.
/**
* @throws Exception if the test fails
*/
@Test
public void scriptUnderNoFrames() throws Exception {
final String firstContent = "<html><head><title>first</title></head>\n" + "<frameset cols='100%'>\n" + " <frame src='" + URL_SECOND + "'' id='frame1'/>\n" + " <noframes>\n" + " <div><script>alert(1);</script></div>\n" + " <script src='" + URL_THIRD + "'></script>\n" + " </noframes>\n" + "</frameset></html>";
final String secondContent = "<html><body><script>alert(2);</script></body></html>";
final String thirdContent = "alert('3');\n";
final WebClient client = getWebClientWithMockWebConnection();
final MockWebConnection webConnection = getMockWebConnection();
webConnection.setResponse(URL_FIRST, firstContent);
webConnection.setResponse(URL_SECOND, secondContent);
webConnection.setResponse(URL_THIRD, thirdContent, "text/javascript");
final String[] expectedAlerts = { "2" };
final ArrayList<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
client.getPage(URL_FIRST);
assertEquals(expectedAlerts, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlFormTest method submitURLWithoutParameters.
/**
* Tests that submitting a form without parameters does not trail the URL with a question mark (IE only).
*
* @throws Exception if the test fails
*/
@Test
public void submitURLWithoutParameters() throws Exception {
final String firstHtml = "<html><head><title>foo</title></head><body>\n" + "<form action='" + URL_SECOND + "'>\n" + " <input type='submit' name='mySubmit' onClick='document.forms[0].submit(); return false;'>\n" + "</form></body></html>";
final String secondHtml = "<html><head><title>second</title></head></html>";
final List<String> collectedAlerts = new ArrayList<>();
final WebClient client = getWebClientWithMockWebConnection();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection conn = getMockWebConnection();
conn.setResponse(URL_FIRST, firstHtml);
conn.setDefaultResponse(secondHtml);
final HtmlPage page = client.getPage(URL_FIRST);
final HtmlForm form = page.getForms().get(0);
final HtmlSubmitInput submit = form.getInputByName("mySubmit");
final HtmlPage secondPage = submit.click();
final String expectedURL = URL_SECOND.toString();
assertEquals(expectedURL, secondPage.getUrl());
}
Aggregations