Search in sources :

Example 71 with CollectingAlertHandler

use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.

the class HTMLElement3Test method onBlurOnWindowFocusChange.

/**
 * @throws Exception if the test fails
 */
@Test
@Alerts({ "onblur text2", "onblur text1" })
public void onBlurOnWindowFocusChange() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final String firstHtml = "<html><head><title>First</title></head>\n" + "<body><form name='form1'>\n" + "<input id='text1' onblur='alert(\"onblur text1\")'>\n" + "<button type='button' id='clickme' onClick='window.open(\"" + URL_SECOND + "\");'>Click me</a>\n" + "</form></body></html>";
    webConnection.setResponse(URL_FIRST, firstHtml);
    final String secondHtml = "<html><head><title>Second</title></head>\n" + "<body onLoad='doTest()'>\n" + "<input id='text2' onblur='alert(\"onblur text2\")'>\n" + "<script>\n" + "  function doTest() {\n" + "    opener.document.getElementById('text1').focus();\n" + "    document.getElementById('text2').focus();\n" + "  }\n" + "</script></body></html>";
    webConnection.setResponse(URL_SECOND, secondHtml);
    webClient.setWebConnection(webConnection);
    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());
    final HtmlButton buttonA = firstPage.getHtmlElementById("clickme");
    final HtmlPage secondPage = buttonA.click();
    assertEquals("Second", secondPage.getTitleText());
    webClient.setCurrentWindow(firstPage.getEnclosingWindow());
    webClient.setCurrentWindow(secondPage.getEnclosingWindow());
    assertEquals(getExpectedAlerts(), collectedAlerts);
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ArrayList(java.util.ArrayList) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) HtmlButton(com.gargoylesoftware.htmlunit.html.HtmlButton) Test(org.junit.Test) Alerts(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)

Example 72 with CollectingAlertHandler

use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.

the class HTMLElement3Test method onFocusOnWindowFocusGain.

/**
 * @throws Exception if the test fails
 */
@Test
@Alerts({ "onfocus text1", "onfocus text2", "onfocus text1", "onfocus text2" })
public void onFocusOnWindowFocusGain() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final String firstHtml = "<html><head><title>First</title></head>\n" + "<body><form name='form1'>\n" + "<input id='text1' onfocus='alert(\"onfocus text1\")'>\n" + "<button type='button' id='clickme' onClick='window.open(\"" + URL_SECOND + "\");'>Click me</a>\n" + "</form></body></html>";
    webConnection.setResponse(URL_FIRST, firstHtml);
    final String secondHtml = "<html><head><title>Second</title></head>\n" + "<body onLoad='doTest()'>\n" + "<input id='text2' onfocus='alert(\"onfocus text2\")'>\n" + "<script>\n" + "  function doTest() {\n" + "    opener.document.getElementById('text1').focus();\n" + "    document.getElementById('text2').focus();\n" + "  }\n" + "</script></body></html>";
    webConnection.setResponse(URL_SECOND, secondHtml);
    webClient.setWebConnection(webConnection);
    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());
    final HtmlButton buttonA = firstPage.getHtmlElementById("clickme");
    final HtmlPage secondPage = buttonA.click();
    assertEquals("Second", secondPage.getTitleText());
    webClient.setCurrentWindow(firstPage.getEnclosingWindow());
    webClient.setCurrentWindow(secondPage.getEnclosingWindow());
    assertEquals(getExpectedAlerts(), collectedAlerts);
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ArrayList(java.util.ArrayList) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) HtmlButton(com.gargoylesoftware.htmlunit.html.HtmlButton) Test(org.junit.Test) Alerts(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)

Example 73 with CollectingAlertHandler

use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.

the class HTMLDocumentWriteTest method write_script.

/**
 * Regression test for Bug #71.
 * @throws Exception if the test fails
 */
@Test
public void write_script() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    webClient.setWebConnection(webConnection);
    final String mainHtml = "<html><head><title>Main</title></head><body>\n" + "<iframe name='iframe' id='iframe' src='http://first'></iframe>\n" + "<script type='text/javascript'>\n" + "document.write('<script type=\"text/javascript\" src=\"http://script\"></' + 'script>');\n" + "</script></body></html>";
    webConnection.setResponse(new URL("http://main/"), mainHtml);
    final String firstHtml = "<html><body><h1 id='first'>First</h1></body></html>";
    webConnection.setResponse(URL_FIRST, firstHtml);
    final String secondHtml = "<html><body><h1 id='second'>Second</h1></body></html>";
    webConnection.setResponse(URL_SECOND, secondHtml);
    final String script = "document.getElementById('iframe').src = '" + URL_SECOND + "';\n";
    webConnection.setResponse(new URL("http://script/"), script, MimeType.APPLICATION_JAVASCRIPT);
    final List<String> collectedAlerts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final HtmlPage mainPage = webClient.getPage("http://main");
    assertEquals("Main", mainPage.getTitleText());
    final HtmlInlineFrame iFrame = mainPage.getHtmlElementById("iframe");
    assertEquals(URL_SECOND.toExternalForm(), iFrame.getSrcAttribute());
    final HtmlPage enclosedPage = (HtmlPage) iFrame.getEnclosedPage();
    // This will blow up if the script hasn't been written to the document
    // and executed so the second page has been loaded.
    enclosedPage.getHtmlElementById("second");
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ArrayList(java.util.ArrayList) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL) HtmlInlineFrame(com.gargoylesoftware.htmlunit.html.HtmlInlineFrame) Test(org.junit.Test)

Example 74 with CollectingAlertHandler

use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.

the class HTMLCollection2Test method childNodes.

/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("1")
public void childNodes() throws Exception {
    final String firstContent = "<html><head><title>foo</title><script>\n" + "  function test() {\n" + "    var doc = " + callLoadXMLDocumentFromFile("'" + URL_SECOND + "'") + ";\n" + "    alert(doc.documentElement.childNodes.length);\n" + "  }\n" + LOAD_XML_DOCUMENT_FROM_FILE_FUNCTION + "</script></head><body onload='test()'>\n" + "</body></html>";
    final String secondContent = "<title>Immortality</title>";
    final List<String> collectedAlerts = new ArrayList<>();
    final WebClient client = getWebClient();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection conn = new MockWebConnection();
    conn.setResponse(URL_FIRST, firstContent);
    conn.setResponse(URL_SECOND, secondContent, MimeType.TEXT_XML);
    client.setWebConnection(conn);
    client.getPage(URL_FIRST);
    assertEquals(getExpectedAlerts(), collectedAlerts);
}
Also used : ArrayList(java.util.ArrayList) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test) Alerts(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)

Example 75 with CollectingAlertHandler

use of com.gargoylesoftware.htmlunit.CollectingAlertHandler in project htmlunit by HtmlUnit.

the class HtmlPage4Test method bigJavaScript.

/**
 * @exception Exception if an error occurs
 */
@Test
@Alerts("hello")
public void bigJavaScript() throws Exception {
    final StringBuilder html = new StringBuilder("<html><head>\n" + "<script src='two.js'></script>\n" + "<link rel='stylesheet' type='text/css' href='three.css'/>\n" + "</head>\n" + "<body onload='test()'></body></html>");
    final StringBuilder javaScript = new StringBuilder("function test() {\n" + "alert('hello');\n" + "}");
    final StringBuilder css = new StringBuilder("body {color: blue}");
    final long maxInMemory = getWebClient().getOptions().getMaxInMemory();
    for (int i = 0; i < maxInMemory; i++) {
        html.append(' ');
        javaScript.append(' ');
        css.append(' ');
    }
    BigJavaScriptServlet1.CONTENT_ = html.toString();
    BigJavaScriptServlet2.CONTENT_ = javaScript.toString();
    BigJavaScriptServlet3.CONTENT_ = css.toString();
    final int initialTempFiles = getTempFiles();
    final Map<String, Class<? extends Servlet>> map = new HashMap<>();
    map.put("/one.html", BigJavaScriptServlet1.class);
    map.put("/two.js", BigJavaScriptServlet2.class);
    map.put("/three.css", BigJavaScriptServlet3.class);
    startWebServer(".", null, map);
    try (WebClient client = getWebClient()) {
        final CollectingAlertHandler alertHandler = new CollectingAlertHandler();
        client.setAlertHandler(alertHandler);
        final HtmlPage page = client.getPage(URL_FIRST + "one.html");
        ((HTMLBodyElement) page.getBody().getScriptableObject()).getCurrentStyle();
        assertEquals(getExpectedAlerts(), alertHandler.getCollectedAlerts());
        assertEquals(initialTempFiles + 1, getTempFiles());
    }
    assertEquals(initialTempFiles, getTempFiles());
}
Also used : HashMap(java.util.HashMap) HttpServlet(javax.servlet.http.HttpServlet) Servlet(javax.servlet.Servlet) HTMLBodyElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement) CollectingAlertHandler(com.gargoylesoftware.htmlunit.CollectingAlertHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test) Alerts(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)

Aggregations

CollectingAlertHandler (com.gargoylesoftware.htmlunit.CollectingAlertHandler)96 WebClient (com.gargoylesoftware.htmlunit.WebClient)92 ArrayList (java.util.ArrayList)89 Test (org.junit.Test)87 MockWebConnection (com.gargoylesoftware.htmlunit.MockWebConnection)82 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)35 URL (java.net.URL)22 Alerts (com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)20 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)7 HtmlAnchor (com.gargoylesoftware.htmlunit.html.HtmlAnchor)7 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)6 HashMap (java.util.HashMap)5 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)3 Servlet (javax.servlet.Servlet)3 HttpServlet (javax.servlet.http.HttpServlet)3 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)2 Page (com.gargoylesoftware.htmlunit.Page)2 ScriptException (com.gargoylesoftware.htmlunit.ScriptException)2 WebWindowEvent (com.gargoylesoftware.htmlunit.WebWindowEvent)2 WebWindowListener (com.gargoylesoftware.htmlunit.WebWindowListener)2