Search in sources :

Example 86 with CollectingAlertHandler

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);
}
Also used : ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Example 87 with CollectingAlertHandler

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"));
}
Also used : ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Test(org.junit.Test)

Example 88 with CollectingAlertHandler

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());
}
Also used : ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Example 89 with CollectingAlertHandler

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());
}
Also used : ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Example 90 with CollectingAlertHandler

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);
}
Also used : ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Aggregations

CollectingAlertHandler (com.gargoylesoftware.htmlunit.CollectingAlertHandler)96 WebClient (com.gargoylesoftware.htmlunit.WebClient)92 ArrayList (java.util.ArrayList)89 Test (org.junit.Test)87 MockWebConnection (com.gargoylesoftware.htmlunit.MockWebConnection)82 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)35 URL (java.net.URL)22 Alerts (com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)20 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)7 HtmlAnchor (com.gargoylesoftware.htmlunit.html.HtmlAnchor)7 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)6 HashMap (java.util.HashMap)5 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)3 Servlet (javax.servlet.Servlet)3 HttpServlet (javax.servlet.http.HttpServlet)3 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)2 Page (com.gargoylesoftware.htmlunit.Page)2 ScriptException (com.gargoylesoftware.htmlunit.ScriptException)2 WebWindowEvent (com.gargoylesoftware.htmlunit.WebWindowEvent)2 WebWindowListener (com.gargoylesoftware.htmlunit.WebWindowListener)2