use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class WindowConcurrencyTest method verifyGoingToNewPageStopsJavaScript.
/**
* Verifies that when you go to a new page, background JS jobs are stopped (see bug 2127419).
* @throws Exception if the test fails
*/
@Test
public void verifyGoingToNewPageStopsJavaScript() throws Exception {
final String html1 = "<html><head><title>foo</title><script>\n" + " function f() {\n" + " alert('Oh no!');\n" + " }\n" + " function test() {\n" + " window.timeoutId = setInterval(f, 1000);\n" + " }\n" + "</script></head><body onload='test()'>\n" + "</body></html>";
final String html2 = "<html></html>";
final List<String> collectedAlerts = new ArrayList<>();
client_.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection conn = new MockWebConnection();
conn.setResponse(URL_FIRST, html1);
conn.setResponse(URL_SECOND, html2);
client_.setWebConnection(conn);
client_.getPage(URL_FIRST);
client_.getPage(URL_SECOND);
client_.waitForBackgroundJavaScript(5000);
client_.waitForBackgroundJavaScript(5000);
assertTrue(collectedAlerts.isEmpty());
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class WindowTest method close.
/**
* Test closing using JavaScript.
* @throws Exception if the test fails
*/
@Test
public void close() throws Exception {
final WebClient webClient = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final List<String> collectedAlerts = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final String firstContent = "<html><head><title>First</title></head><body>\n" + "<a href='" + URL_SECOND + "' id='link' target='_blank'>Link</a>\n" + "</body></html>";
final String secondContent = "<html><head><title>Second</title></head><body>\n" + "<h1>Second</h1><form>\n" + "<input type='submit' name='action' value='Close' id='button' " + "onclick='window.close(); return false;'>\n" + "</form></body></html>";
webConnection.setResponse(URL_FIRST, firstContent);
webConnection.setResponse(URL_SECOND, secondContent);
webClient.setWebConnection(webConnection);
final HtmlPage firstPage = webClient.getPage(URL_FIRST);
assertEquals("First", firstPage.getTitleText());
assertEquals(1, webClient.getWebWindows().size());
final WebWindow firstWindow = firstPage.getEnclosingWindow();
final HtmlPage secondPage = firstPage.getHtmlElementById("link").click();
assertEquals("Second", secondPage.getTitleText());
assertEquals(2, webClient.getWebWindows().size());
final WebWindow secondWindow = secondPage.getEnclosingWindow();
assertNotSame(firstWindow, secondWindow);
final List<WebWindowEvent> events = new LinkedList<>();
webClient.addWebWindowListener(new WebWindowListener() {
@Override
public void webWindowOpened(final WebWindowEvent event) {
events.add(event);
}
@Override
public void webWindowContentChanged(final WebWindowEvent event) {
events.add(event);
}
@Override
public void webWindowClosed(final WebWindowEvent event) {
events.add(event);
}
});
secondPage.getHtmlElementById("button").click();
final List<WebWindowEvent> expectedEvents = Arrays.asList(new WebWindowEvent[] { new WebWindowEvent(secondWindow, WebWindowEvent.CLOSE, secondPage, null) });
assertEquals(expectedEvents, events);
assertEquals(1, webClient.getWebWindows().size());
assertEquals(firstWindow, webClient.getCurrentWindow());
assertEquals(Collections.EMPTY_LIST, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class WindowTest method openWindow_base.
/**
* @throws Exception if the test fails
*/
@Test
public void openWindow_base() throws Exception {
final WebClient webClient = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final List<String> collectedAlerts = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final String firstContent = "<html><head><title>First</title><base target='MyNewWindow'></head><body>\n" + "<form name='form1'>\n" + " <a id='link' href='" + URL_SECOND + "'>Click me</a>\n" + "</form>\n" + "</body></html>";
final String secondContent = "<html><head><title>Second</title></head><body>\n" + "<script>alert(self.name)</script>\n" + "</body></html>";
webConnection.setResponse(URL_FIRST, firstContent);
webConnection.setResponse(URL_SECOND, secondContent);
webClient.setWebConnection(webConnection);
final HtmlPage firstPage = webClient.getPage(URL_FIRST);
assertEquals("First", firstPage.getTitleText());
final WebWindow firstWebWindow = firstPage.getEnclosingWindow();
assertEquals(firstWebWindow, firstWebWindow.getTopWindow());
final HtmlAnchor anchor = firstPage.getHtmlElementById("link");
final HtmlPage secondPage = anchor.click();
assertEquals("Second", secondPage.getTitleText());
assertNotSame(firstPage, secondPage);
final WebWindow secondWebWindow = secondPage.getEnclosingWindow();
assertNotSame(firstWebWindow, secondWebWindow);
assertEquals("MyNewWindow", secondWebWindow.getName());
assertEquals(secondWebWindow, secondWebWindow.getTopWindow());
assertEquals(new String[] { "MyNewWindow" }, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class LocationTest method testGetVariousAttributes.
private void testGetVariousAttributes(final String url, final String[] expectedAlerts) throws Exception {
final WebClient client = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final String content = "<html><head><title>First</title><script>\n" + "function doTest() {\n" + " var location = document.location;\n" + " alert(location.hash);\n" + " alert(location.host);\n" + " alert(location.hostname);\n" + " alert(location.href);\n" + " alert(location.pathname);\n" + " alert(location.port);\n" + " alert(location.protocol);\n" + " alert(location.search);\n" + "}\n</script></head>\n" + "<body onload='doTest()'></body></html>";
webConnection.setDefaultResponse(content);
client.setWebConnection(webConnection);
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
// Try page with only a server name
client.getPage(url);
assertEquals(expectedAlerts, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class LocationTest method locationWithTarget.
/**
* @throws Exception if the test fails
*/
@Test
@Alerts("c")
public void locationWithTarget() throws Exception {
final WebClient client = getWebClient();
final List<String> alerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(alerts));
final URL url = getClass().getResource("LocationTest_locationWithTarget_a.html");
assertNotNull(url);
final HtmlPage a = client.getPage(url);
final HtmlPage c = (HtmlPage) a.getFrameByName("c").getEnclosedPage();
c.getHtmlElementById("anchor").click();
assertEquals(getExpectedAlerts(), alerts);
}
Aggregations