KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > html > HtmlOptionTest


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.html;
39
40 import com.gargoylesoftware.htmlunit.MockWebConnection;
41 import com.gargoylesoftware.htmlunit.WebClient;
42 import com.gargoylesoftware.htmlunit.WebTestCase;
43
44 /**
45  * Tests for HtmlOption
46  *
47  * @version $Revision: 1.12 $
48  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
49  */

50 public class HtmlOptionTest extends WebTestCase {
51     /**
52      * Create an instance
53      * @param name The name of the test
54      */

55     public HtmlOptionTest( final String name ) {
56         super(name);
57     }
58
59
60     /**
61      * @throws Exception if the test fails
62      */

63     public void testSelect() throws Exception {
64
65         final String htmlContent
66             = "<html><head><title>foo</title></head><body>"
67             + "<form id='form1'><select name='select1' id='select1'>"
68             + "<option value='option1' id='option1'>Option1</option>"
69             + "<option value='option2' id='option2' selected='selected'>Option2</option>"
70             + "<option value='option3' id='option3'>Option3</option>"
71             + "</select>"
72             + "<input type='submit' name='button' value='foo'/>"
73             + "</form></body></html>";
74         final HtmlPage page = loadPage(htmlContent);
75
76         final HtmlOption option1 = ( HtmlOption )page.getHtmlElementById( "option1" );
77         final HtmlOption option2 = ( HtmlOption )page.getHtmlElementById( "option2" );
78         final HtmlOption option3 = ( HtmlOption )page.getHtmlElementById( "option3" );
79
80         assertFalse( option1.isSelected() );
81         assertTrue ( option2.isSelected() );
82         assertFalse( option3.isSelected() );
83
84         option3.setSelected(true);
85
86         assertFalse( option1.isSelected() );
87         assertFalse( option2.isSelected() );
88         assertTrue ( option3.isSelected() );
89
90         option3.setSelected(false);
91
92         assertFalse( option1.isSelected() );
93         assertFalse( option2.isSelected() );
94         assertFalse( option3.isSelected() );
95     }
96     
97     /**
98      * @throws Exception if the test fails
99      */

100     public void testGetValue() throws Exception {
101
102         final String htmlContent
103             = "<html><head><title>foo</title></head><body>"
104             + "<form id='form1'><select name='select1' id='select1'>"
105             + "<option value='option1' id='option1'>Option1</option>"
106             + "<option id='option2' selected>Number Two</option>"
107             + "</select>"
108             + "<input type='submit' name='button' value='foo'/>"
109             + "</form></body></html>";
110         final WebClient client = new WebClient();
111
112         final MockWebConnection webConnection = new MockWebConnection( client );
113         webConnection.setDefaultResponse( htmlContent );
114         client.setWebConnection( webConnection );
115
116         final HtmlPage page = ( HtmlPage )client.getPage(URL_FIRST);
117
118         final HtmlOption option1 = ( HtmlOption )page.getHtmlElementById( "option1" );
119         final HtmlOption option2 = ( HtmlOption )page.getHtmlElementById( "option2" );
120         
121         assertEquals("option1", option1.getValue());
122         assertEquals("Number Two", option2.getValue());
123
124     }
125     /**
126      * @throws Exception if the test fails
127      */

128     public void testGetValue_ContentsIsValue() throws Exception {
129
130         final String htmlContent
131             = "<html><head><title>foo</title></head><body>"
132             + "<form id='form1'>"
133             + "<select name='select1' id='select1'>"
134             + " <option id='option1'>Option1</option>"
135             + " <option id='option2' selected>Number Two</option>"
136             + "</select>"
137             + "<input type='submit' name='button' value='foo'/>"
138             + "</form></body></html>";
139         final WebClient client = new WebClient();
140
141         final MockWebConnection webConnection = new MockWebConnection( client );
142         webConnection.setDefaultResponse( htmlContent );
143         client.setWebConnection( webConnection );
144
145         final HtmlPage page = ( HtmlPage )client.getPage(URL_FIRST);
146
147         final HtmlOption option1 = ( HtmlOption )page.getHtmlElementById( "option1" );
148         final HtmlOption option2 = ( HtmlOption )page.getHtmlElementById( "option2" );
149         
150         assertEquals("Option1", option1.getValue());
151         assertEquals("Number Two", option2.getValue());
152
153     }
154 }
155
Popular Tags