use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class LocationTest method fileUrlFormat.
/**
* @throws Exception if an error occurs
*/
@Test
public void fileUrlFormat() throws Exception {
final URL url = getClass().getResource("LocationTest_fileUrlFormat.html");
assertNotNull(url);
final WebClient client = getWebClient();
final List<String> alerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(alerts));
client.getPage(url);
assertEquals(1, alerts.size());
assertTrue(alerts.get(0).startsWith("file:///"));
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class LocationTest method href.
/**
* @throws Exception if the test fails
*/
@Test
public void href() throws Exception {
final WebClient webClient = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final String content = "<html><head><title>First</title><script>\n" + "function test() {\n" + " alert(window.location.href);\n" + "}\n" + "</script></head><body onload='test()'>\n" + "</body></html>";
webConnection.setResponse(new URL("http://myHostName/"), content);
webClient.setWebConnection(webConnection);
final List<String> collectedAlerts = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
webClient.getPage("http://myHostName");
final String[] expectedAlerts = { "http://myHostName/" };
assertEquals(expectedAlerts, collectedAlerts);
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class LocationTest method setHash.
/**
* Verifies that modifying <tt>window.location.hash</tt> works, but that it doesn't
* force the page to reload from the server. This is very important for the Dojo
* unit tests, which will keep reloading themselves if the page gets reloaded.
*
* @throws Exception if the test fails
*/
@Test
public void setHash() throws Exception {
final WebClient webClient = getWebClient();
final MockWebConnection conn = new MockWebConnection();
final String html = "<html><head><title>Test</title></head><body>\n" + "<a id='a' onclick='alert(location.hash);location.hash=\"b\";alert(location.hash);'>go</a>\n" + "<h2 id='b'>...</h2></body></html>";
conn.setResponse(URL_FIRST, html);
webClient.setWebConnection(conn);
final List<String> actual = new ArrayList<>();
webClient.setAlertHandler(new CollectingAlertHandler(actual));
final HtmlPage page = webClient.getPage(URL_FIRST);
final HtmlAnchor anchor = page.getHtmlElementById("a");
final HtmlPage page2 = anchor.click();
// Verify that it worked.
final String[] expected = new String[] { "", "#b" };
assertEquals(expected, actual);
// Verify that we didn't reload the page.
assertTrue(page == page2);
assertEquals(URL_FIRST, conn.getLastWebRequest().getUrl());
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class ActiveXObjectTest method method.
/**
* @throws Exception if the test fails
*/
@Test
public void method() throws Exception {
if (!getBrowserVersion().isIE()) {
return;
}
if (!isJacobInstalled()) {
return;
}
final String html = "<html><head>\n" + "<script>\n" + " function test() {\n" + " try {\n" + " var ie = new ActiveXObject('InternetExplorer.Application');\n" + " ie.PutProperty('Hello', 'There');\n" + " document.tile = ie.GetProperty('Hello'));\n" + " } catch(e) {document.title = 'exception: ' + e.message;}\n" + " }\n" + "</script></head><body onload='test()'>\n" + "</body></html>";
final WebClient client = getWebClient();
client.getOptions().setActiveXNative(true);
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setResponse(URL_FIRST, html);
client.setWebConnection(webConnection);
final HtmlPage page = client.getPage(URL_FIRST);
assertEquals("There", page.getTitleText());
}
use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.
the class Event3Test method testEventBubblingReturns.
private void testEventBubblingReturns(final String onclick1, final String onclick2, final String onclick3, final boolean changesPage) throws Exception {
final String html1 = "<html><head><title>First</title></head><body>\n" + "<div onclick='alert(\"d\"); " + onclick1 + "'>\n" + "<span onclick='alert(\"s\"); " + onclick2 + "'>\n" + "<a href='" + URL_SECOND + "' id='a' onclick='alert(\"a\"); " + onclick3 + "'>go</a>\n" + "</span>\n" + "</div>\n" + "</body></html>";
final String html2 = "<html><head><title>Second</title></head><body></body></html>";
final WebClient client = getWebClientWithMockWebConnection();
final List<String> collectedAlerts = new ArrayList<>();
client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
final MockWebConnection webConnection = getMockWebConnection();
webConnection.setResponse(URL_FIRST, html1);
webConnection.setResponse(URL_SECOND, html2);
final HtmlPage page = client.getPage(URL_FIRST);
final HtmlAnchor anchor = page.getHtmlElementById("a");
final HtmlPage secondPage = anchor.click();
assertEquals(new String[] { "a", "s", "d" }, collectedAlerts);
if (changesPage) {
assertNotSame(page, secondPage);
} else {
assertSame(page, secondPage);
}
}
Aggregations