use of com.gargoylesoftware.htmlunit.WebWindowListener in project htmlunit by HtmlUnit.
the class WindowTest method openWindow_self.
/**
* _self is a magic name. If we call open(url, '_self') then the current window must be
* reloaded.
* @throws Exception if the test fails
*/
@Test
public void openWindow_self() throws Exception {
final WebClient webClient = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final String firstContent = "<html><head><title>First</title></head><body>\n" + "<form name='form1'>\n" + " <a id='link' onClick='window.open(\"" + URL_SECOND + "\", \"_self\"); " + "return false;'>Click me</a>\n" + "</form>\n" + "</body></html>";
final String secondContent = "<html><head><title>Second</title></head><body></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 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);
}
});
final WebWindow firstWebWindow = firstPage.getEnclosingWindow();
final HtmlAnchor anchor = firstPage.getHtmlElementById("link");
final HtmlPage secondPage = anchor.click();
assertEquals("First", firstPage.getTitleText());
assertEquals("Second", secondPage.getTitleText());
assertEquals(1, events.size());
final WebWindow secondWebWindow = (WebWindow) events.get(0).getSource();
assertSame(webClient.getCurrentWindow(), firstWebWindow);
assertSame(firstWebWindow, secondWebWindow);
}
use of com.gargoylesoftware.htmlunit.WebWindowListener in project htmlunit by HtmlUnit.
the class WindowTest method openWindow_xml.
/**
* Open a window with only XML for content, then try to set focus to it.
*
* @throws Exception if the test fails
*/
@Test
public void openWindow_xml() throws Exception {
final WebClient webClient = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final String firstContent = "<html><head><title>First</title></head><body>\n" + "<form name='form1'>\n" + " <a id='link' onClick='window.open(\"" + URL_SECOND + "\", \"_blank\").focus(); return false;'" + "return false;'>Click me</a>\n" + "</form>\n" + "</body></html>";
final String secondContent = "<junk></junk>\n";
final List<NameValuePair> emptyList = Collections.emptyList();
webConnection.setResponse(URL_FIRST, firstContent, 200, "OK", MimeType.TEXT_HTML, emptyList);
webConnection.setResponse(URL_SECOND, secondContent, 200, "OK", MimeType.TEXT_XML, emptyList);
webClient.setWebConnection(webConnection);
final HtmlPage firstPage = webClient.getPage(URL_FIRST);
assertEquals("First", firstPage.getTitleText());
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);
}
});
final WebWindow firstWebWindow = firstPage.getEnclosingWindow();
final HtmlAnchor anchor = firstPage.getHtmlElementById("link");
final Page secondPage = anchor.click();
assertEquals("First", firstPage.getTitleText());
assertEquals(MimeType.TEXT_XML, secondPage.getWebResponse().getContentType());
assertEquals(2, events.size());
final WebWindow secondWebWindow = (WebWindow) events.get(0).getSource();
assertSame(webClient.getCurrentWindow(), secondWebWindow);
assertNotSame(firstWebWindow, secondWebWindow);
}
use of com.gargoylesoftware.htmlunit.WebWindowListener in project htmlunit by HtmlUnit.
the class WindowTest method openWindow_html.
/**
* Open a window with only text for content, then try to set focus to it.
*
* @throws Exception if the test fails
*/
@Test
public void openWindow_html() throws Exception {
final WebClient webClient = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final String firstContent = "<html><head><title>First</title></head><body>\n" + "<form name='form1'>\n" + " <a id='link' onClick='window.open(\"" + URL_SECOND + "\", \"_blank\").focus(); return false;'" + "return false;'>Click me</a>\n" + "</form>\n" + "</body></html>";
final String secondContent = "<html><head><title>Second</title></head><body>\n" + "<p>Hello World</p>\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 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);
}
});
final WebWindow firstWebWindow = firstPage.getEnclosingWindow();
final HtmlAnchor anchor = firstPage.getHtmlElementById("link");
final Page secondPage = anchor.click();
assertEquals("First", firstPage.getTitleText());
assertEquals(MimeType.TEXT_HTML, secondPage.getWebResponse().getContentType());
assertEquals(2, events.size());
final WebWindow secondWebWindow = (WebWindow) events.get(0).getSource();
assertSame(webClient.getCurrentWindow(), secondWebWindow);
assertNotSame(firstWebWindow, secondWebWindow);
}
use of com.gargoylesoftware.htmlunit.WebWindowListener 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.WebWindowListener in project htmlunit by HtmlUnit.
the class WindowTest method openWindow_javascript.
/**
* Open a window with only JavaScript for content, then try to set focus to it.
*
* @throws Exception if the test fails
*/
@Test
public void openWindow_javascript() throws Exception {
final WebClient webClient = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
final String firstContent = "<html><head><title>First</title></head><body>\n" + "<form name='form1'>\n" + " <a id='link' onClick='window.open(\"" + URL_SECOND + "\", \"_blank\").focus(); return false;'" + "return false;'>Click me</a>\n" + "</form>\n" + "</body></html>";
final String secondContent = "var x=1;\n";
final List<NameValuePair> emptyList = Collections.emptyList();
webConnection.setResponse(URL_FIRST, firstContent, 200, "OK", MimeType.TEXT_HTML, emptyList);
webConnection.setResponse(URL_SECOND, secondContent, 200, "OK", "text/javascript", emptyList);
webClient.setWebConnection(webConnection);
final HtmlPage firstPage = webClient.getPage(URL_FIRST);
assertEquals("First", firstPage.getTitleText());
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);
}
});
final WebWindow firstWebWindow = firstPage.getEnclosingWindow();
final HtmlAnchor anchor = firstPage.getHtmlElementById("link");
final Page secondPage = anchor.click();
assertEquals("First", firstPage.getTitleText());
assertEquals("text/javascript", secondPage.getWebResponse().getContentType());
assertEquals(2, events.size());
final WebWindow secondWebWindow = (WebWindow) events.get(0).getSource();
assertSame(webClient.getCurrentWindow(), secondWebWindow);
assertNotSame(firstWebWindow, secondWebWindow);
}
Aggregations