001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.html;
039:
040: import java.util.ArrayList;
041: import java.util.List;
042:
043: import com.gargoylesoftware.htmlunit.MockWebConnection;
044: import com.gargoylesoftware.htmlunit.WebTestCase;
045:
046: /**
047: * Tests for elements inside {@link HtmlNoScript}.
048: *
049: * @version $Revision: 2132 $
050: * @author Ahmed Ashour
051: */
052: public class HtmlNoScriptTest extends WebTestCase {
053:
054: /**
055: * Create an instance
056: *
057: * @param name The name of the test
058: */
059: public HtmlNoScriptTest(final String name) {
060: super (name);
061: }
062:
063: /**
064: * @throws Exception If the test fails
065: */
066: public void testGetElementById() throws Exception {
067: final String htmlContent = "<html><head><title>foo</title>\n"
068: + "<script>\n"
069: + " function test() {\n"
070: + " alert(document.getElementById('second'));\n"
071: + " }\n"
072: + "</script>\n"
073: + "</head><body onload='test()'>\n"
074: + " <input type='text' id='first' name='textfield'/>\n"
075: + " <noscript>\n"
076: + " <input type='text' id='second' name='button'/>\n"
077: + " </noscript>\n" + "</body></html>";
078:
079: final String[] expectedAlerts = { "null" };
080: final List collectedAlerts = new ArrayList();
081: loadPage(htmlContent, collectedAlerts);
082: assertEquals(expectedAlerts, collectedAlerts);
083: }
084:
085: /**
086: * @throws Exception If the test fails
087: */
088: public void testChildNodes() throws Exception {
089: final String htmlContent = "<html><head><title>foo</title>\n"
090: + "<script>\n"
091: + " function test() {\n"
092: + " var noscript = document.getElementById('myDiv' ).childNodes.item(0);\n"
093: + " alert(noscript.childNodes.length);\n" + " }\n"
094: + "</script>\n" + "</head><body onload='test()'>\n"
095: + " <div id='myDiv'><noscript>\n"
096: + " <input type='text' name='button'/>\n"
097: + " </noscript></div>\n" + "</body></html>";
098:
099: final String[] expectedAlerts = { "0" };
100: final List collectedAlerts = new ArrayList();
101: loadPage(htmlContent, collectedAlerts);
102: assertEquals(expectedAlerts, collectedAlerts);
103: }
104:
105: /**
106: * @throws Exception If the test fails
107: */
108: public void testJavaScript() throws Exception {
109: final String htmlContent = "<html><head><title>foo</title>\n"
110: + "<script>\n" + " alert(1);\n" + "</script>\n"
111: + "<noscript>\n" + " <script>\n" + " alert(2);\n"
112: + " </script>\n" + "</noscript>\n" + "</head><body>\n"
113: + "</body></html>";
114:
115: final String[] expectedAlerts = { "1" };
116: final List collectedAlerts = new ArrayList();
117: loadPage(htmlContent, collectedAlerts);
118: assertEquals(expectedAlerts, collectedAlerts);
119: }
120:
121: /**
122: * @throws Exception If the test fails
123: */
124: public void testFormValues() throws Exception {
125: final String htmlContent = "<html><body>\n"
126: + "<form name='item' method='post'>\n"
127: + " <noscript>\n"
128: + " <input type=hidden name='__webpage_no_js__' value='1'>\n"
129: + " </noscript>\n"
130: + " <input type=hidden name='myParam' value='myValue'>\n"
131: + " <input type='submit'>\n" + "</form>\n"
132: + "</body></html>";
133:
134: final HtmlPage firstPage = loadPage(htmlContent);
135: final HtmlForm form = (HtmlForm) firstPage.getForms().get(0);
136: final HtmlPage secondPage = (HtmlPage) form
137: .submit((SubmittableElement) null);
138:
139: final MockWebConnection mockWebConnection = getMockConnection(secondPage);
140: assertEquals(1, mockWebConnection.getLastParameters().size());
141: assertTrue(secondPage.asXml().indexOf("__webpage_no_js__") > -1);
142: }
143: }
|