Search in sources :

Example 56 with CollectingAlertHandler

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

the class HtmlAnchor2Test method correctLinkTargetWhenOnclickOpensWindow.

/**
 * Links with an href and a non-false returning onclick that opens a new window should still
 * open the href in the first window.
 *
 * http://sourceforge.net/p/htmlunit/bugs/394/
 *
 * @throws Exception on test failure
 */
@Test
public void correctLinkTargetWhenOnclickOpensWindow() throws Exception {
    final String firstContent = "<html><head><title>First</title></head><body>\n" + "<a href='page2.html' id='clickme' onclick=\"window.open('popup.html', 'newWindow');\">X</a>\n" + "</body></html>";
    final String html2 = "<html><head><title>Second</title></head><body></body></html>";
    final String htmlPopup = "<html><head><title>Popup</title></head><body></body></html>";
    final WebClient client = getWebClient();
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setResponse(URL_FIRST, firstContent);
    webConnection.setResponse(new URL(URL_FIRST, "page2.html"), html2);
    webConnection.setResponse(new URL(URL_FIRST, "popup.html"), htmlPopup);
    client.setWebConnection(webConnection);
    final HtmlPage firstPage = client.getPage(URL_FIRST);
    final HtmlAnchor anchor = firstPage.getHtmlElementById("clickme");
    final HtmlPage pageAfterClick = anchor.click();
    assertEquals("Second window did not open", 2, client.getWebWindows().size());
    assertNotSame("New Page was not returned", firstPage, pageAfterClick);
    assertEquals("Wrong new Page returned", "Popup", pageAfterClick.getTitleText());
    assertEquals("Original window not updated", "Second", ((HtmlPage) firstPage.getEnclosingWindow().getEnclosedPage()).getTitleText());
}
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) Test(org.junit.Test)

Example 57 with CollectingAlertHandler

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

the class HtmlAnchor2Test method clickCtrl.

/**
 * @throws Exception if the test fails
 */
@Test
public void clickCtrl() throws Exception {
    final String first = "<html><head><title>First</title></head><body>\n" + "  <a href='" + URL_SECOND + "' id='a2'>link to foo2</a>\n" + "</body></html>";
    final String second = "<html><head><title>Second</title></head><body></body></html>";
    final WebClient client = getWebClient();
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setResponse(URL_FIRST, first);
    webConnection.setResponse(URL_SECOND, second);
    client.setWebConnection(webConnection);
    assertEquals(1, getWebClient().getTopLevelWindows().size());
    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlAnchor anchor = page.getHtmlElementById("a2");
    final HtmlPage secondPage = anchor.click(false, true, false);
    assertEquals(2, getWebClient().getTopLevelWindows().size());
    assertEquals("First", secondPage.getTitleText());
}
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 58 with CollectingAlertHandler

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

the class HtmlAnchor2Test method click_onClickHandler_javascriptDisabled.

/**
 * @throws Exception if the test fails
 */
@Test
public void click_onClickHandler_javascriptDisabled() throws Exception {
    final String htmlContent = "<html><head><title>foo</title></head><body>\n" + "<a href='http://www.foo1.com' id='a1'>link to foo1</a>\n" + "<a href='http://www.foo2.com' id='a2' " + "onClick='alert(\"clicked\")'>link to foo2</a>\n" + "<a href='http://www.foo3.com' id='a3'>link to foo3</a>\n" + "</body></html>";
    final WebClient client = getWebClient();
    client.getOptions().setJavaScriptEnabled(false);
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setDefaultResponse(htmlContent);
    client.setWebConnection(webConnection);
    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlAnchor anchor = page.getHtmlElementById("a2");
    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
    final HtmlPage secondPage = anchor.click();
    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
    final List<?> expectedParameters = Collections.EMPTY_LIST;
    assertEquals("url", "http://www.foo2.com/", secondPage.getUrl());
    assertSame("method", HttpMethod.GET, webConnection.getLastMethod());
    assertEquals("parameters", expectedParameters, webConnection.getLastParameters());
}
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 59 with CollectingAlertHandler

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

the class HtmlAnchor2Test method click_onClickHandler_returnFalse.

/**
 * @throws Exception if the test fails
 */
@Test
public void click_onClickHandler_returnFalse() throws Exception {
    final String firstContent = "<html><head><title>First</title></head><body>\n" + "<a href='http://www.foo1.com' id='a1'>link to foo1</a>\n" + "<a href='" + URL_SECOND + "' id='a2' " + "onClick='alert(\"clicked\");return false;'>link to foo2</a>\n" + "<a href='http://www.foo3.com' id='a3'>link to foo3</a>\n" + "</body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body></body></html>";
    final WebClient client = getWebClient();
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setResponse(URL_FIRST, firstContent);
    webConnection.setResponse(URL_SECOND, secondContent);
    client.setWebConnection(webConnection);
    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlAnchor anchor = page.getHtmlElementById("a2");
    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
    final HtmlPage secondPage = anchor.click();
    assertEquals(new String[] { "clicked" }, collectedAlerts);
    assertSame(page, secondPage);
}
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 60 with CollectingAlertHandler

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

the class HtmlAnchor2Test method click_javascriptUrl_javascriptDisabled.

/**
 * @throws Exception if the test fails
 */
@Test
public void click_javascriptUrl_javascriptDisabled() throws Exception {
    final String htmlContent = "<html><head><title>foo</title></head><body>\n" + "<a href='http://www.foo1.com' id='a1'>link to foo1</a>\n" + "<a href='javascript:alert(\"clicked\")' id='a2'>link to foo2</a>\n" + "<a href='http://www.foo3.com' id='a3'>link to foo3</a>\n" + "</body></html>";
    final WebClient client = getWebClient();
    client.getOptions().setJavaScriptEnabled(false);
    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setDefaultResponse(htmlContent);
    client.setWebConnection(webConnection);
    final HtmlPage page = client.getPage(URL_FIRST);
    final HtmlAnchor anchor = page.getHtmlElementById("a2");
    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
    final HtmlPage secondPage = anchor.click();
    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
    assertSame(page, secondPage);
}
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