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.assertLinkPresentWithExactText;
22  import static net.sourceforge.jwebunit.junit.JWebUnit.assertLinkPresentWithText;
23  import static net.sourceforge.jwebunit.junit.JWebUnit.assertTitleEquals;
24  import static net.sourceforge.jwebunit.junit.JWebUnit.beginAt;
25  import static net.sourceforge.jwebunit.junit.JWebUnit.clickLink;
26  import static net.sourceforge.jwebunit.junit.JWebUnit.clickLinkWithExactText;
27  import static net.sourceforge.jwebunit.junit.JWebUnit.clickLinkWithImage;
28  import static net.sourceforge.jwebunit.junit.JWebUnit.clickLinkWithText;
29  import static net.sourceforge.jwebunit.junit.JWebUnit.getTestContext;
30  import static net.sourceforge.jwebunit.junit.JWebUnit.gotoPage;
31  import static net.sourceforge.jwebunit.junit.JWebUnit.setBaseUrl;
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertTrue;
34  import static org.junit.Assert.fail;
35  
36  import org.junit.Before;
37  
38  import net.sourceforge.jwebunit.exception.TestingEngineResponseException;
39  
40  import org.junit.Test;
41  
42  /**
43   * Test url navigation methods on WebTestCase (starting at a url, navigating
44   * links).
45   * 
46   * @author Jim Weaver
47   * @author Wilkes Joiner
48   */
49  public class NavigationTest extends JWebUnitAPITestCase {
50  
51      @Before
52  	public void setUp() throws Exception {
53  		super.setUp();
54  		setBaseUrl(HOST_PATH + "/NavigationTest");
55  	}
56  
57  	@Test
58  	public void testBeginAtRelative() {
59  	    beginAt("/blah.html");
60  	}
61  
62  	@Test
63  	public void testBeginAtAbsolute() {
64  	    beginAt(HOST_PATH + "/NavigationTest/blah.html");
65  	}
66  
67  	@Test
68  	public void testForwardSlashConfusion() throws Exception {
69  	    beginAt("/blah.html");
70  	    beginAt("blah.html");
71  		getTestContext().setBaseUrl(HOST_PATH + "/NavigationTest/");
72  		beginAt("/blah.html");
73  		beginAt("blah.html");
74  	}
75  
76  	@Test
77  	public void testInvalidBeginAt() {
78  		//the testing engines should throw an exception if a 404 Error is found.
79  		TestingEngineResponseException e = assertException(TestingEngineResponseException.class, "beginAt", new Object[] {"/nosuchresource.html"});
80  		assertEquals(404, e.getHttpStatusCode());
81  	}
82  	
83  	@Test
84  	public void testClickLinkWithText() {
85  		beginAt("/pageWithLink.html");
86  		assertTitleEquals("pageWithLink");
87  
88  		clickLinkWithText("an active link");
89  		assertTitleEquals("targetPage");
90  	}
91  
92  	@Test 
93  	public void testClickLinkWithTextN() {
94  		beginAt("/pageWithLink.html");
95  		assertTitleEquals("pageWithLink");
96  
97  		clickLinkWithText("an active link", 0);
98  		assertTitleEquals("targetPage");
99  
100 		beginAt("/pageWithLink.html");
101 		clickLinkWithText("an active link", 1);
102 
103 		assertTitleEquals("targetPage2");
104 		beginAt("/pageWithLink.html");
105 		try {
106 			clickLinkWithText("an active link", 2);
107 			fail();
108 		} catch (AssertionError expected) {
109 			assertEquals("Link with text [an active link] and index [2] "
110 					+ "not found in response.", expected.getMessage());
111 		}
112 		assertTitleEquals("pageWithLink");
113 	}
114 
115 	@Test
116 	public void testClickLinkWithImage() {
117 		beginAt("/pageWithLink.html");
118 		assertTitleEquals("pageWithLink");
119 
120 		clickLinkWithImage("graphic.jpg");
121 		assertTitleEquals("targetPage2");
122 	}
123 
124     @Test
125     public void testClickLinkWithImageAnd0Index() {
126         beginAt("/pageWithLink.html");
127         assertTitleEquals("pageWithLink");
128 
129         clickLinkWithImage("graphic.jpg", 0);
130         assertTitleEquals("targetPage2");
131     }
132 
133     @Test
134     public void testClickLinkWithImageAnd1Index() {
135         beginAt("/pageWithLink.html");
136         assertTitleEquals("pageWithLink");
137 
138         clickLinkWithImage("graphic.jpg", 1);
139         assertTitleEquals("targetPage");
140     }
141 
142     @Test
143     public void testClickLinkByID() {
144 		beginAt("/pageWithLink.html");
145 		assertTitleEquals("pageWithLink");
146 
147 		clickLink("activeID");
148 		assertTitleEquals("targetPage");
149 	}
150 
151 	@Test
152 	public void testInvalidClickLink() {
153 		beginAt("/pageWithLink.html");
154 		assertTitleEquals("pageWithLink");
155 
156 		try {
157 			clickLinkWithText("no such link");
158 		} catch (Throwable t) {
159 			return;
160 		}
161 		fail("Expected exception");
162 	}
163 
164 	@Test
165 	public void testGotoPageRelative() {
166 		beginAt("/targetPage.html");
167 		assertTitleEquals("targetPage");
168 		gotoPage("/targetPage2.html");
169 		assertTitleEquals("targetPage2");
170 	}
171 	
172 	@Test
173 	public void testGotoPageAbsolute() {
174 		beginAt("/targetPage.html");
175 		assertTitleEquals("targetPage");
176                 gotoPage(HOST_PATH + "/NavigationTest/targetPage2.html");
177 		assertTitleEquals("targetPage2");
178 	}
179 
180 	@Test
181 	public void testInvalidGotoPage() {
182 		beginAt("/targetPage.html");
183 		//the testing engines should throw an exception if a 404 Error is found.
184 		TestingEngineResponseException e = assertException(TestingEngineResponseException.class, "gotoPage", new Object[] {"/nosuchresource.html"});
185 		assertEquals(404, e.getHttpStatusCode());
186 	}
187 
188 	//For bug 726143
189 	@Test
190 	public void testLinkWithEscapedText() {
191 		beginAt("/pageWithAmpersandInLink.html");
192 		assertLinkPresentWithText("Map & Directions");
193 		clickLinkWithText("Map & Directions");
194 		assertTitleEquals("targetPage");
195 	}
196 	
197 	/**
198 	 * Testing for issue 996031
199 	 */
200 	@Test
201 	public void testLinkExactText() {
202 		beginAt("/test1.html");
203 		assertTitleEquals("test1");
204 		assertLinkPresentWithExactText("one");
205 		assertLinkPresentWithExactText("tone");
206 		clickLinkWithExactText("one");
207 		assertTitleEquals("test2");
208 		
209 		// the following should fail
210 		boolean passed = false;
211 		try {
212 			clickLinkWithExactText("doesn't exist");
213 		} catch (AssertionError e) {
214 			// expected
215 			passed = true;
216 		}
217 		assertTrue("non-existant link should throw an error", passed);
218 	}
219 	
220 }