use of com.gargoylesoftware.htmlunit.WebWindowEvent in project htmlunit by HtmlUnit.
the class WindowTest method openWindow_text.
/**
* 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_text() 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 = "Hello World";
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_PLAIN, 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_PLAIN, 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.WebWindowEvent in project htmlunit by HtmlUnit.
the class WindowTest method openWindow_image.
/**
* Open a window with only an image for content, then try to set focus to it.
*
* @throws Exception if the test fails
*/
@Test
public void openWindow_image() 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 = new String(new char[] { 'G', 'I', 'F', '8', '9', 'a', 0x01, 0x00, 0x01, 0x00, 0x80, 0x00, 0x00, 0xfe, 0xd4, 0xaf, 0x00, 0x00, 0x00, 0x21, 0xf9, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44, 0x01, 0x00, 0x3b });
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.IMAGE_GIF, 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.IMAGE_GIF, 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.WebWindowEvent in project htmlunit by HtmlUnit.
the class WindowTest method openWindow.
/**
* @throws Exception if the test fails
*/
@Test
public void openWindow() 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" + "<form name='form1'>\n" + " <a id='link' onClick='window.open(\"" + URL_SECOND + "\", \"MyNewWindow\").focus(); " + "return false;'>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>";
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);
}
});
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 HtmlAnchor anchor = firstPage.getHtmlElementById("link");
final HtmlPage secondPage = anchor.click();
assertNotSame(firstPage, secondPage);
// Expecting contentChanged, opened, contentChanged
assertEquals(3, events.size());
final WebWindow firstWebWindow = (WebWindow) events.get(0).getSource();
final WebWindow secondWebWindow = (WebWindow) events.get(2).getSource();
assertSame(webClient.getCurrentWindow(), secondWebWindow);
assertEquals("MyNewWindow", secondWebWindow.getName());
assertEquals("First", ((HtmlPage) firstWebWindow.getEnclosedPage()).getTitleText());
assertEquals("Second", ((HtmlPage) secondWebWindow.getEnclosedPage()).getTitleText());
final WebWindowEvent changedEvent = events.get(2);
assertNull(changedEvent.getOldPage());
assertEquals("Second", ((HtmlPage) changedEvent.getNewPage()).getTitleText());
assertEquals(new String[] { "MyNewWindow" }, collectedAlerts);
}
Aggregations