Search in sources :

Example 26 with CollectingAlertHandler

use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.

the class HtmlFormTest method submit_onSubmitHandler_returnFalse.

/**
 * @throws Exception if the test fails
 */
@Test
public void submit_onSubmitHandler_returnFalse() throws Exception {
    final String firstHtml = "<html><head><title>First</title></head><body>\n" + "<form method='get' action='" + URL_SECOND + "' " + "onSubmit='alert(\"clicked\");return false;'>\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)

Example 27 with CollectingAlertHandler

use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.

the class HtmlFormTest method submit_onSubmitHandler_javascriptDisabled.

/**
 * @throws Exception if the test fails
 */
@Test
public void submit_onSubmitHandler_javascriptDisabled() 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();
    client.getOptions().setJavaScriptEnabled(false);
    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("First", firstPage.getTitleText());
    assertEquals("Second", secondPage.getTitleText());
    assertEquals(Collections.EMPTY_LIST, 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 28 with CollectingAlertHandler

use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.

the class HtmlFormTest method submit_javascriptAction.

/**
 * @throws Exception if the test fails
 */
@Test
public void submit_javascriptAction() 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)

Example 29 with CollectingAlertHandler

use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.

the class HtmlFrameSetTest method frameOnloadAccessOtherFrame.

/**
 * Forward referencing issue in FrameSet.
 * Test for bug #291
 * @throws Exception if the test fails
 */
@Test
public void frameOnloadAccessOtherFrame() throws Exception {
    final String framesContent = "<html><head><title>Main</title>\n" + "</head>\n" + "  <frameset cols='18%,*'>\n" + "    <frame name='menu' src='" + URL_SECOND + "'>\n" + "    <frame name='button_pallete' src='about:blank'>\n" + "  </frameset>\n" + "</html>";
    final String menuContent = "<html><head><title>Menu</title>\n" + "  <script>\n" + "    function init() {\n" + "      var oFrame = top.button_pallete;\n" + "      alert((oFrame == null) ? 'Failure' : 'Success');\n" + "    }\n" + "  </script>\n" + "</head>\n" + "<body onload='init()'></body></html>";
    final WebClient webClient = getWebClientWithMockWebConnection();
    final MockWebConnection webConnection = getMockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();
    final String[] expectedAlerts = { "Success" };
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    webConnection.setResponse(URL_FIRST, framesContent);
    webConnection.setResponse(URL_SECOND, menuContent);
    final HtmlPage framesPage = webClient.getPage(URL_FIRST);
    assertEquals("Main", framesPage.getTitleText());
    assertEquals(expectedAlerts, collectedAlerts);
}
Also used : ArrayList(java.util.ArrayList) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Example 30 with CollectingAlertHandler

use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.

the class HtmlFrameTest method onLoadHandler.

/**
 * @throws Exception if the test fails
 */
@Test
public void onLoadHandler() throws Exception {
    final WebClient webClient = getWebClientWithMockWebConnection();
    final MockWebConnection webConnection = getMockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final String html = "<html><head><title>first</title></head>\n" + "<frameset cols='20%,80%'>\n" + "  <frame id='frame1'>\n" + "  <frame onload='alert(this.tagName)' id='frame2'>\n" + "</frameset></html>";
    final String[] expectedAlerts = { "FRAME" };
    webConnection.setResponse(URL_FIRST, html);
    final HtmlPage page = webClient.getPage(URL_FIRST);
    assertEquals("first", page.getTitleText());
    final HtmlFrame frame1 = page.getHtmlElementById("frame1");
    assertEquals("frame1", "", ((HtmlPage) frame1.getEnclosedPage()).getTitleText());
    final HtmlFrame frame2 = page.getHtmlElementById("frame2");
    assertEquals("frame2", "", ((HtmlPage) frame2.getEnclosedPage()).getTitleText());
    assertEquals(expectedAlerts, collectedAlerts);
}
Also used : ArrayList(java.util.ArrayList) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) 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