Search in sources :

Example 1 with CollectingAlertHandler

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

the class HtmlAnchor2Test method click_onClickHandler.

/**
 * @throws Exception if the test fails
 */
@Test
public void click_onClickHandler() 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\")'>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);
    assertEquals("Second", 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 2 with CollectingAlertHandler

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

the class HtmlAnchor2Test method clickShiftCtrlJavascript.

/**
 * @throws Exception if the test fails
 */
@Test
public void clickShiftCtrlJavascript() throws Exception {
    final String first = "<html><head><title>First</title></head><body>\n" + "  <a href='javascript: window.location=\"" + 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(true, 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 3 with CollectingAlertHandler

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

the class HtmlAnchor2Test method clickCtrlShift.

/**
 * @throws Exception if the test fails
 */
@Test
public void clickCtrlShift() 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(true, 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 4 with CollectingAlertHandler

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

the class HtmlArea2Test 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><map>\n" + "<area href='javascript:alert(\"clicked\")' id='a2' coords='0,0,10,10'/>\n" + "</map></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 HtmlArea area = page.getHtmlElementById("a2");
    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
    final HtmlPage secondPage = area.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)

Example 5 with CollectingAlertHandler

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

the class HtmlAnchor2Test method clickShift.

/**
 * @throws Exception if the test fails
 */
@Test
public void clickShift() 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(true, false, false);
    assertEquals(2, getWebClient().getTopLevelWindows().size());
    assertEquals("Second", 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)

Aggregations

CollectingAlertHandler (com.gargoylesoftware.htmlunit.CollectingAlertHandler)95 WebClient (com.gargoylesoftware.htmlunit.WebClient)91 ArrayList (java.util.ArrayList)88 Test (org.junit.Test)87 MockWebConnection (com.gargoylesoftware.htmlunit.MockWebConnection)82 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)34 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)6 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)5 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