0001: /*
0002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
0003: *
0004: * Redistribution and use in source and binary forms, with or without
0005: * modification, are permitted provided that the following conditions are met:
0006: *
0007: * 1. Redistributions of source code must retain the above copyright notice,
0008: * this list of conditions and the following disclaimer.
0009: * 2. Redistributions in binary form must reproduce the above copyright notice,
0010: * this list of conditions and the following disclaimer in the documentation
0011: * and/or other materials provided with the distribution.
0012: * 3. The end-user documentation included with the redistribution, if any, must
0013: * include the following acknowledgment:
0014: *
0015: * "This product includes software developed by Gargoyle Software Inc.
0016: * (http://www.GargoyleSoftware.com/)."
0017: *
0018: * Alternately, this acknowledgment may appear in the software itself, if
0019: * and wherever such third-party acknowledgments normally appear.
0020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
0021: * products derived from this software without prior written permission.
0022: * For written permission, please contact info@GargoyleSoftware.com.
0023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
0024: * "HtmlUnit" appear in their name, without prior written permission of
0025: * Gargoyle Software Inc.
0026: *
0027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
0028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
0029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
0030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
0031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
0033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
0036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0037: */
0038: package com.gargoylesoftware.htmlunit.html;
0039:
0040: import java.io.ByteArrayInputStream;
0041: import java.io.ByteArrayOutputStream;
0042: import java.io.IOException;
0043: import java.io.ObjectInputStream;
0044: import java.io.ObjectOutputStream;
0045: import java.net.URL;
0046: import java.util.ArrayList;
0047: import java.util.Arrays;
0048: import java.util.Collections;
0049: import java.util.Iterator;
0050: import java.util.List;
0051:
0052: import javax.xml.parsers.DocumentBuilder;
0053: import javax.xml.parsers.DocumentBuilderFactory;
0054:
0055: import org.apache.commons.httpclient.Cookie;
0056:
0057: import com.gargoylesoftware.htmlunit.BrowserVersion;
0058: import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
0059: import com.gargoylesoftware.htmlunit.ElementNotFoundException;
0060: import com.gargoylesoftware.htmlunit.ImmediateRefreshHandler;
0061: import com.gargoylesoftware.htmlunit.IncorrectnessListener;
0062: import com.gargoylesoftware.htmlunit.KeyValuePair;
0063: import com.gargoylesoftware.htmlunit.MockWebConnection;
0064: import com.gargoylesoftware.htmlunit.OnbeforeunloadHandler;
0065: import com.gargoylesoftware.htmlunit.Page;
0066: import com.gargoylesoftware.htmlunit.StringWebResponse;
0067: import com.gargoylesoftware.htmlunit.SubmitMethod;
0068: import com.gargoylesoftware.htmlunit.TextUtil;
0069: import com.gargoylesoftware.htmlunit.WebClient;
0070: import com.gargoylesoftware.htmlunit.WebRequestSettings;
0071: import com.gargoylesoftware.htmlunit.WebResponse;
0072: import com.gargoylesoftware.htmlunit.WebTestCase;
0073: import com.gargoylesoftware.htmlunit.html.HtmlElementTest.HtmlAttributeChangeListenerTestImpl;
0074:
0075: /**
0076: * Tests for {@link HtmlPage}.
0077: *
0078: * @version $Revision: 2132 $
0079: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
0080: * @author Noboru Sinohara
0081: * @author David K. Taylor
0082: * @author Andreas Hangler
0083: * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
0084: * @author Marc Guillemot
0085: * @author Ahmed Ashour
0086: */
0087: public class HtmlPageTest extends WebTestCase {
0088:
0089: /**
0090: * Create an instance
0091: *
0092: * @param name The name of the test
0093: */
0094: public HtmlPageTest(final String name) {
0095: super (name);
0096: }
0097:
0098: /**
0099: * @exception Exception If the test fails
0100: */
0101: public void testConstructor() throws Exception {
0102: final String htmlContent = "<html>\n"
0103: + "<head><title>foo</title></head>\n"
0104: + "<body>\n"
0105: + "<p>hello world</p>\n"
0106: + "<form id='form1' action='/formSubmit' method='post'>\n"
0107: + "<input type='text' NAME='textInput1' value='textInput1'/>\n"
0108: + "<input type='text' name='textInput2' value='textInput2'/>\n"
0109: + "<input type='hidden' name='hidden1' value='hidden1'/>\n"
0110: + "<input type='submit' name='submitInput1' value='push me'/>\n"
0111: + "</form>\n" + "</body></html>";
0112:
0113: final HtmlPage page = loadPage(htmlContent);
0114: assertEquals("foo", page.getTitleText());
0115: }
0116:
0117: /**
0118: * @throws Exception if the test fails
0119: */
0120: public void testGetInputByName() throws Exception {
0121: final String htmlContent = "<html>\n"
0122: + "<head><title>foo</title></head>\n"
0123: + "<body>\n"
0124: + "<p>hello world</p>\n"
0125: + "<form id='form1' action='/formSubmit' method='post'>\n"
0126: + "<input type='text' NAME='textInput1' value='textInput1'/>\n"
0127: + "<input type='text' name='textInput2' value='textInput2'/>\n"
0128: + "<input type='hidden' name='hidden1' value='hidden1'/>\n"
0129: + "<input type='submit' name='submitInput1' value='push me'/>\n"
0130: + "</form>\n" + "</body></html>";
0131: final HtmlPage page = loadPage(htmlContent);
0132:
0133: final HtmlForm form = (HtmlForm) page
0134: .getHtmlElementById("form1");
0135: final HtmlInput input = form.getInputByName("textInput1");
0136: assertEquals("name", "textInput1", input.getNameAttribute());
0137:
0138: assertEquals("value", "textInput1", input.getValueAttribute());
0139: assertEquals("type", "text", input.getTypeAttribute());
0140: }
0141:
0142: /**
0143: * @throws Exception if the test fails
0144: */
0145: public void testFormSubmit() throws Exception {
0146: final String htmlContent = "<html>\n"
0147: + "<head><title>foo</title></head>\n"
0148: + "<body>\n"
0149: + "<p>hello world</p>\n"
0150: + "<form id='form1' action='/formSubmit' method='PoSt'>\n"
0151: + "<input type='text' NAME='textInput1' value='textInput1'/>\n"
0152: + "<input type='text' name='textInput2' value='textInput2'/>\n"
0153: + "<input type='hidden' name='hidden1' value='hidden1'/>\n"
0154: + "<input type='submit' name='submitInput1' value='push me'/>\n"
0155: + "</form>\n" + "</body></html>";
0156: final HtmlPage page = loadPage(htmlContent);
0157: final MockWebConnection webConnection = getMockConnection(page);
0158:
0159: final HtmlForm form = (HtmlForm) page
0160: .getHtmlElementById("form1");
0161: final HtmlInput textInput = form.getInputByName("textInput1");
0162: textInput.setValueAttribute("foo");
0163:
0164: final HtmlSubmitInput button = (HtmlSubmitInput) form
0165: .getInputByName("submitInput1");
0166: final HtmlPage secondPage = (HtmlPage) button.click();
0167:
0168: final List expectedParameters = new ArrayList();
0169: expectedParameters.add(new KeyValuePair("textInput1", "foo"));
0170: expectedParameters.add(new KeyValuePair("textInput2",
0171: "textInput2"));
0172: expectedParameters.add(new KeyValuePair("hidden1", "hidden1"));
0173: expectedParameters.add(new KeyValuePair("submitInput1",
0174: "push me"));
0175:
0176: final URL expectedUrl = new URL(
0177: "http://www.gargoylesoftware.com/formSubmit");
0178: final URL actualUrl = secondPage.getWebResponse().getUrl();
0179: assertEquals("url", expectedUrl, actualUrl);
0180: assertEquals("method", SubmitMethod.POST, webConnection
0181: .getLastMethod());
0182: assertEquals("parameters", expectedParameters, webConnection
0183: .getLastParameters());
0184: assertNotNull(secondPage);
0185: }
0186:
0187: /**
0188: * Test getHtmlElement() for all elements that can be loaded
0189: *
0190: * @throws Exception if the test fails
0191: */
0192: public void testGetHtmlElement() throws Exception {
0193: final String htmlContent = "<html>\n"
0194: + "<head><title>foo</title></head>\n"
0195: + "<body>\n"
0196: + " <p>hello world</p>\n"
0197: + " <form id='form1' id='form1' action='/formSubmit' method='post'>\n"
0198: + " <input type='text' NAME='textInput1' value='textInput1'/>\n"
0199: + " <button type='submit' name='button1'>foobar</button>\n"
0200: + " <select name='select1'>\n"
0201: + " <option value='option1'>Option1</option>\n"
0202: + " </select>\n"
0203: + " <textarea name='textArea1'>foobar</textarea>\n"
0204: + " </form>\n"
0205: + " <a href='http://www.foo.com' name='anchor1'>foo.com</a>\n"
0206: + " <table id='table1'>\n" + " <tr>\n"
0207: + " <th id='header1'>Header</th>\n"
0208: + " <td id='data1'>Data</td>\n"
0209: + " </tr>\n" + " </table>\n"
0210: + "</body></html>";
0211: final HtmlPage page = loadPage(htmlContent);
0212:
0213: final HtmlForm form = (HtmlForm) page
0214: .getHtmlElementById("form1");
0215: assertSame("form1", form, page.getHtmlElementById("form1")); //huh??
0216:
0217: final HtmlInput input = form.getInputByName("textInput1");
0218: assertSame("input1", input, form.getInputByName("textInput1")); //??
0219:
0220: final HtmlButton button = form.getButtonByName("button1");
0221: assertSame("button1", button, form.getButtonByName("button1"));
0222:
0223: final HtmlSelect select = (HtmlSelect) form.getSelectsByName(
0224: "select1").get(0);
0225: assertSame("select1", select, form.getSelectsByName("select1")
0226: .get(0));
0227:
0228: final HtmlOption option = select.getOptionByValue("option1");
0229: assertSame("option1", option, select
0230: .getOptionByValue("option1"));
0231:
0232: final HtmlTable table = (HtmlTable) page
0233: .getHtmlElementById("table1");
0234: assertSame("table1", table, page.getHtmlElementById("table1"));
0235:
0236: final HtmlAnchor anchor = page.getAnchorByName("anchor1");
0237: assertSame("anchor1", anchor, page.getAnchorByName("anchor1"));
0238: assertSame("anchor3", anchor, page
0239: .getAnchorByHref("http://www.foo.com"));
0240: assertSame("anchor4", anchor, page
0241: .getFirstAnchorByText("foo.com"));
0242:
0243: final HtmlTableRow tableRow = table.getRow(0);
0244: assertSame("tableRow1", tableRow, table.getRow(0));
0245:
0246: final HtmlTableHeaderCell tableHeaderCell = (HtmlTableHeaderCell) tableRow
0247: .getCell(0);
0248: assertSame("tableHeaderCell1", tableHeaderCell, tableRow
0249: .getCell(0));
0250: assertSame("tableHeaderCell2", tableHeaderCell, page
0251: .getHtmlElementById("header1"));
0252:
0253: final HtmlTableDataCell tableDataCell = (HtmlTableDataCell) tableRow
0254: .getCell(1);
0255: assertSame("tableDataCell1", tableDataCell, tableRow.getCell(1));
0256: assertSame("tableDataCell2", tableDataCell, page
0257: .getHtmlElementById("data1"));
0258:
0259: final HtmlTextArea textArea = form
0260: .getTextAreaByName("textArea1");
0261: assertSame("textArea1", textArea, form
0262: .getTextAreaByName("textArea1"));
0263: }
0264:
0265: /**
0266: * @throws Exception if the test fails
0267: */
0268: public void testGetTabbableElements_None() throws Exception {
0269: final String htmlContent = "<html>\n"
0270: + "<head><title>foo</title></head>\n" + "<body>\n"
0271: + "<p>hello world</p>\n"
0272: + "<table><tr><td>foo</td></tr></table>\n"
0273: + "</body></html>";
0274:
0275: final HtmlPage page = loadPage(htmlContent);
0276:
0277: assertEquals(Collections.EMPTY_LIST, page.getTabbableElements());
0278: }
0279:
0280: /**
0281: * @throws Exception if the test fails
0282: */
0283: public void testGetTabbableElements_OneEnabled_OneDisabled()
0284: throws Exception {
0285: final String htmlContent = "<html>\n"
0286: + "<head><title>foo</title></head>\n"
0287: + "<body>\n"
0288: + "<form><p>hello world</p>\n"
0289: + "<input name='foo' type='submit' disabled='disabled' id='foo'/>\n"
0290: + "<input name='bar' type='submit' id='bar'/>\n"
0291: + "</form></body></html>";
0292: final HtmlPage page = loadPage(htmlContent);
0293:
0294: final List expectedElements = new ArrayList();
0295: expectedElements.add(page.getHtmlElementById("bar"));
0296:
0297: assertEquals(expectedElements, page.getTabbableElements());
0298: }
0299:
0300: /**
0301: * @throws Exception if the test fails
0302: */
0303: public void testGetTabbableElements() throws Exception {
0304: final String htmlContent = "<html>\n"
0305: + "<head><title>foo</title></head>\n"
0306: + "<body>\n"
0307: + "<a id='a' tabindex='1'>foo</a>\n"
0308: + "<a id='b'>foo</a>\n"
0309: + "<form>\n"
0310: + "<a id='c' tabindex='3'>foo</a>\n"
0311: + "<a id='d' tabindex='2'>foo</a>\n"
0312: + "<a id='e' tabindex='0'>foo</a>\n"
0313: + "</form>\n"
0314: + "<a id='f' tabindex='3'>foo</a>\n"
0315: + "<a id='g' tabindex='1'>foo</a>\n"
0316: + "<a id='q' tabindex='-1'>foo</a>\n"
0317: + "<form><p>hello world</p>\n"
0318: + "<input name='foo' type='submit' disabled='disabled' id='foo'/>\n"
0319: + "<input name='bar' type='submit' id='bar'/>\n"
0320: + "</form></body></html>";
0321: final HtmlPage page = loadPage(htmlContent);
0322:
0323: final List expectedElements = Arrays.asList(new Object[] {
0324: page.getHtmlElementById("a"),
0325: page.getHtmlElementById("g"),
0326: page.getHtmlElementById("d"),
0327: page.getHtmlElementById("c"),
0328: page.getHtmlElementById("f"),
0329: page.getHtmlElementById("e"),
0330: page.getHtmlElementById("b"),
0331: page.getHtmlElementById("bar") });
0332:
0333: assertEquals(expectedElements, page.getTabbableElements());
0334:
0335: final String[] expectedIds = { "a", "g", "d", "c", "f", "e",
0336: "b", "bar" };
0337: assertEquals(expectedIds, page.getTabbableElementIds());
0338: }
0339:
0340: /**
0341: * @throws Exception if the test fails
0342: */
0343: public void testGetHtmlElementByAccessKey() throws Exception {
0344: final String htmlContent = "<html>\n"
0345: + "<head><title>foo</title></head>\n"
0346: + "<body>\n"
0347: + "<a id='a' accesskey='a'>foo</a>\n"
0348: + "<a id='b'>foo</a>\n"
0349: + "<form>\n"
0350: + "<a id='c' accesskey='c'>foo</a>\n"
0351: + "</form>\n"
0352: + "<form><p>hello world</p>\n"
0353: + "<input name='foo' type='submit' disabled='disabled' id='foo' accesskey='f'/>\n"
0354: + "<input name='bar' type='submit' id='bar'/>\n"
0355: + "</form></body></html>";
0356: final HtmlPage page = loadPage(htmlContent);
0357:
0358: assertEquals(page.getHtmlElementById("a"), page
0359: .getHtmlElementByAccessKey('A'));
0360: assertEquals(page.getHtmlElementById("c"), page
0361: .getHtmlElementByAccessKey('c'));
0362: assertNull(page.getHtmlElementByAccessKey('z'));
0363: }
0364:
0365: /**
0366: * @throws Exception if the test fails
0367: */
0368: public void testGetHtmlElementsByAccessKey() throws Exception {
0369: final String htmlContent = "<html>\n"
0370: + "<head><title>foo</title></head><body>\n"
0371: + "<a id='a' accesskey='a'>foo</a>\n"
0372: + "<a id='b' accesskey='a'>foo</a>\n" + "<form>\n"
0373: + "<a id='c' accesskey='c'>foo</a>\n"
0374: + "</form></body></html>";
0375: final HtmlPage page = loadPage(htmlContent);
0376:
0377: final List expectedElements = Arrays.asList(new Object[] {
0378: page.getHtmlElementById("a"),
0379: page.getHtmlElementById("b") });
0380: final List collectedElements = page
0381: .getHtmlElementsByAccessKey('a');
0382: assertEquals(expectedElements, collectedElements);
0383: }
0384:
0385: /**
0386: * @throws Exception if the test fails
0387: * @deprecated
0388: */
0389: public void testAssertAllIdAttributesUnique() throws Exception {
0390: final String htmlContent = "<html>\n"
0391: + "<head><title>foo</title></head>\n"
0392: + "<body>\n"
0393: + "<a id='a' accesskey='a'>foo</a>\n"
0394: + "<a id='b'>foo</a>\n"
0395: + "<form>\n"
0396: + "<a id='c' accesskey='c'>foo</a>\n"
0397: + "</form>\n"
0398: + "<form><p>hello world</p>\n"
0399: + "<input name='foo' type='submit' disabled='disabled' id='foo' accesskey='f'/>\n"
0400: + "<input name='bar' type='submit' id='bar'/>\n"
0401: + "</form></body></html>";
0402: final HtmlPage page = loadPage(htmlContent);
0403:
0404: page.assertAllIdAttributesUnique();
0405: }
0406:
0407: /**
0408: * @throws Exception if the test fails
0409: * @deprecated
0410: */
0411: public void testAssertAllIdAttributesUnique_Duplicates()
0412: throws Exception {
0413: final String htmlContent = "<html>\n"
0414: + "<head><title>foo</title></head>\n"
0415: + "<body>\n"
0416: + "<a id='dupeID' accesskey='a'>foo</a>\n"
0417: + "<a id='b'>foo</a>\n"
0418: + "<form>\n"
0419: + "<a id='c' accesskey='c'>foo</a>\n"
0420: + "</form>\n"
0421: + "<form><p>hello world</p>\n"
0422: + "<input name='foo' type='submit' disabled='disabled' id='dupeID' accesskey='f'/>\n"
0423: + "<input name='bar' type='submit' id='bar'/>\n"
0424: + "</form></body></html>";
0425: final HtmlPage page = loadPage(htmlContent);
0426:
0427: try {
0428: page.assertAllIdAttributesUnique();
0429: fail("Expected AssertionFailedError");
0430: } catch (final AssertionError e) {
0431: assertTrue("dupeID", e.getMessage().indexOf("dupeID") != -1);
0432: }
0433: }
0434:
0435: /**
0436: * @throws Exception if the test fails
0437: * @deprecated
0438: */
0439: public void testAssertAllAccessKeyAttributesUnique()
0440: throws Exception {
0441: final String htmlContent = "<html>\n"
0442: + "<head><title>foo</title></head>\n"
0443: + "<body>\n"
0444: + "<a id='a' accesskey='a'>foo</a>\n"
0445: + "<a id='b'>foo</a>\n"
0446: + "<form>\n"
0447: + "<a id='c' accesskey='c'>foo</a>\n"
0448: + "</form>\n"
0449: + "<form><p>hello world</p>\n"
0450: + "<input name='foo' type='submit' disabled='disabled' id='foo' accesskey='f'/>\n"
0451: + "<input name='bar' type='submit' id='bar'/>\n"
0452: + "</form></body></html>";
0453: final HtmlPage page = loadPage(htmlContent);
0454:
0455: page.assertAllAccessKeyAttributesUnique();
0456: }
0457:
0458: /**
0459: * @throws Exception if the test fails
0460: * @deprecated
0461: */
0462: public void testAssertAllAccessKeyAttributesUnique_Duplicates()
0463: throws Exception {
0464: final String htmlContent = "<html>\n"
0465: + "<head><title>foo</title></head>\n"
0466: + "<body>\n"
0467: + "<a id='a' accesskey='a'>foo</a>\n"
0468: + "<a id='b'>foo</a>\n"
0469: + "<form>\n"
0470: + "<a id='c' accesskey='c'>foo</a>\n"
0471: + "</form>\n"
0472: + "<form><p>hello world</p>\n"
0473: + "<input name='foo' type='submit' disabled='disabled' id='foo' accesskey='f'/>\n"
0474: + "<input name='bar' type='submit' id='bar' accesskey='a'/>\n"
0475: + "</form></body></html>";
0476: final HtmlPage page = loadPage(htmlContent);
0477:
0478: try {
0479: page.assertAllAccessKeyAttributesUnique();
0480: fail("Expected AssertionFailedError");
0481: } catch (final AssertionError e) {
0482: //pass
0483: }
0484: }
0485:
0486: /**
0487: * @throws Exception if the test fails
0488: * @deprecated
0489: */
0490: public void testAssertAllTabIndexAttributesSet() throws Exception {
0491: final String htmlContent = "<html>\n"
0492: + "<head><title>foo</title></head>\n" + "<body>\n"
0493: + "<a id='a' tabindex='1'>foo</a>\n" + "<form>\n"
0494: + "<a id='c' tabindex='3'>foo</a>\n"
0495: + "<a id='d' tabindex='2'>foo</a>\n"
0496: + "<a id='e' tabindex='0'>foo</a>\n" + "</form>\n"
0497: + "<a id='f' tabindex='3'>foo</a>\n"
0498: + "<a id='g' tabindex='1'>foo</a>\n"
0499: + "<a id='q' tabindex='5'>foo</a>\n"
0500: + "<form><p>hello world</p>\n"
0501: + "</form></body></html>";
0502:
0503: final HtmlPage page = loadPage(htmlContent);
0504:
0505: page.assertAllTabIndexAttributesSet();
0506: }
0507:
0508: /**
0509: * @throws Exception if the test fails
0510: * @deprecated
0511: */
0512: public void testAssertAllTabIndexAttributesSet_SomeMissing()
0513: throws Exception {
0514: final String htmlContent = "<html>\n"
0515: + "<head><title>foo</title></head>\n" + "<body>\n"
0516: + "<a id='a' tabindex='1'>foo</a>\n" + "<form>\n"
0517: + "<a id='c' tabindex='3'>foo</a>\n"
0518: + "<a id='d' tabindex='2'>foo</a>\n"
0519: + "<a id='e' tabindex='0'>foo</a>\n" + "</form>\n"
0520: + "<a id='f' tabindex='3'>foo</a>\n"
0521: + "<a id='g'>foo</a>\n"
0522: + "<a id='q' tabindex='1'>foo</a>\n"
0523: + "<form><p>hello world</p>\n"
0524: + "</form></body></html>";
0525:
0526: final HtmlPage page = loadPage(htmlContent);
0527:
0528: try {
0529: page.assertAllTabIndexAttributesSet();
0530: fail("Expected AssertionError");
0531: } catch (final AssertionError e) {
0532: //pass
0533: }
0534: }
0535:
0536: /**
0537: * @throws Exception if the test fails
0538: * @deprecated
0539: */
0540: public void testAssertAllTabIndexAttributesSet_BadValue()
0541: throws Exception {
0542: final String htmlContent = "<html>\n"
0543: + "<head><title>foo</title></head>\n" + "<body>\n"
0544: + "<a id='a' tabindex='1'>foo</a>\n" + "<form>\n"
0545: + "<a id='c' tabindex='3'>foo</a>\n"
0546: + "<a id='d' tabindex='2'>foo</a>\n"
0547: + "<a id='e' tabindex='0'>foo</a>\n" + "</form>\n"
0548: + "<a id='f' tabindex='3'>foo</a>\n"
0549: + "<a id='g' tabindex='1'>foo</a>\n"
0550: + "<a id='q' tabindex='300000'>foo</a>\n"
0551: + "<form><p>hello world</p>\n"
0552: + "</form></body></html>";
0553:
0554: final HtmlPage page = loadPage(htmlContent);
0555:
0556: try {
0557: page.assertAllTabIndexAttributesSet();
0558: fail("Expected AssertionError");
0559: } catch (final AssertionError e) {
0560: //pass
0561: }
0562: }
0563:
0564: /**
0565: * @throws Exception if the test fails
0566: */
0567: public void testGetFullQualifiedUrl_NoBaseSpecified()
0568: throws Exception {
0569: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
0570: + "<form id='form1'>\n"
0571: + "<table><tr><td><input type='text' id='foo'/></td></tr></table>\n"
0572: + "</form></body></html>";
0573: final WebClient client = new WebClient();
0574:
0575: final MockWebConnection webConnection = new MockWebConnection(
0576: client);
0577: webConnection.setDefaultResponse(htmlContent);
0578: client.setWebConnection(webConnection);
0579:
0580: final String urlString = URL_GARGOYLE.toExternalForm();
0581: final HtmlPage page = (HtmlPage) client.getPage(URL_GARGOYLE);
0582:
0583: assertEquals(urlString, page.getFullyQualifiedUrl(""));
0584: assertEquals(urlString + "foo", page
0585: .getFullyQualifiedUrl("foo"));
0586: assertEquals("http://foo.com/bar", page
0587: .getFullyQualifiedUrl("http://foo.com/bar"));
0588: assertEquals("mailto:me@foo.com", page
0589: .getFullyQualifiedUrl("mailto:me@foo.com"));
0590:
0591: assertEquals(urlString + "foo", page
0592: .getFullyQualifiedUrl("foo"));
0593: assertEquals(urlString + "bbb", page
0594: .getFullyQualifiedUrl("aaa/../bbb"));
0595: assertEquals(urlString + "c/d", page
0596: .getFullyQualifiedUrl("c/./d"));
0597:
0598: final HtmlPage secondPage = (HtmlPage) client.getPage(urlString
0599: + "foo/bar?a=b&c=d");
0600: assertEquals(urlString + "foo/bar?a=b&c=d", secondPage
0601: .getFullyQualifiedUrl(""));
0602: assertEquals(urlString + "foo/one", secondPage
0603: .getFullyQualifiedUrl("one"));
0604: assertEquals(urlString + "two", secondPage
0605: .getFullyQualifiedUrl("/two"));
0606: assertEquals(urlString + "foo/two?a=b", secondPage
0607: .getFullyQualifiedUrl("two?a=b"));
0608:
0609: final HtmlPage thirdPage = (HtmlPage) client
0610: .getPage("http://foo.com/dog/cat/one.html");
0611: assertEquals("http://foo.com/dog/cat/one.html", thirdPage
0612: .getFullyQualifiedUrl(""));
0613: assertEquals("http://foo.com/dog/cat/two.html", thirdPage
0614: .getFullyQualifiedUrl("two.html"));
0615: }
0616:
0617: /**
0618: * @throws Exception if the test fails
0619: */
0620: public void testGetFullQualifiedUrl_WithBase() throws Exception {
0621: testGetFullQualifiedUrl_WithBase("http", "");
0622: testGetFullQualifiedUrl_WithBase("http", ":8080");
0623: testGetFullQualifiedUrl_WithBase("https", "");
0624: testGetFullQualifiedUrl_WithBase("https", ":2005");
0625: }
0626:
0627: /**
0628: * @throws Exception if the test fails
0629: */
0630: private void testGetFullQualifiedUrl_WithBase(
0631: final String baseProtocol, final String basePortPart)
0632: throws Exception {
0633:
0634: final String baseUrl = baseProtocol + "://second"
0635: + basePortPart;
0636: final String htmlContent = "<html><head><title>foo</title>\n"
0637: + "<base href='"
0638: + baseUrl
0639: + "'>\n"
0640: + "</head><body>\n"
0641: + "<form id='form1'>\n"
0642: + "<table><tr><td><input type='text' id='foo'/></td></tr></table>\n"
0643: + "</form></body></html>";
0644: final HtmlPage page = loadPage(htmlContent);
0645:
0646: assertEquals(baseUrl, page.getFullyQualifiedUrl(""));
0647: assertEquals(baseUrl + "/foo", page.getFullyQualifiedUrl("foo"));
0648: assertEquals(baseUrl + "/foo.js", page
0649: .getFullyQualifiedUrl("/foo.js"));
0650: assertEquals("http://foo.com/bar", page
0651: .getFullyQualifiedUrl("http://foo.com/bar"));
0652: assertEquals("mailto:me@foo.com", page
0653: .getFullyQualifiedUrl("mailto:me@foo.com"));
0654:
0655: assertEquals(baseUrl + "/bbb", page
0656: .getFullyQualifiedUrl("aaa/../bbb"));
0657: assertEquals(baseUrl + "/c/d", page
0658: .getFullyQualifiedUrl("c/./d"));
0659: }
0660:
0661: /**
0662: * @throws Exception if the test fails
0663: */
0664: public void testGetFullQualifiedUrl_WithInvalidBase()
0665: throws Exception {
0666: final String htmlContent = "<html><head><base href='---****://=='/></head></html>";
0667: final HtmlPage page = loadPage(htmlContent);
0668:
0669: // invalid base url should be ignored
0670: assertEquals("http://somewhere.com/", page
0671: .getFullyQualifiedUrl("http://somewhere.com/"));
0672: assertEquals(page.getWebResponse().getUrl().toExternalForm()
0673: + "foo.html", page.getFullyQualifiedUrl("foo.html"));
0674: }
0675:
0676: /**
0677: * @throws Exception If an error occurs.
0678: */
0679: public void testBase_Multiple() throws Exception {
0680: final String html = "<html>\n" + "<head>\n" + "<base href='"
0681: + URL_SECOND + "'>\n" + "<base href='" + URL_THIRD
0682: + "'>\n" + "</head>\n" + "<body>\n"
0683: + " <a href='somepage.html'>\n" + "</body></html>";
0684:
0685: final WebClient webClient = new WebClient();
0686: final List collectedIncorrectness = new ArrayList();
0687: final IncorrectnessListener listener = new IncorrectnessListener() {
0688: public void notify(final String message, final Object origin) {
0689: collectedIncorrectness.add(message);
0690: }
0691: };
0692: webClient.setIncorrectnessListener(listener);
0693:
0694: final MockWebConnection webConnection = new MockWebConnection(
0695: webClient);
0696: webClient.setWebConnection(webConnection);
0697: webConnection.setDefaultResponse(html);
0698: final HtmlPage page = (HtmlPage) webClient.getPage(URL_FIRST);
0699: final HtmlAnchor anchor = (HtmlAnchor) page.getAnchors().get(0);
0700: anchor.click();
0701:
0702: final String[] expectedIncorrectness = { "Multiple 'base' detected, only the first is used." };
0703: assertEquals(expectedIncorrectness, collectedIncorrectness);
0704: }
0705:
0706: /**
0707: * @throws Exception If an error occurs.
0708: */
0709: public void testBase_InsideBody() throws Exception {
0710: final String html = "<html>\n" + "<head>\n" + "</head>\n"
0711: + "<body>\n" + " <base href='" + URL_SECOND + "'>\n"
0712: + " <a href='somepage.html'>\n" + "</body></html>";
0713:
0714: final WebClient webClient = new WebClient();
0715: final List collectedIncorrectness = new ArrayList();
0716: final IncorrectnessListener listener = new IncorrectnessListener() {
0717: public void notify(final String message, final Object origin) {
0718: collectedIncorrectness.add(message);
0719: }
0720: };
0721: webClient.setIncorrectnessListener(listener);
0722:
0723: final MockWebConnection webConnection = new MockWebConnection(
0724: webClient);
0725: webClient.setWebConnection(webConnection);
0726: webConnection.setDefaultResponse(html);
0727: final HtmlPage page = (HtmlPage) webClient.getPage(URL_FIRST);
0728: final HtmlAnchor anchor = (HtmlAnchor) page.getAnchors().get(0);
0729: final HtmlPage secondPage = (HtmlPage) anchor.click();
0730:
0731: final String[] expectedIncorrectness = { "Element 'base' must appear in <head>, it is ignored." };
0732: assertEquals(expectedIncorrectness, collectedIncorrectness);
0733: assertEquals(URL_FIRST + "/somepage.html", secondPage
0734: .getWebResponse().getUrl());
0735: }
0736:
0737: /**
0738: * @throws Exception if the test fails
0739: */
0740: public void testOnLoadHandler_BodyStatement() throws Exception {
0741: final String htmlContent = "<html><head><title>foo</title>\n"
0742: + "</head><body onLoad='alert(\"foo\")'>\n"
0743: + "</body></html>";
0744: final List collectedAlerts = new ArrayList();
0745: final HtmlPage page = loadPage(htmlContent, collectedAlerts);
0746: assertEquals("foo", page.getTitleText());
0747:
0748: final String[] expectedAlerts = { "foo" };
0749: assertEquals(expectedAlerts, collectedAlerts);
0750: }
0751:
0752: /**
0753: * If the onload handler contains two statements then only the first would execute
0754: * @throws Exception if the test fails
0755: */
0756: public void testOnLoadHandler_TwoBodyStatements() throws Exception {
0757: final String htmlContent = "<html><head><title>foo</title>\n"
0758: + "</head><body onLoad='alert(\"foo\");alert(\"bar\")'>\n"
0759: + "</body></html>";
0760: final List collectedAlerts = new ArrayList();
0761: final HtmlPage page = loadPage(htmlContent, collectedAlerts);
0762: assertEquals("foo", page.getTitleText());
0763:
0764: final String[] expectedAlerts = { "foo", "bar" };
0765: assertEquals(expectedAlerts, collectedAlerts);
0766: }
0767:
0768: /**
0769: * Regression test for bug 713646
0770: * @throws Exception if the test fails
0771: */
0772: public void testOnLoadHandler_BodyName() throws Exception {
0773: final String htmlContent = "<html><head><title>foo</title>\n"
0774: + "<script type='text/javascript'>\n"
0775: + "window.onload=function(){alert('foo')}</script>\n"
0776: + "</head><body></body></html>";
0777: final List collectedAlerts = new ArrayList();
0778: final HtmlPage page = loadPage(htmlContent, collectedAlerts);
0779: assertEquals("foo", page.getTitleText());
0780:
0781: final String[] expectedAlerts = { "foo" };
0782: assertEquals(expectedAlerts, collectedAlerts);
0783: }
0784:
0785: /**
0786: * Regression test for bug 713646
0787: * @throws Exception if the test fails
0788: */
0789: public void testOnLoadHandler_BodyName_NotAFunction()
0790: throws Exception {
0791: final String htmlContent = "<html><head><title>foo</title></head>\n"
0792: + "<body onLoad='foo=4711'>\n"
0793: + "<a name='alert' href='javascript:alert(foo)'/>\n"
0794: + "</body></html>";
0795: final List collectedAlerts = new ArrayList();
0796: final HtmlPage page = loadPage(htmlContent, collectedAlerts);
0797: assertEquals("foo", page.getTitleText());
0798:
0799: page.getAnchorByName("alert").click();
0800:
0801: final String[] expectedAlerts = { "4711" };
0802: assertEquals(expectedAlerts, collectedAlerts);
0803: }
0804:
0805: /**
0806: * Regression test for window.onload property
0807: * @throws Exception if the test fails
0808: */
0809: public void testOnLoadHandler_ScriptName() throws Exception {
0810: final String htmlContent = "<html><head><title>foo</title>\n"
0811: + "<script type='text/javascript'>\n"
0812: + "load=function(){alert('foo')};\n" + "onload=load\n"
0813: + "</script></head><body></body></html>";
0814: final List collectedAlerts = new ArrayList();
0815: final HtmlPage page = loadPage(htmlContent, collectedAlerts);
0816: assertEquals("foo", page.getTitleText());
0817:
0818: final String[] expectedAlerts = { "foo" };
0819: assertEquals(expectedAlerts, collectedAlerts);
0820: }
0821:
0822: /**
0823: * Regression test for window.onload property
0824: * @throws Exception if the test fails
0825: */
0826: public void testOnLoadHandler_ScriptNameRead() throws Exception {
0827: final String htmlContent = "<html><head><title>foo</title>\n"
0828: + "<script type='text/javascript'>\n"
0829: + "load=function(){};\n" + "onload=load;\n"
0830: + "alert(onload);\n"
0831: + "</script></head><body></body></html>";
0832: final List collectedAlerts = new ArrayList();
0833: final HtmlPage page = loadPage(htmlContent, collectedAlerts);
0834: assertEquals("foo", page.getTitleText());
0835:
0836: final String[] expectedAlerts = { "\nfunction () {\n}\n" };
0837: assertEquals(expectedAlerts, collectedAlerts);
0838: }
0839:
0840: /**
0841: * @throws Exception if the test fails
0842: */
0843: public void testEmbeddedMetaTag_Regression() throws Exception {
0844: final String htmlContent = "<html><head><title>foo</title>\n"
0845: + "</head><body>\n"
0846: + "<table><tr><td>\n"
0847: + "<meta name=vs_targetSchema content=\"http://schemas.microsoft.com/intellisense/ie5\">\n"
0848: + "<form name='form1'>\n"
0849: + " <input type='text' name='textfield1' id='textfield1' value='foo' />\n"
0850: + " <input type='text' name='textfield2' id='textfield2'/>\n"
0851: + "</form>\n" + "</td></tr></table>\n"
0852: + "</body></html>";
0853: final List collectedAlerts = new ArrayList();
0854:
0855: // This used to blow up on page load
0856: final HtmlPage page = loadPage(htmlContent, collectedAlerts);
0857: assertEquals("foo", page.getTitleText());
0858:
0859: final List expectedAlerts = Collections.EMPTY_LIST;
0860: assertEquals(expectedAlerts, collectedAlerts);
0861: }
0862:
0863: /**
0864: * @throws Exception if the test fails
0865: */
0866: public void testGetPageEncoding() throws Exception {
0867: final String htmlContent = "<html><head>\n"
0868: + "<title>foo</title>\n"
0869: + "<meta http-equiv='Content-Type' content='text/html ;charset=Shift_JIS'>\n"
0870: + "</head><body>\n"
0871: + "<table><tr><td>\n"
0872: + "<meta name=vs_targetSchema content=\"http://schemas.microsoft.com/intellisense/ie5\">\n"
0873: + "<form name='form1'>\n"
0874: + " <input type='text' name='textfield1' id='textfield1' value='foo' />\n"
0875: + " <input type='text' name='textfield2' id='textfield2'/>\n"
0876: + "</form>\n" + "</td></tr></table>\n"
0877: + "</body></html>";
0878: final HtmlPage page = loadPage(htmlContent);
0879:
0880: assertEquals("Shift_JIS", page.getPageEncoding());
0881: }
0882:
0883: /**
0884: * @throws Exception if the test fails
0885: */
0886: public void testGetForms() throws Exception {
0887: final String htmlContent = "<html>\n"
0888: + "<head><title>foo</title></head>\n"
0889: + "<body>\n"
0890: + "<form name='one'>\n"
0891: + "<a id='c' accesskey='c'>foo</a>\n"
0892: + "</form>\n"
0893: + "<form name='two'>\n"
0894: + "<a id='c' accesskey='c'>foo</a>\n"
0895: + "</form>\n"
0896: + "<input name='foo' type='submit' disabled='disabled' id='foo' accesskey='f'/>\n"
0897: + "<input name='bar' type='submit' id='bar'/>\n"
0898: + "</form></body></html>";
0899:
0900: final HtmlPage page = loadPage(htmlContent);
0901:
0902: final List expectedForms = Arrays.asList(new HtmlForm[] {
0903: page.getFormByName("one"), page.getFormByName("two") });
0904: assertEquals(expectedForms, page.getForms());
0905: }
0906:
0907: /**
0908: * Test auto-refresh from a meta tag.
0909: * @throws Exception if the test fails
0910: */
0911: public void testRefresh_MetaTag_DefaultRefreshHandler()
0912: throws Exception {
0913: final String firstContent = "<html><head><title>first</title>\n"
0914: + "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3;URL="
0915: + URL_SECOND + "\">\n" + "</head><body></body></html>";
0916: final String secondContent = "<html><head><title>second</title></head><body></body></html>";
0917:
0918: final WebClient client = new WebClient();
0919:
0920: final MockWebConnection webConnection = new MockWebConnection(
0921: client);
0922: webConnection.setResponse(URL_FIRST, firstContent);
0923: webConnection.setResponse(URL_SECOND, secondContent);
0924: client.setWebConnection(webConnection);
0925:
0926: final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
0927:
0928: assertEquals("second", page.getTitleText());
0929: }
0930:
0931: /**
0932: * Test auto-refresh from a meta tag with no URL.
0933: * @throws Exception if the test fails
0934: */
0935: public void testRefresh_MetaTag_NoUrl() throws Exception {
0936: final String firstContent = "<html><head><title>first</title>\n"
0937: + "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1\">\n"
0938: + "</head><body></body></html>";
0939:
0940: final WebClient client = new WebClient();
0941: final List collectedItems = new ArrayList();
0942: client.setRefreshHandler(new LoggingRefreshHandler(
0943: collectedItems));
0944:
0945: final MockWebConnection webConnection = new MockWebConnection(
0946: client);
0947: webConnection.setResponse(URL_FIRST, firstContent);
0948: client.setWebConnection(webConnection);
0949:
0950: client.getPage(URL_FIRST);
0951:
0952: // avoid using equals() on URL because it takes to much time (due to ip resolution)
0953: assertEquals("first", collectedItems.get(0));
0954: assertEquals(URL_FIRST, (URL) collectedItems.get(1));
0955: assertEquals(new Integer(1), collectedItems.get(2));
0956: }
0957:
0958: /**
0959: * Ensures that if a page is supposed to refresh itself every certain amount of
0960: * time, and the ImmediateRefreshHandler is being used, an OOME is avoided by
0961: * not performing the refresh.
0962: * @throws Exception if the test fails
0963: */
0964: public void testRefresh_ImmediateRefresh_AvoidOOME()
0965: throws Exception {
0966: final String firstContent = "<html><head><title>first</title>\n"
0967: + "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1\">\n"
0968: + "</head><body></body></html>";
0969:
0970: final WebClient client = new WebClient();
0971: assertInstanceOf(client.getRefreshHandler(),
0972: ImmediateRefreshHandler.class);
0973: try {
0974: loadPage(firstContent);
0975: fail("should have thrown");
0976: } catch (final RuntimeException e) {
0977: assertTrue(e.getMessage().indexOf(
0978: "could have caused an OutOfMemoryError") > -1);
0979: }
0980: Thread.sleep(1000);
0981: }
0982:
0983: /**
0984: * Test auto-refresh from a meta tag with url quoted.
0985: * @throws Exception if the test fails
0986: */
0987: public void testRefresh_MetaTagQuoted() throws Exception {
0988: final String firstContent = "<html><head><title>first</title>\n"
0989: + "<META HTTP-EQUIV='Refresh' CONTENT='0;URL=\""
0990: + URL_SECOND + "\"'>\n" + "</head><body></body></html>";
0991: final String secondContent = "<html><head><title>second</title></head><body></body></html>";
0992:
0993: final WebClient client = new WebClient();
0994:
0995: final MockWebConnection webConnection = new MockWebConnection(
0996: client);
0997: webConnection.setResponse(URL_FIRST, firstContent);
0998: webConnection.setResponse(URL_SECOND, secondContent);
0999: client.setWebConnection(webConnection);
1000:
1001: final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
1002:
1003: assertEquals("second", page.getTitleText());
1004: }
1005:
1006: /**
1007: * Test auto-refresh from a meta tag with url partly quoted.
1008: * @throws Exception if the test fails
1009: */
1010: public void testRefresh_MetaTagPartlyQuoted() throws Exception {
1011: final String firstContent = "<html><head><title>first</title>\n"
1012: + "<META HTTP-EQUIV='Refresh' CONTENT=\"0;URL='"
1013: + URL_SECOND + "\">\n" + "</head><body></body></html>";
1014: final String secondContent = "<html><head><title>second</title></head><body></body></html>";
1015:
1016: final WebClient client = new WebClient();
1017:
1018: final MockWebConnection webConnection = new MockWebConnection(
1019: client);
1020: webConnection.setResponse(URL_FIRST, firstContent);
1021: webConnection.setResponse(URL_SECOND, secondContent);
1022: client.setWebConnection(webConnection);
1023:
1024: final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
1025:
1026: assertEquals("second", page.getTitleText());
1027: }
1028:
1029: /**
1030: * Test auto-refresh from a meta tag inside noscript.
1031: * @throws Exception if the test fails
1032: */
1033: public void testRefresh_MetaTagNoScript() throws Exception {
1034: final String firstContent = "<html><head><title>first</title>\n"
1035: + "<noscript>\n"
1036: + "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL="
1037: + URL_SECOND
1038: + "\">\n"
1039: + "</noscript>\n"
1040: + "</head><body></body></html>";
1041: final String secondContent = "<html><head><title>second</title></head><body></body></html>";
1042:
1043: final WebClient client = new WebClient();
1044:
1045: final MockWebConnection webConnection = new MockWebConnection(
1046: client);
1047: webConnection.setResponse(URL_FIRST, firstContent);
1048: webConnection.setResponse(URL_SECOND, secondContent);
1049: client.setWebConnection(webConnection);
1050:
1051: HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
1052: assertEquals("first", page.getTitleText());
1053:
1054: client.setJavaScriptEnabled(false);
1055: page = (HtmlPage) client.getPage(URL_FIRST);
1056: assertEquals("second", page.getTitleText());
1057: }
1058:
1059: /**
1060: * Test auto-refresh from a meta tag with a refresh handler that doesn't refresh.
1061: * @throws Exception if the test fails
1062: */
1063: public void testRefresh_MetaTag_CustomRefreshHandler()
1064: throws Exception {
1065: final String firstContent = "<html><head><title>first</title>\n"
1066: + "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3;URL="
1067: + URL_SECOND + "\">\n" + "</head><body></body></html>";
1068: final String secondContent = "<html><head><title>second</title></head><body></body></html>";
1069:
1070: final WebClient client = new WebClient();
1071: final List collectedItems = new ArrayList();
1072: client.setRefreshHandler(new LoggingRefreshHandler(
1073: collectedItems));
1074:
1075: final MockWebConnection webConnection = new MockWebConnection(
1076: client);
1077: webConnection.setResponse(URL_FIRST, firstContent);
1078: webConnection.setResponse(URL_SECOND, secondContent);
1079: client.setWebConnection(webConnection);
1080:
1081: final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
1082:
1083: assertEquals("first", page.getTitleText());
1084:
1085: // avoid using equals() on URL because it takes to much time (due to ip resolution)
1086: assertEquals("first", collectedItems.get(0));
1087: assertEquals(URL_SECOND, ((URL) collectedItems.get(1)));
1088: assertEquals(new Integer(3), collectedItems.get(2));
1089: }
1090:
1091: /**
1092: * Test that whitespace before and after ';' is permitted.
1093: *
1094: * @throws Exception if the test fails
1095: */
1096: public void testRefresh_MetaTag_Whitespace() throws Exception {
1097: final String firstContent = "<html><head><title>first</title>\n"
1098: + "<META HTTP-EQUIV='Refresh' CONTENT='0 ; URL="
1099: + URL_SECOND + "'>\n" + "</head><body></body></html>";
1100: final String secondContent = "<html><head><title>second</title></head><body></body></html>";
1101:
1102: final WebClient client = new WebClient();
1103:
1104: final MockWebConnection webConnection = new MockWebConnection(
1105: client);
1106: webConnection.setResponse(URL_FIRST, firstContent);
1107: webConnection.setResponse(URL_SECOND, secondContent);
1108: client.setWebConnection(webConnection);
1109:
1110: final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
1111:
1112: assertEquals("second", page.getTitleText());
1113: }
1114:
1115: /**
1116: * Test auto-refresh from a response header.
1117: * @throws Exception if the test fails
1118: */
1119: public void testRefresh_HttpResponseHeader() throws Exception {
1120: final String firstContent = "<html><head><title>first</title>\n"
1121: + "</head><body></body></html>";
1122: final String secondContent = "<html><head><title>second</title></head><body></body></html>";
1123:
1124: final WebClient client = new WebClient();
1125:
1126: final MockWebConnection webConnection = new MockWebConnection(
1127: client);
1128: webConnection.setResponse(URL_FIRST, firstContent, 200, "OK",
1129: "text/html", Collections
1130: .singletonList(new KeyValuePair("Refresh",
1131: "3;URL=" + URL_SECOND + "")));
1132: webConnection.setResponse(URL_SECOND, secondContent);
1133: client.setWebConnection(webConnection);
1134:
1135: final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
1136:
1137: assertEquals("second", page.getTitleText());
1138: }
1139:
1140: /**
1141: * Test that the parent of the DOM Document (HtmlPage) is null.
1142: * @throws Exception if the test fails
1143: */
1144: public void testDocumentParentIsNull() throws Exception {
1145: final String htmlContent = "<html>\n"
1146: + "<head><title>foo</title></head>\n" + "<body>\n"
1147: + "</body></html>";
1148: final HtmlPage page = loadPage(htmlContent);
1149:
1150: assertNotNull(page);
1151: assertNull(page.getParentDomNode());
1152: }
1153:
1154: /**
1155: * @throws Exception if the test fails
1156: */
1157: public void testDocumentElement() throws Exception {
1158: final String htmlContent = "<html>\n"
1159: + "<head><title>foo</title></head>\n" + "<body>\n"
1160: + "</body></html>";
1161: final HtmlPage page = loadPage(htmlContent);
1162:
1163: final HtmlElement root = page.getDocumentHtmlElement();
1164:
1165: assertNotNull(root);
1166: assertEquals("html", root.getTagName());
1167: assertSame(page, root.getParentDomNode());
1168: }
1169:
1170: /**
1171: * @throws Exception if the test fails
1172: */
1173: public void testDocumentNodeType() throws Exception {
1174: final String htmlContent = "<html>\n"
1175: + "<head><title>foo</title></head>\n" + "<body>\n"
1176: + "</body></html>";
1177: final HtmlPage page = loadPage(htmlContent);
1178:
1179: final HtmlElement root = page.getDocumentHtmlElement();
1180:
1181: assertEquals(org.w3c.dom.Node.DOCUMENT_NODE, page.getNodeType());
1182: assertEquals(org.w3c.dom.Node.ELEMENT_NODE, root.getNodeType());
1183: assertEquals("#document", page.getNodeName());
1184: }
1185:
1186: /**
1187: *
1188: * @throws Exception if the test fails
1189: */
1190: public void testDeregisterFrameWithoutSrc() throws Exception {
1191: final String htmlContent = "<html>\n"
1192: + "<head><title>foo</title></head>\n" + "<body>\n"
1193: + "<iframe />\n" + "<a href='about:blank'>link</a>\n"
1194: + "</body></html>";
1195:
1196: final HtmlPage page = loadPage(htmlContent);
1197: final HtmlAnchor link = (HtmlAnchor) page.getAnchors().get(0);
1198: link.click();
1199: }
1200:
1201: /**
1202: * Test that a return statement in onload doesn't throw any exception
1203: * @throws Exception if the test fails
1204: */
1205: public void testOnLoadReturn() throws Exception {
1206: final String htmlContent = "<html><head><title>foo</title></head>\n"
1207: + "<body onload='return true'>\n" + "</body></html>";
1208:
1209: loadPage(htmlContent);
1210: }
1211:
1212: /**
1213: * Test that wrong formed html code is parsed like browsers do
1214: * @throws Exception if the test fails
1215: */
1216: public void testWrongHtml_TagBeforeHtml() throws Exception {
1217: if (notYetImplemented()) {
1218: return;
1219: }
1220:
1221: final String htmlContent = "<div>\n" + "<html>\n"
1222: + "<head><title>foo</title>\n" + "<script>\n"
1223: + "var toto = 12345;\n" + "</script>\n" + "</head>\n"
1224: + "<body onload='alert(toto)'>\n" + "blabla"
1225: + "</body>\n" + "</html>";
1226: final List collectedAlerts = new ArrayList();
1227: final HtmlPage page = loadPage(htmlContent, collectedAlerts);
1228: final String[] expectedAlerts = { "12345" };
1229: createTestPageForRealBrowserIfNeeded(htmlContent,
1230: expectedAlerts);
1231:
1232: assertEquals("foo", page.getTitleText());
1233:
1234: assertEquals(expectedAlerts, collectedAlerts);
1235: }
1236:
1237: /**
1238: * @exception Exception If the test fails
1239: */
1240: public void testAsXml() throws Exception {
1241: final String htmlContent = "<html><head><title>foo</title></head>"
1242: + "<body><p>helloworld</p></body>" + "</html>";
1243:
1244: final HtmlPage page = loadPage(htmlContent);
1245: assertEquals(htmlContent, page.asXml().replaceAll("\\s", ""));
1246: }
1247:
1248: /**
1249: * Test that the generated xml is valid as html code too
1250: * @exception Exception If the test fails
1251: */
1252: public void testAsXmlValidHtmlOutput() throws Exception {
1253: final String html = "<html><head><title>foo</title>"
1254: + "<script src='script.js'></script></head>"
1255: + "<body><div></div><iframe src='about:blank'></iframe></body>"
1256: + "</html>";
1257:
1258: final WebClient client = new WebClient();
1259: final MockWebConnection webConnection = new MockWebConnection(
1260: client);
1261: webConnection.setDefaultResponse(html);
1262: webConnection.setResponse(new URL(URL_FIRST, "script.js"), "",
1263: "text/javascript");
1264: client.setWebConnection(webConnection);
1265:
1266: final HtmlPage page = (HtmlPage) client.getPage(URL_FIRST);
1267:
1268: assertEquals(html, page.asXml().replaceAll("[\\n\\r]", "")
1269: .replaceAll("\\s\\s+", "").replaceAll("\"", "'"));
1270: }
1271:
1272: /**
1273: * @exception Exception If the test fails
1274: */
1275: public void testAsXml2() throws Exception {
1276: final String htmlContent = "<html><head><title>foo</title>\n"
1277: + "<script>var test = 15 < 16;</script></head>\n"
1278: + "</head>\n"
1279: + "<body onload='test=(1 > 2) && (45 < 78)'><p>helloworld &amp; helloall</p></body>\n"
1280: + "</html>";
1281:
1282: final HtmlPage page = loadPage(htmlContent);
1283: assertNotNull("xml document could not be parsed", page.asXml());
1284: final DocumentBuilderFactory factory = DocumentBuilderFactory
1285: .newInstance();
1286: final DocumentBuilder builder = factory.newDocumentBuilder();
1287: builder.parse(TextUtil.toInputStream(page.asXml()));
1288: }
1289:
1290: /**
1291: * Regression test for bug 1204637
1292: * @exception Exception If the test fails
1293: */
1294: public void testIsJavascript() throws Exception {
1295: assertTrue(HtmlPage.isJavaScript("text/javascript", null));
1296: assertTrue(HtmlPage.isJavaScript("text/JavaScript", null));
1297: }
1298:
1299: /**
1300: * @exception Exception if the test fails
1301: */
1302: public void testGetHtmlElementsByName() throws Exception {
1303: final String html = "<html><body><div name='a'>foo</div><div name='b'/><div name='b'/></body></html>";
1304: final HtmlPage page = loadPage(html);
1305: assertEquals(1, page.getHtmlElementsByName("a").size());
1306: assertEquals(2, page.getHtmlElementsByName("b").size());
1307: assertEquals(0, page.getHtmlElementsByName("c").size());
1308:
1309: final HtmlElement a = (HtmlElement) page.getHtmlElementsByName(
1310: "a").get(0);
1311: a.remove();
1312: assertEquals(0, page.getHtmlElementsByName("a").size());
1313:
1314: final HtmlElement b1 = (HtmlElement) page
1315: .getHtmlElementsByName("b").get(0);
1316: b1.appendDomChild(a);
1317: assertEquals(1, page.getHtmlElementsByName("a").size());
1318: }
1319:
1320: /**
1321: * Regression test for bug 1233519
1322: * @exception Exception If the test fails
1323: */
1324: public void testGetHtmlElementByIdAfterRemove() throws Exception {
1325: final String htmlContent = "<html><head><title>foo</title></head>\n"
1326: + "<body>\n"
1327: + "<div id='div1'>\n"
1328: + "<div id='div2'>\n"
1329: + "</div>\n"
1330: + "</div>\n"
1331: + "</body>\n" + "</html>";
1332:
1333: final HtmlPage page = loadPage(htmlContent);
1334: final HtmlElement div1 = page.getHtmlElementById("div1");
1335: page.getHtmlElementById("div2"); // would throw if not found
1336: div1.remove();
1337: try {
1338: page.getHtmlElementById("div1"); // throws if not found
1339: fail("div1 should have been removed");
1340: } catch (final ElementNotFoundException e) {
1341: // nothing
1342: }
1343:
1344: try {
1345: page.getHtmlElementById("div2"); // throws if not found
1346: fail("div2 should have been removed");
1347: } catch (final ElementNotFoundException e) {
1348: // nothing
1349: }
1350: }
1351:
1352: /**
1353: * Test getHtmlElementById() when 2 elements have the same id and the first one
1354: * is removed.
1355: * @exception Exception If the test fails
1356: */
1357: public void testGetHtmlElementById_idTwice() throws Exception {
1358: final String htmlContent = "<html><head><title>foo</title></head>\n"
1359: + "<body>\n"
1360: + "<div id='id1'>foo</div>\n"
1361: + "<span id='id1'>bla</span>\n"
1362: + "</body>\n"
1363: + "</html>";
1364:
1365: final HtmlPage page = loadPage(htmlContent);
1366: final HtmlElement elt1 = page.getHtmlElementById("id1");
1367: assertEquals("div", elt1.getNodeName());
1368: elt1.remove();
1369: assertEquals("span", page.getHtmlElementById("id1")
1370: .getNodeName());
1371: }
1372:
1373: /**
1374: * Test the set-cookie meta tag
1375: * @throws Exception if the test fails
1376: */
1377: public void testSetCookieMetaTag() throws Exception {
1378: final String content = "<html><head><title>first</title>\n"
1379: + "<meta http-equiv='set-cookie' content='webm=none; path=/;'>\n"
1380: + "</head><body>\n"
1381: + "<script>alert(document.cookie)</script>\n"
1382: + "</body></html>";
1383:
1384: final String[] expectedAlerts = { "webm=none" };
1385: createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
1386:
1387: final List collectedAlerts = new ArrayList();
1388: final HtmlPage page = loadPage(content, collectedAlerts);
1389: assertEquals(expectedAlerts, collectedAlerts);
1390:
1391: final Cookie[] cookies = page.getWebClient().getWebConnection()
1392: .getState().getCookies();
1393: assertEquals(1, cookies.length);
1394: final Cookie cookie = cookies[0];
1395: assertEquals(page.getWebResponse().getUrl().getHost(), cookie
1396: .getDomain());
1397: assertEquals("webm", cookie.getName());
1398: assertEquals("none", cookie.getValue());
1399: assertEquals("/", cookie.getPath());
1400: }
1401:
1402: /**
1403: * Regression test for bug 1658273
1404: * @throws Exception if the test fails
1405: */
1406: public void testOnLoadHandler_idChange() throws Exception {
1407: final String content = "<html><head><title>foo</title>\n"
1408: + "<div id='id1' class='cl1'><div id='id2' class='cl2'></div></div>'"
1409: + "<script type='text/javascript'>\n"
1410: + "document.getElementById('id1').id = 'id3';\n"
1411: + "alert(document.getElementById('id2').className);\n"
1412: + "alert(document.getElementById('id3').className);\n"
1413: + "</script>\n" + "</head><body></body></html>";
1414:
1415: final String[] expectedAlerts = { "cl2", "cl1" };
1416: createTestPageForRealBrowserIfNeeded(content, expectedAlerts);
1417:
1418: final List collectedAlerts = new ArrayList();
1419: loadPage(content, collectedAlerts);
1420:
1421: assertEquals(expectedAlerts, collectedAlerts);
1422: }
1423:
1424: /**
1425: * Test for bug 1714767
1426: * @throws Exception if the test fails
1427: */
1428: public void testNoSlashURL() throws Exception {
1429: testNoSlashURL("http:/second");
1430: testNoSlashURL("http:second");
1431: }
1432:
1433: private void testNoSlashURL(final String url) throws Exception {
1434: final String firstContent = "<html><body>\n"
1435: + "<iframe id='myIFrame' src='" + url + "'></iframe>\n"
1436: + "</body></html>";
1437:
1438: final String secondContent = "<html><body></body></html>";
1439: final WebClient client = new WebClient(BrowserVersion.FIREFOX_2);
1440:
1441: final MockWebConnection webConnection = new MockWebConnection(
1442: client);
1443: webConnection.setResponse(URL_FIRST, firstContent);
1444: webConnection.setResponse(URL_SECOND, secondContent);
1445:
1446: client.setWebConnection(webConnection);
1447:
1448: final HtmlPage firstPage = (HtmlPage) client.getPage(URL_FIRST);
1449: final HtmlInlineFrame iframe = (HtmlInlineFrame) firstPage
1450: .getHtmlElementById("myIFrame");
1451:
1452: assertEquals(URL_SECOND, iframe.getEnclosedPage()
1453: .getWebResponse().getUrl());
1454: }
1455:
1456: /**
1457: * @throws Exception failure
1458: */
1459: public void testMetaTagWithEmptyURL() throws Exception {
1460: final WebClient client = new WebClient();
1461: client.setRefreshHandler(new ImmediateRefreshHandler());
1462:
1463: // connection will return a page with <meta ... refresh> for the first call
1464: // and the same page without it for the other calls
1465: final MockWebConnection webConnection = new MockWebConnection(
1466: client) {
1467: private int nbCalls_ = 0;
1468:
1469: public WebResponse getResponse(
1470: final WebRequestSettings settings)
1471: throws IOException {
1472: String content = "<html><head>\n";
1473: if (nbCalls_ == 0) {
1474: content += "<meta http-equiv='refresh' content='1; URL='>\n";
1475: }
1476: content += "</head><body></body></html>";
1477: nbCalls_++;
1478: return new StringWebResponse(content, settings.getURL()) {
1479: private static final long serialVersionUID = 4945986137562358686L;
1480:
1481: public SubmitMethod getRequestMethod() {
1482: return settings.getSubmitMethod();
1483: }
1484: };
1485: }
1486: };
1487: client.setWebConnection(webConnection);
1488:
1489: final WebRequestSettings settings = new WebRequestSettings(
1490: URL_GARGOYLE);
1491: settings.setSubmitMethod(SubmitMethod.POST);
1492: client.getPage(settings);
1493: }
1494:
1495: /**
1496: * @throws Exception If the test fails
1497: */
1498: public void testSerialization() throws Exception {
1499: final String content = "<html><body>\n"
1500: + "<div id='myId'>Hello there!</div>\n"
1501: + "</body></html>";
1502:
1503: final HtmlPage page1 = loadPage(content);
1504:
1505: final ByteArrayOutputStream byteOS = new ByteArrayOutputStream();
1506: final ObjectOutputStream objectOS = new ObjectOutputStream(
1507: byteOS);
1508: objectOS.writeObject(page1);
1509:
1510: final ByteArrayInputStream byteIS = new ByteArrayInputStream(
1511: byteOS.toByteArray());
1512: final ObjectInputStream objectIS = new ObjectInputStream(byteIS);
1513: final HtmlPage page2 = (HtmlPage) objectIS.readObject();
1514:
1515: final Iterator iterator1 = page1.getAllHtmlChildElements();
1516: final Iterator iterator2 = page2.getAllHtmlChildElements();
1517: while (iterator1.hasNext()) {
1518: assertTrue(iterator2.hasNext());
1519: final HtmlElement element1 = (HtmlElement) iterator1.next();
1520: final HtmlElement element2 = (HtmlElement) iterator2.next();
1521: assertEquals(element1.getNodeName(), element2.getNodeName());
1522: }
1523: assertFalse(iterator2.hasNext());
1524: assertEquals("Hello there!", page2.getHtmlElementById("myId")
1525: .getFirstDomChild().getNodeValue());
1526: }
1527:
1528: /**
1529: * Verifies that a cloned HtmlPage has its own idMap_
1530: * @throws Exception if the test fails
1531: */
1532: public void testClonedPageHasOwnIdMap() throws Exception {
1533: final String content = "<html><head><title>foo</title>"
1534: + "<body>"
1535: + "<div id='id1' class='cl1'><div id='id2' class='cl2'></div></div>"
1536: + "</body></html>";
1537:
1538: final HtmlPage page = loadPage(content);
1539: final HtmlElement id1 = (HtmlElement) page
1540: .getDocumentHtmlElement().getLastDomChild()
1541: .getLastDomChild();
1542: assertEquals("id1", id1.getId());
1543: assertSame(id1, page.getHtmlElementById("id1"));
1544: final HtmlPage clone = (HtmlPage) page.cloneDomNode(true);
1545: assertSame(id1, page.getHtmlElementById("id1"));
1546: final HtmlElement id1clone = (HtmlElement) clone
1547: .getDocumentHtmlElement().getLastDomChild()
1548: .getLastDomChild();
1549: assertNotSame(id1, id1clone);
1550: assertEquals("id1", id1clone.getId());
1551: assertSame(id1clone, clone.getHtmlElementById("id1"));
1552: assertNotSame(id1clone, page.getHtmlElementById("id1"));
1553: assertNotSame(id1, clone.getHtmlElementById("id1"));
1554:
1555: page.getHtmlElementById("id2").remove();
1556: try {
1557: page.getHtmlElementById("id2");
1558: fail("should have thrown ElementNotFoundException");
1559: } catch (final ElementNotFoundException enfe) {
1560: // expected
1561: }
1562: assertNotNull(clone.getHtmlElementById("id2"));
1563: }
1564:
1565: /**
1566: * Verifies that a cloned HtmlPage has its own documentElement
1567: * @throws Exception if the test fails
1568: */
1569: public void testClonedPageHasOwnDocumentElement() throws Exception {
1570: final String content = "<html><head><title>foo</title>\n"
1571: + "<body>\n"
1572: + "<div id='id1' class='cl1'><div id='id2' class='cl2'></div></div>\n"
1573: + "</body></html>";
1574:
1575: final HtmlPage page = loadPage(content);
1576: final HtmlPage clone = (HtmlPage) page.cloneDomNode(true);
1577: assertTrue(page != clone);
1578: final HtmlElement doc = page.getDocumentHtmlElement();
1579: final HtmlElement docclone = clone.getDocumentHtmlElement();
1580: assertTrue(doc != docclone);
1581: }
1582:
1583: /**
1584: * @throws Exception if the test fails
1585: */
1586: public void testHtmlAttributeChangeListener_AddAttribute()
1587: throws Exception {
1588: final String htmlContent = "<html><head><title>foo</title>\n"
1589: + "<script>\n"
1590: + " function clickMe() {\n"
1591: + " var p1 = document.getElementById('p1');\n"
1592: + " p1.setAttribute('title', 'myTitle');\n"
1593: + " }\n"
1594: + "</script>\n"
1595: + "</head>\n"
1596: + "<body>\n"
1597: + "<p id='p1'></p>\n"
1598: + "<input id='myButton' type='button' onclick='clickMe()'>\n"
1599: + "</body></html>";
1600:
1601: final String[] expectedValues = { "attributeAdded: p,title,myTitle" };
1602: final HtmlPage page = loadPage(htmlContent);
1603: final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl();
1604: page.addHtmlAttributeChangeListener(listenerImpl);
1605: final HtmlButtonInput myButton = (HtmlButtonInput) page
1606: .getHtmlElementById("myButton");
1607:
1608: myButton.click();
1609: assertEquals(expectedValues, listenerImpl.getCollectedValues());
1610: }
1611:
1612: /**
1613: * @throws Exception if the test fails
1614: */
1615: public void testHtmlAttributeChangeListener_ReplaceAttribute()
1616: throws Exception {
1617: final String htmlContent = "<html><head><title>foo</title>\n"
1618: + "<script>\n"
1619: + " function clickMe() {\n"
1620: + " var p1 = document.getElementById('p1');\n"
1621: + " p1.setAttribute('title', p1.getAttribute('title') + 'a');\n"
1622: + " }\n"
1623: + "</script>\n"
1624: + "</head>\n"
1625: + "<body>\n"
1626: + "<p id='p1' title='myTitle'></p>\n"
1627: + "<input id='myButton' type='button' onclick='clickMe()'>\n"
1628: + "</body></html>";
1629:
1630: final String[] expectedValues = { "attributeReplaced: p,title,myTitle" };
1631: final HtmlPage page = loadPage(htmlContent);
1632: final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl();
1633: page.addHtmlAttributeChangeListener(listenerImpl);
1634: final HtmlButtonInput myButton = (HtmlButtonInput) page
1635: .getHtmlElementById("myButton");
1636:
1637: myButton.click();
1638: assertEquals(expectedValues, listenerImpl.getCollectedValues());
1639: }
1640:
1641: /**
1642: * @throws Exception if the test fails
1643: */
1644: public void testHtmlAttributeChangeListener_RemoveAttribute()
1645: throws Exception {
1646: final String htmlContent = "<html><head><title>foo</title>\n"
1647: + "<script>\n"
1648: + " function clickMe() {\n"
1649: + " var p1 = document.getElementById('p1');\n"
1650: + " p1.removeAttribute('title');\n"
1651: + " }\n"
1652: + "</script>\n"
1653: + "</head>\n"
1654: + "<body>\n"
1655: + "<p id='p1' title='myTitle'></p>\n"
1656: + "<input id='myButton' type='button' onclick='clickMe()'>\n"
1657: + "</body></html>";
1658:
1659: final String[] expectedValues = { "attributeRemoved: p,title,myTitle" };
1660: final HtmlPage page = loadPage(htmlContent);
1661: final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl();
1662: page.addHtmlAttributeChangeListener(listenerImpl);
1663: final HtmlButtonInput myButton = (HtmlButtonInput) page
1664: .getHtmlElementById("myButton");
1665:
1666: myButton.click();
1667: assertEquals(expectedValues, listenerImpl.getCollectedValues());
1668: }
1669:
1670: /**
1671: * @throws Exception if the test fails
1672: */
1673: public void testHtmlAttributeChangeListener_RemoveListener()
1674: throws Exception {
1675: final String htmlContent = "<html><head><title>foo</title>\n"
1676: + "<script>\n"
1677: + " function clickMe() {\n"
1678: + " var p1 = document.getElementById('p1');\n"
1679: + " p1.setAttribute('title', p1.getAttribute('title') + 'a');\n"
1680: + " }\n"
1681: + "</script>\n"
1682: + "</head>\n"
1683: + "<body>\n"
1684: + "<p id='p1' title='myTitle'></p>\n"
1685: + "<input id='myButton' type='button' onclick='clickMe()'>\n"
1686: + "</body></html>";
1687:
1688: final String[] expectedValues = { "attributeReplaced: p,title,myTitle" };
1689: final HtmlPage page = loadPage(htmlContent);
1690: final HtmlAttributeChangeListenerTestImpl listenerImpl = new HtmlAttributeChangeListenerTestImpl();
1691: page.addHtmlAttributeChangeListener(listenerImpl);
1692: final HtmlButtonInput myButton = (HtmlButtonInput) page
1693: .getHtmlElementById("myButton");
1694:
1695: myButton.click();
1696: page.removeHtmlAttributeChangeListener(listenerImpl);
1697: myButton.click();
1698: assertEquals(expectedValues, listenerImpl.getCollectedValues());
1699: }
1700:
1701: /**
1702: * @throws Exception if the test fails
1703: */
1704: public void testHtmlAttributeChangeListener_ListenerRegistersNewListener()
1705: throws Exception {
1706: final String htmlContent = "<html><head><title>foo</title>\n"
1707: + "</head>\n" + "<body>\n"
1708: + "<p id='p1' title='myTitle'></p>\n"
1709: + "</body></html>";
1710:
1711: final HtmlPage page = loadPage(htmlContent);
1712:
1713: final List collector = new ArrayList();
1714: final HtmlAttributeChangeListener listener2 = new HtmlAttributeChangeListenerTestImpl() {
1715: public void attributeReplaced(
1716: final HtmlAttributeChangeEvent event) {
1717: collector.add("in listener 2");
1718: }
1719: };
1720: final HtmlAttributeChangeListener listener1 = new HtmlAttributeChangeListenerTestImpl() {
1721: public void attributeReplaced(
1722: final HtmlAttributeChangeEvent event) {
1723: collector.add("in listener 1");
1724: page.addHtmlAttributeChangeListener(listener2);
1725: }
1726: };
1727:
1728: page.addHtmlAttributeChangeListener(listener1);
1729:
1730: final HtmlElement p = page.getHtmlElementById("p1");
1731: p.setAttributeValue("title", "new title");
1732:
1733: final String[] expectedValues = { "in listener 1" };
1734: assertEquals(expectedValues, collector);
1735: collector.clear();
1736:
1737: p.setAttributeValue("title", "new new title");
1738: final String[] expectedValues2 = { "in listener 1",
1739: "in listener 2" };
1740: assertEquals(expectedValues2, collector);
1741: }
1742:
1743: /**
1744: * @throws Exception if the test fails
1745: */
1746: public void testCaseInsensitiveRegexReplacement() throws Exception {
1747: final String html = "<html><body><script>\n"
1748: + "var r = /^([#.]?)([a-z0-9\\*_-]*)/i;\n"
1749: + "var s = '#userAgent';\n" + "s = s.replace(r, '');\n"
1750: + "alert(s.length);\n" + "</script></body></html>";
1751: final String[] expectedAlerts = { "0" };
1752: final List actualAlerts = new ArrayList();
1753: loadPage(html, actualAlerts);
1754: assertEquals(expectedAlerts, actualAlerts);
1755: }
1756:
1757: /**
1758: * @throws Exception if the test fails
1759: */
1760: public void testRegexReplacementWithFunction() throws Exception {
1761: final String html = "<html><body><script>\n"
1762: + "var r = /-([a-z])/ig;\n"
1763: + "var s = 'font-size';\n"
1764: + "s = s.replace(r, function(z,b){return b.toUpperCase();});\n"
1765: + "alert(s);\n" + "</script></body></html>";
1766: final String[] expectedAlerts = { "fontSize" };
1767: final List collectedAlerts = new ArrayList();
1768: loadPage(html, collectedAlerts);
1769: assertEquals(expectedAlerts, collectedAlerts);
1770: }
1771:
1772: /**
1773: * @throws Exception if the test fails
1774: */
1775: public void testTitle_EmptyXmlTagExpanded() throws Exception {
1776: final String content = "<html><head><title/></head>"
1777: + "<body>Hello World!</body></html>";
1778: final HtmlPage page = loadPage(content);
1779: assertTrue(page.asXml().indexOf("</title>") != -1);
1780: }
1781:
1782: /**
1783: * Tests getElementById() of child element after appendChild(), removeChild(), then appendChild()
1784: * of the parent element.
1785: *
1786: * @throws Exception if the test fails
1787: */
1788: public void testGetElementById_AfterAppendRemoveAppendChild()
1789: throws Exception {
1790: final String content = "<html><head><title>foo</title><script>\n"
1791: + " function test() {\n"
1792: + " var table = document.createElement('table');\n"
1793: + " var tr = document.createElement('tr');\n"
1794: + " tr.id='myTR';\n"
1795: + " table.appendChild(tr);\n"
1796: + " document.body.appendChild(table);\n"
1797: + " document.body.removeChild(table);\n"
1798: + " document.body.appendChild(table);\n"
1799: + " alert(document.getElementById('myTR'));\n"
1800: + " }\n"
1801: + "</script></head><body onload='test()'>\n"
1802: + "</body></html>";
1803: final List collectedAlerts = new ArrayList();
1804: loadPage(content, collectedAlerts);
1805: assertFalse(collectedAlerts.get(0).equals("null"));
1806: }
1807:
1808: /**
1809: * @throws Exception if the test fails
1810: */
1811: public void testGetElementById_AfterAppendingToNewlyCreatedElement()
1812: throws Exception {
1813: final String content = "<html><head><title>foo</title><script>\n"
1814: + " function test() {\n"
1815: + " var table = document.createElement('table');\n"
1816: + " var tr = document.createElement('tr');\n"
1817: + " tr.id='myTR';\n"
1818: + " table.appendChild(tr);\n"
1819: + " alert(document.getElementById('myTR'));\n"
1820: + " }\n"
1821: + "</script></head><body onload='test()'>\n"
1822: + "</body></html>";
1823: final List collectedAlerts = new ArrayList();
1824: loadPage(content, collectedAlerts);
1825: assertTrue(collectedAlerts.get(0).equals("null"));
1826: }
1827:
1828: /**
1829: * @throws Exception if the test fails
1830: */
1831: public void testOnunLoadHandler() throws Exception {
1832: final String htmlContent = "<html><head><title>foo</title>\n"
1833: + "</head><body onunload='alert(\"foo\");alert(\"bar\")'>\n"
1834: + "</body></html>";
1835:
1836: final String[] expectedAlerts = { "foo", "bar" };
1837: final List collectedAlerts = new ArrayList();
1838: final WebClient client = new WebClient();
1839: client.setAlertHandler(new CollectingAlertHandler(
1840: collectedAlerts));
1841: final MockWebConnection conn = new MockWebConnection(client);
1842: conn.setResponse(URL_FIRST, htmlContent);
1843: client.setWebConnection(conn);
1844:
1845: client.getPage(URL_FIRST);
1846: client.getPage(URL_FIRST);
1847:
1848: assertEquals(expectedAlerts, collectedAlerts);
1849: }
1850:
1851: /**
1852: * @throws Exception If the test fails
1853: */
1854: public void testOnbeforeunloadHandler() throws Exception {
1855: testOnbeforeunloadHandler(BrowserVersion.INTERNET_EXPLORER_7_0,
1856: false, "first");
1857: testOnbeforeunloadHandler(BrowserVersion.INTERNET_EXPLORER_7_0,
1858: true, "second");
1859: testOnbeforeunloadHandler(BrowserVersion.FIREFOX_2, false,
1860: "first");
1861: testOnbeforeunloadHandler(BrowserVersion.FIREFOX_2, true,
1862: "second");
1863: }
1864:
1865: /**
1866: * @param browserVersion BrowserVersion to use
1867: * @param handlerOk whether OnbeforeunloadHandler.handleEvent will return true of false.
1868: * @param expectedPageTitle the expected title of the page after clicking.
1869: */
1870: private void testOnbeforeunloadHandler(
1871: final BrowserVersion browserVersion,
1872: final boolean handlerOk, final String expectedPageTitle)
1873: throws Exception {
1874: final WebClient webClient = new WebClient(browserVersion);
1875: final MockWebConnection webConnection = new MockWebConnection(
1876: webClient);
1877: final List collectedConfirms = new ArrayList();
1878:
1879: webClient.setOnbeforeunloadHandler(new OnbeforeunloadHandler() {
1880: public boolean handleEvent(final Page page,
1881: final String message) {
1882: collectedConfirms.add(message);
1883: return handlerOk;
1884: }
1885: });
1886:
1887: final String expectedMessage = "Any string value here forces a dialog box to appear before closing the window.";
1888: final String firstContent = "<html><head><title>first</title>\n"
1889: + "<script>\n"
1890: + " function closeIt(event) {\n"
1891: + " event.returnValue = '"
1892: + expectedMessage
1893: + "';\n"
1894: + " }\n"
1895: + "</script>\n"
1896: + "</head><body onbeforeunload='closeIt(event)'>\n"
1897: + " <a href='"
1898: + URL_SECOND
1899: + "'>Second page</a>\n"
1900: + "</body></html>";
1901:
1902: final String secondContent = "<html><head><title>second</title>\n"
1903: + "</head><body>\n" + "</body></html>";
1904:
1905: webConnection.setResponse(URL_FIRST, firstContent);
1906: webConnection.setResponse(URL_SECOND, secondContent);
1907: webClient.setWebConnection(webConnection);
1908:
1909: final HtmlPage page = (HtmlPage) webClient.getPage(URL_FIRST);
1910: final HtmlAnchor anchor = (HtmlAnchor) page.getAnchors().get(0);
1911: final HtmlPage secondPage = (HtmlPage) anchor.click();
1912:
1913: assertEquals(new String[] { expectedMessage },
1914: collectedConfirms);
1915: assertEquals(expectedPageTitle, secondPage.getTitleText());
1916: }
1917:
1918: /**
1919: * @throws Exception If an error occurs.
1920: */
1921: public void testSrcJavaScript() throws Exception {
1922: final String htmlContent = "<html><head><title>foo</title></head><body>\n"
1923: + "<script id='ie_ready' src='javascript:void(0)'></script>\n"
1924: + "</body></html>";
1925:
1926: loadPage(htmlContent);
1927: }
1928:
1929: /**
1930: * Regression test for asText() which would blow up.
1931: *
1932: * @exception Exception If the test fails
1933: */
1934: public void testAsText() throws Exception {
1935: final String htmlContent = "<html><head><title>test</title></head>\n"
1936: + "<body><table>\n"
1937: + "<tr><form><td>a</td></form></tr>\n"
1938: + "</table></body></html>";
1939:
1940: final HtmlPage page = loadPage(htmlContent);
1941: page.asText();
1942: }
1943: }
|