use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlFormTest method submit_onSubmitHandler.
/**
* @throws Exception if the test fails
*/
@Test
public void submit_onSubmitHandler() throws Exception {
final String firstHtml = "<html><head><title>First</title></head><body>\n" + "<form method='get' action='" + URL_SECOND + "' onSubmit='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.setDefaultResponse(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("Second", secondPage.getTitleText());
assertEquals(new String[] { "clicked" }, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlFrameSetTest method frameReloadsAnother.
/**
* Regression test for Bug #521.
* @throws Exception if the test fails
*/
@Test
public void frameReloadsAnother() throws Exception {
final URL framesetURL = new URL("http://domain/frameset.html");
final URL leftURL = new URL("http://domain/left.html");
final URL rightURL = new URL("http://domain/right.html");
final URL right2URL = new URL("http://domain/right2.html");
final String framesetHtml = "<html><head><title>Test Frameset</title><script>\n" + "function writeLeftFrame() {\n" + " var leftDoc = leftFrame.document;\n" + " leftDoc.open();\n" + " leftDoc.writeln('<HTML><BODY>This is the left frame.<br><br>'\n" + " + '<A HREF=\"javaScript:parent.showNextPage()\" id=\"node_0\">Show version 2 '\n" + " + 'of right frame</A></BODY></HTML>');\n" + " leftDoc.close();\n" + "}\n" + "\n" + "function showNextPage() {\n" + " rightFrame.location = 'right2.html';\n" + "}\n" + "</script></head>\n" + "<frameset cols='300,*' border=1>\n" + " <frame name='leftFrame' src='left.html'>\n" + " <frame name='rightFrame' src='right.html'>\n" + "</frameset>\n" + "</html>";
final String leftHtml = "<html>\n" + "<body onLoad=\"parent.writeLeftFrame()\">\n" + " This is the initial left frame, to be overwritten immediately (onLoad).\n" + "</body></html>";
final String rightHtml = "<html>\n" + "<body>\n" + " This is the right frame, version 1.\n" + "</body></html>";
final String right2Html = "<html>\n" + "<body>\n" + " This is the right frame, version 2.\n" + "</body></html>";
final WebClient client = getWebClientWithMockWebConnection();
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = getMockWebConnection();
webConnection.setResponse(framesetURL, framesetHtml);
webConnection.setResponse(leftURL, leftHtml);
webConnection.setResponse(rightURL, rightHtml);
webConnection.setResponse(right2URL, right2Html);
final HtmlPage page = client.getPage(framesetURL);
final HtmlPage leftPage = (HtmlPage) page.getFrames().get(0).getEnclosedPage();
final WebWindow rightWindow = page.getFrames().get(1);
assertTrue(((HtmlPage) rightWindow.getEnclosedPage()).asXml().contains("version 1"));
leftPage.getAnchors().get(0).click();
assertTrue(((HtmlPage) rightWindow.getEnclosedPage()).asXml().contains("version 2"));
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlArea2Test method click_onclickReturnsFalse.
/**
* @throws Exception if the test fails
*/
@Test
public void click_onclickReturnsFalse() throws Exception {
final WebClient client = createWebClient("alert('foo');return false;");
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final HtmlPage page = client.getPage(URL_FIRST);
final HtmlArea area = page.getHtmlElementById("second");
final HtmlPage thirdPage = area.click();
assertEquals(new String[] { "foo" }, collectedAlerts);
assertEquals("first", thirdPage.getTitleText());
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlArea2Test method click_onclickReturnsTrue.
/**
* @throws Exception if the test fails
*/
@Test
public void click_onclickReturnsTrue() throws Exception {
final WebClient client = createWebClient("alert('foo');return true;");
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final HtmlPage page = client.getPage(URL_FIRST);
final HtmlArea area = page.getHtmlElementById("second");
final HtmlPage thirdPage = area.click();
assertEquals(new String[] { "foo" }, collectedAlerts);
assertEquals("second", thirdPage.getTitleText());
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlFormTest method submit_javascriptActionMixedCase.
/**
* @throws Exception if the test fails
*/
@Test
public void submit_javascriptActionMixedCase() 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);
}
Aggregations