use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlAnchor2Test method clickShiftJavascript.
/**
* @throws Exception if the test fails
*/
@Test
public void clickShiftJavascript() 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, false, false);
assertEquals(2, getWebClient().getTopLevelWindows().size());
assertEquals("Second", secondPage.getTitleText());
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class ClickableElementTest method onClickPageTest.
/**
* Full page driver for onClick tests.
*
* @param htmlContent HTML fragment for body of page with clickable element identified by clickId ID attribute
* @param numClicks number of times to click element
* @param expectedAlerts array of expected popup values
* @param exceptionOnError
* @throws Exception if the test fails
*/
private void onClickPageTest(final String htmlContent, final int numClicks, final boolean exceptionOnError) throws Exception {
final WebClient client = getWebClientWithMockWebConnection();
final MockWebConnection webConnection = getMockWebConnection();
webConnection.setDefaultResponse(htmlContent);
client.getOptions().setThrowExceptionOnScriptError(exceptionOnError);
final List<String> collectedAlerts = new ArrayList<>();
final CollectingAlertHandler alertHandler = new CollectingAlertHandler(collectedAlerts);
client.setAlertHandler(alertHandler);
final HtmlPage page = client.getPage(URL_FIRST);
final HtmlElement clickable = page.getHtmlElementById("clickId");
for (int i = 0; i < numClicks; i++) {
clickable.click();
}
assertEquals(getExpectedAlerts(), collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlScriptTest method addEventListener_error.
private void addEventListener_error(final boolean throwOnFailingStatusCode) throws Exception {
final URL fourOhFour = new URL(URL_FIRST, "/404");
final String html = "<html><head>\n" + "<script>\n" + " function test() {\n" + " var s1 = document.createElement('script');\n" + " s1.text = 'var foo';\n" + " s1.addEventListener('error', function() {alert('a')}, false);\n" + " document.body.insertBefore(s1, document.body.firstChild);\n" + " \n" + " var s2 = document.createElement('script');\n" + " s2.text = 'varrrr foo';\n" + " s2.addEventListener('error', function() {alert('b')}, false);\n" + " document.body.insertBefore(s2, document.body.firstChild);\n" + " \n" + " var s3 = document.createElement('script');\n" + " s3.src = '//:';\n" + " s3.addEventListener('error', function() {alert('c')}, false);\n" + " document.body.insertBefore(s3, document.body.firstChild);\n" + " \n" + " var s4 = document.createElement('script');\n" + " s4.src = '" + URL_SECOND + "';\n" + " s4.addEventListener('error', function() {alert('d')}, false);\n" + " document.body.insertBefore(s4, document.body.firstChild);\n" + " \n" + " var s5 = document.createElement('script');\n" + " s5.src = '" + URL_THIRD + "';\n" + " s5.addEventListener('error', function() {alert('e')}, false);\n" + " document.body.insertBefore(s5, document.body.firstChild);\n" + " \n" + " var s6 = document.createElement('script');\n" + " s6.src = '" + fourOhFour + "';\n" + " s6.addEventListener('error', function() {alert('f')}, false);\n" + " document.body.insertBefore(s6, document.body.firstChild);\n" + " }\n" + "</script>\n" + "</head>\n" + "<body onload='test()'></body>\n" + "</html>";
final WebClient client = getWebClient();
client.getOptions().setThrowExceptionOnFailingStatusCode(throwOnFailingStatusCode);
final MockWebConnection conn = new MockWebConnection();
conn.setResponse(URL_FIRST, html);
conn.setResponse(URL_SECOND, "var foo;", MimeType.APPLICATION_JAVASCRIPT);
conn.setResponse(URL_THIRD, "varrrr foo;", MimeType.APPLICATION_JAVASCRIPT);
conn.setResponse(fourOhFour, "", 404, "Missing", MimeType.APPLICATION_JAVASCRIPT, new ArrayList<NameValuePair>());
client.setWebConnection(conn);
final List<String> actual = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(actual));
client.getOptions().setThrowExceptionOnScriptError(false);
client.getPage(URL_FIRST);
assertEquals(getExpectedAlerts(), actual);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class HtmlPage2Test method save.
/**
* @throws Exception if the test fails
*/
@Test
@Alerts("Hello there")
public void save() throws Exception {
final String html = "<html><head><script src='" + URL_SECOND + "'>\n</script></head></html>";
final String js = "alert('Hello there')";
final WebClient webClient = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setResponse(URL_FIRST, html);
webConnection.setResponse(URL_SECOND, js);
webClient.setWebConnection(webConnection);
final List<String> collectedAlerts = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final HtmlPage page = webClient.getPage(URL_FIRST);
assertEquals(getExpectedAlerts(), collectedAlerts);
final HtmlScript sript = page.getFirstByXPath("//script");
assertEquals(URL_SECOND.toString(), sript.getSrcAttribute());
final File tmpFolder = tmpFolderProvider_.newFolder("hu");
final File file = new File(tmpFolder, "hu_HtmlPageTest_save.html");
page.save(file);
assertTrue(file.exists());
assertTrue(file.isFile());
final String content = FileUtils.readFileToString(file, ISO_8859_1);
assertFalse(content.contains("<script"));
assertEquals(URL_SECOND.toString(), sript.getSrcAttribute());
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class WindowConcurrencyTest method verifyCloseStopsJavaScript.
/**
* Verifies that when all windows are closed, background JS jobs are stopped (see bug 2127419).
* @throws Exception if the test fails
*/
@Test
public void verifyCloseStopsJavaScript() throws Exception {
final String html = "<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 List<String> collectedAlerts = new ArrayList<>();
client_.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setDefaultResponse(html);
client_.setWebConnection(webConnection);
client_.getPage(URL_FIRST);
client_.close();
client_.waitForBackgroundJavaScript(5000);
assertTrue(collectedAlerts.isEmpty());
}
Aggregations