View Javadoc

1   /**
2    * Copyright (c) 2002-2015, JWebUnit team.
3    *
4    * This file is part of JWebUnit.
5    *
6    * JWebUnit is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * JWebUnit is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with JWebUnit.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package net.sourceforge.jwebunit.tests;
20  
21  import static net.sourceforge.jwebunit.junit.JWebUnit.*;
22  import static org.junit.Assert.*;
23  
24  import java.util.Date;
25  import java.util.List;
26  
27  import net.sourceforge.jwebunit.api.IElement;
28  
29  import org.junit.Test;
30  
31  /**
32   * Test the IElement interface
33   *
34   * @author jmwright
35   */
36  public class IElementTest extends JWebUnitAPITestCase {
37  
38      public void setUp() throws Exception {
39          super.setUp();
40          setBaseUrl(HOST_PATH + "/IElementTest");
41          beginAt("/template.html");
42      }
43      
44      @Test
45      public void testSimple() {
46      	// test an element that exists
47      	IElement element = getElementByXPath("//input[@id='test']");
48      	assertNotNull(element);
49      	assertEquals(element.getName(), "input");
50      	assertEquals(element.getAttribute("name"), "element_name");
51      	assertEquals(element.getAttribute("id"), "test");
52      	assertEquals(element.getAttribute("value"), "test3");
53      }
54      
55      @Test
56      public void testSimpleHasID() {
57      	assertTrue(hasElementById("test"));
58      }
59  
60      @Test
61      public void testSimpleHasXPath() {
62      	assertTrue(hasElementByXPath("//input[@id='test']"));
63      }
64  
65      @Test
66      public void testSimpleHasXPaths() {
67      	assertTrue(hasElementsByXPath("//input[@id='test']"));
68      }
69  
70      @Test
71      public void testMissing() {
72      	// a missing element should throw an exception
73      	try {
74      		getElementByXPath("//input[@id='test2']");
75      		fail("getElementByXPath() should have thrown an assertion exception.");
76      	} catch (AssertionError e) {
77      		// nothing
78      	}
79      }
80      
81      @Test
82      public void testMissingHasID() {
83      	assertFalse(hasElementById("test2"));
84      }    
85      
86      @Test
87      public void testMissingHasXPath() {
88      	assertFalse(hasElementByXPath("//input[@id='test2']"));
89      }
90      
91      @Test
92      public void testMissingHasXPaths() {
93      	assertFalse(hasElementsByXPath("//input[@id='test2']"));
94      }
95  
96      /**
97       * Test parent, child methods
98       */
99      @Test
100     public void testChildrenAndParent() {
101     	assertElementPresent("first");
102     	IElement element = getElementById("first");
103     	assertEquals(element.getName(), "li");
104     	assertEquals(element.getTextContent(), "one");
105     	assertEquals(element.getAttribute("id"), "first");
106     	
107     	// parent should be an <ol>
108     	IElement parent = element.getParent();
109     	assertEquals(parent.getName(), "ol");
110     	
111     	// it should have four children
112     	List<IElement> children = parent.getChildren();
113     	assertEquals(children.size(), 4);
114     	assertEquals(children.get(0).getTextContent(), "one");
115     	assertEquals(children.get(1).getTextContent(), "two");
116     	assertEquals(children.get(2).getTextContent(), "three");
117     	assertEquals(children.get(3).getTextContent(), "four");
118     	
119     	// they are all <li>'s
120     	for (IElement e : children)
121     		assertEquals(e.getName(), "li");
122     }
123     
124     /**
125      * Test getting the XPath for multiple possible results
126      */
127     @Test
128     public void testMultiple() {
129     	List<IElement> children = getElementsByXPath("//li");
130     	assertEquals(children.size(), 4);
131     	assertEquals(children.get(0).getTextContent(), "one");
132     	assertEquals(children.get(1).getTextContent(), "two");
133     	assertEquals(children.get(2).getTextContent(), "three");
134     	assertEquals(children.get(3).getTextContent(), "four");
135     	
136     }
137 
138     /**
139      * change the element and make sure XPath has changed
140      */
141     @Test
142     public void testChanging() {
143     	{
144 	    	IElement element = getElementByXPath("//input[@id='test']");
145 	    	assertNotNull(element);
146 	    	assertEquals(element.getName(), "input");
147 	    	assertEquals(element.getAttribute("name"), "element_name");
148 	    	assertEquals(element.getAttribute("id"), "test");
149 	    	assertEquals(element.getAttribute("value"), "test3");
150 	    	// the element should also be available through the normal method
151 	    	assertFormElementPresent("element_name");
152     	}
153 
154     	String testingText = new Date().toString();
155     	setTextField("element_name", testingText);
156     	assertTextFieldEquals("element_name", testingText);		// should still work
157     	
158     	{
159 	    	IElement element = getElementByXPath("//input[@id='test']");
160 	    	assertNotNull(element);
161 	    	assertEquals(element.getName(), "input");
162 	    	assertEquals(element.getAttribute("name"), "element_name");
163 	    	assertEquals(element.getAttribute("id"), "test");
164 	    	assertEquals(element.getAttribute("value"), testingText);		// should have changed
165 	    	// the element should also be available through the normal method
166 	    	assertFormElementPresent("element_name");
167     	}
168     	
169     }
170     
171     @Test
172     public void testWithXpath() {
173     	IElement element = getElementByXPath("//body");
174     	assertNotNull(element);
175     	assertEquals("body", element.getName());
176     	
177     	// get first input children
178     	IElement input = element.getElement("input");
179     	assertNotNull(input);
180     	assertEquals("input", input.getName());
181     	assertEquals("element_name", input.getAttribute("name"));
182     	assertEquals("test3", input.getAttribute("value"));
183     	
184     	// get all input children
185     	List<IElement> inputs = element.getElements("input");
186     	assertEquals(4, inputs.size());	// there should be two
187     	assertEquals("test3", inputs.get(0).getAttribute("value"));
188     	assertEquals("Do nothing", inputs.get(1).getAttribute("value"));
189     	assertEquals("initial", inputs.get(2).getAttribute("value"));
190     	assertEquals("unchanged", inputs.get(3).getAttribute("value"));
191     	
192     	// test regexps
193     	assertMatch("init.+", inputs.get(2).getAttribute("value"));
194     	assertNotMatch("^xinitial", inputs.get(2).getAttribute("value"));
195     	assertMatch("test regexp with message", "init.+", inputs.get(2).getAttribute("value"));
196     	assertNotMatch("test regexp with message", "$xinitial", inputs.get(2).getAttribute("value"));
197     	
198     	// get parent through xpath
199     	IElement parent = element.getElement("..");
200     	assertNotNull(parent);
201     	assertEquals("html", parent.getName());
202     	
203     }
204     
205     /**
206      * Test that setting attributes manually (e.g setAttribute("value") 
207      * properly calls any attached Javascript.
208      */
209     @Test
210     public void testAttributeJavascript() {
211     	String testingText = new Date().toString();
212     	
213     	{
214 	    	IElement js1 = getElementById("js1");
215 	    	IElement js2 = getElementById("js2");
216 	    	
217 	    	assertEquals("initial", js1.getAttribute("value"));
218 	    	assertEquals("unchanged", js2.getAttribute("value"));
219 	    	
220 	    	// change js1's value
221 	    	js1.setAttribute("value", testingText);
222     	}
223     	
224     	// refresh the elements and check they have changed
225     	{
226 	    	IElement js1 = getElementById("js1");
227 	    	IElement js2 = getElementById("js2");
228 	    	
229 	    	assertEquals(testingText, js1.getAttribute("value"));
230 	    	assertEquals(testingText, js2.getAttribute("value"));
231     	}
232 
233     }
234     
235     /**
236      * Tests searching for comments.
237      */
238     @Test public void testComments() {
239     	// whitespace is ignored
240     	assertCommentPresent("a comment");
241     	assertCommentPresent("another comment");
242     	assertCommentPresent("  a comment");
243     	assertCommentPresent("   another comment  ");
244     	
245     	// but case is not
246     	assertCommentNotPresent("A Comment");
247     	assertCommentNotPresent("definitely not here");
248     }
249     
250     /**
251      * Test preceding element XPath.
252      * preceding: "Selects everything in the document that is before the start tag of the current node"
253      */
254     @Test
255     public void testPreceding() {
256     	IElement element = getElementById("first"); // li
257     	// should get the first <input>, which is
258     	// <input id="test" name="element_name" value="test3">
259     	IElement preceding = element.getElement("preceding::input"); 
260     	
261     	assertEquals(preceding.getName(), "input");
262     	assertEquals(preceding.getAttribute("name"), "element_name");
263     }
264     
265     /**
266      * Test that {@link IElement#equals(Object)} is implemented
267      * correctly.
268      * 
269      */
270     @Test public void testIElementEquals() {
271     	
272     	// through getElementById
273     	IElement container1 = getElementById("container");
274     	
275     	// through IElement.getElement
276     	IElement span = getElementByXPath("//span[@class='inline']");
277     	IElement container2 = span.getElement("..");
278     	
279     	// through getByXPath
280     	IElement container3 = null;
281     	for (IElement e : getElementsByXPath("//div")) {
282     		if ("container".equals(e.getAttribute("id"))) {
283     			container3 = e;
284     		}
285     	}
286     	
287     	// should have found all of these
288     	assertNotNull(container1);
289     	assertNotNull(container2);
290     	assertNotNull(container3);
291     	
292     	// check equality
293     	assertEquals(container1, container2);
294     	assertEquals(container2, container3);
295     	assertEquals(container1, container3);
296     	
297     }
298     
299 }