Tests for Window. @author Mike Bowler @author Chen Jun @author David K. Taylor @author Darrell DeBoer @author Marc Guillemot @author Dierk Koenig @author Chris Erskine @author David D. Kilzer @author Ahmed Ashour @author Daniel Gredler @author Frank Danek @author Ronald Brill
| 68 | * @author Ronald Brill |
| 69 | */ |
| 70 | public class WindowTest extends SimpleWebTestCase { |
| 71 | |
| 72 | /** |
| 73 | * @throws Exception if the test fails |
| 74 | */ |
| 75 | @Test |
| 76 | public void openWindow() throws Exception { |
| 77 | final WebClient webClient = getWebClient(); |
| 78 | final MockWebConnection webConnection = new MockWebConnection(); |
| 79 | |
| 80 | final List<String> collectedAlerts = new ArrayList<>(); |
| 81 | webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); |
| 82 | |
| 83 | final String firstContent = DOCTYPE_HTML |
| 84 | + "<html><head><title>First</title></head><body>\n" |
| 85 | + "<form name='form1'>\n" |
| 86 | + " <a id='link' onClick='window.open(\"" + URL_SECOND + "\", \"MyNewWindow\").focus(); " |
| 87 | + "return false;'>Click me</a>\n" |
| 88 | + "</form>\n" |
| 89 | + "</body></html>"; |
| 90 | final String secondContent = DOCTYPE_HTML |
| 91 | + "<html><head><title>Second</title></head><body>\n" |
| 92 | + "<script>alert(self.name)</script>\n" |
| 93 | + "</body></html>"; |
| 94 | |
| 95 | final List<WebWindowEvent> events = new LinkedList<>(); |
| 96 | webClient.addWebWindowListener(new WebWindowListener() { |
| 97 | @Override |
| 98 | public void webWindowOpened(final WebWindowEvent event) { |
| 99 | events.add(event); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void webWindowContentChanged(final WebWindowEvent event) { |
| 104 | events.add(event); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void webWindowClosed(final WebWindowEvent event) { |
| 109 | events.add(event); |
| 110 | } |
| 111 | }); |
| 112 | |
| 113 | webConnection.setResponse(URL_FIRST, firstContent); |
| 114 | webConnection.setResponse(URL_SECOND, secondContent); |
| 115 | webClient.setWebConnection(webConnection); |
| 116 | |
| 117 | final HtmlPage firstPage = webClient.getPage(URL_FIRST); |
| 118 | assertEquals("First", firstPage.getTitleText()); |
| 119 | |
| 120 | final HtmlAnchor anchor = firstPage.getHtmlElementById("link"); |
| 121 | final HtmlPage secondPage = anchor.click(); |
| 122 | assertNotSame(firstPage, secondPage); |
| 123 | |
| 124 | // Expecting contentChanged, opened, contentChanged |
| 125 | assertEquals(3, events.size()); |
| 126 | |
| 127 | final WebWindow firstWebWindow = (WebWindow) events.get(0).getSource(); |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…