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());
}
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());
}
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());
}
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);
}
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);
}
Aggregations