|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| package org.objectledge.web.test; |
|
29 |
| |
|
30 |
| import junit.framework.Assert; |
|
31 |
| |
|
32 |
| import org.w3c.dom.Document; |
|
33 |
| import org.w3c.dom.Element; |
|
34 |
| |
|
35 |
| import com.meterware.httpunit.HTMLElementPredicate; |
|
36 |
| import com.meterware.httpunit.WebImage; |
|
37 |
| import com.meterware.httpunit.WebLink; |
|
38 |
| import com.meterware.httpunit.WebTable; |
|
39 |
| |
|
40 |
| import net.sourceforge.jwebunit.WebTestCase; |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| public class LedgeWebTestCase |
|
49 |
| extends WebTestCase |
|
50 |
| { |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
0
| public void setUp()
|
|
55 |
| { |
|
56 |
0
| String url = System.getProperty("base.url", "http://localhost:8080");
|
|
57 |
0
| String baseUrl = url.endsWith("/") ? url.substring(0, url.length()-1) : url;
|
|
58 |
0
| getTestContext().setBaseUrl(baseUrl);
|
|
59 |
0
| beginAt("/action/i18n.SetLocale/locale/pl_PL");
|
|
60 |
0
| beginAt("/action/webcore,SetLocale/locale/pl_PL");
|
|
61 |
| } |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
0
| public void assertActualView(String expectedView)
|
|
71 |
| { |
|
72 |
0
| String actualView = Utils.getActualView(getTester().getDialog().getResponseText());
|
|
73 |
0
| if(actualView == null)
|
|
74 |
| { |
|
75 |
0
| Assert.fail("unable to determine current view, expected: "+expectedView);
|
|
76 |
| } |
|
77 |
0
| if(!actualView.equals(expectedView))
|
|
78 |
| { |
|
79 |
0
| Assert.fail("expected view " + expectedView+" but current view is " + actualView);
|
|
80 |
| } |
|
81 |
| } |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
0
| public void assertActionResult(String expectedResult)
|
|
89 |
| { |
|
90 |
0
| String actionResult = Utils.getActionResult(getTester().getDialog().getResponseText());
|
|
91 |
0
| if(actionResult == null)
|
|
92 |
| { |
|
93 |
0
| Assert.fail("unable to determine action result");
|
|
94 |
| } |
|
95 |
0
| if(!actionResult.equals(expectedResult))
|
|
96 |
| { |
|
97 |
0
| Assert.fail("expected result " + expectedResult + " but action reported " +
|
|
98 |
| actionResult); |
|
99 |
| } |
|
100 |
| } |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
0
| public void assertNoActionResult()
|
|
106 |
| { |
|
107 |
0
| String actionResult = Utils.getActionResult(getTester().getDialog().getResponseText());
|
|
108 |
0
| if(actionResult != null && actionResult.length() > 0)
|
|
109 |
| { |
|
110 |
0
| Assert.fail("exected no result but action reported "+actionResult);
|
|
111 |
| } |
|
112 |
| } |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
0
| protected WebLink getLinkWithURL(String text)
|
|
125 |
| throws Exception |
|
126 |
| { |
|
127 |
0
| WebLink[] links = getTester().getDialog().getResponse().getLinks();
|
|
128 |
0
| WebLink link = null;
|
|
129 |
0
| for(WebLink l: links)
|
|
130 |
| { |
|
131 |
0
| if(l.getURLString().contains(text))
|
|
132 |
| { |
|
133 |
0
| return l;
|
|
134 |
| } |
|
135 |
| } |
|
136 |
0
| return null;
|
|
137 |
| } |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
0
| protected int countLinksWithString(String text)
|
|
147 |
| throws Exception |
|
148 |
| { |
|
149 |
0
| int i = 0;
|
|
150 |
0
| WebLink[] links = getTester().getDialog().getResponse().getLinks();
|
|
151 |
0
| for(WebLink l: links)
|
|
152 |
| { |
|
153 |
0
| if(l.getText().contains(text))
|
|
154 |
| { |
|
155 |
0
| i++;
|
|
156 |
| } |
|
157 |
| } |
|
158 |
0
| return i;
|
|
159 |
| } |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
0
| protected int countLinksWithExactString(String text)
|
|
169 |
| throws Exception |
|
170 |
| { |
|
171 |
0
| int i = 0;
|
|
172 |
0
| WebLink[] links = getTester().getDialog().getResponse().getLinks();
|
|
173 |
0
| for(WebLink l: links)
|
|
174 |
| { |
|
175 |
0
| if(l.getText().equals(text))
|
|
176 |
| { |
|
177 |
0
| i++;
|
|
178 |
| } |
|
179 |
| } |
|
180 |
0
| return i;
|
|
181 |
| } |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
0
| protected int countImagesWithSource(String text)
|
|
191 |
| throws Exception |
|
192 |
| { |
|
193 |
0
| int i = 0;
|
|
194 |
0
| WebImage[] images = getTester().getDialog().getResponse().getImages();
|
|
195 |
0
| for(WebImage l: images)
|
|
196 |
| { |
|
197 |
0
| if(l.getSource().contains(text))
|
|
198 |
| { |
|
199 |
0
| i++;
|
|
200 |
| } |
|
201 |
| } |
|
202 |
0
| return i;
|
|
203 |
| } |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
0
| protected int countString(String text)
|
|
212 |
| { |
|
213 |
0
| String response = getTester().getDialog().getResponseText();
|
|
214 |
0
| int counter = 0;
|
|
215 |
0
| int index = response.indexOf(text);
|
|
216 |
0
| while(index >= 0)
|
|
217 |
| { |
|
218 |
0
| counter++;
|
|
219 |
0
| index = response.indexOf(text, index + text.length());
|
|
220 |
| } |
|
221 |
0
| return counter;
|
|
222 |
| } |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
0
| protected int countElements(String tagName)
|
|
232 |
| throws Exception |
|
233 |
| { |
|
234 |
0
| Document doc = getTester().getDialog().getResponse().getDOM();
|
|
235 |
0
| DOMTreeWalker walker = new DOMTreeWalker(doc.getDocumentElement());
|
|
236 |
0
| return walker.countTags(tagName);
|
|
237 |
| } |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
0
| public Element getTableWithText(String text)
|
|
247 |
| throws Exception |
|
248 |
| { |
|
249 |
0
| Document doc = getTester().getDialog().getResponse().getDOM();
|
|
250 |
0
| DOMTreeWalker walker = new DOMTreeWalker(doc.getDocumentElement());
|
|
251 |
0
| return getTableWithText(walker, text);
|
|
252 |
| } |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
0
| public Element getTableWithText(DOMTreeWalker walker, String text)
|
|
262 |
| { |
|
263 |
0
| return walker.findElementWithText(text,"table");
|
|
264 |
| } |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
0
| public String getTableCellText(Element table, int row, int cell)
|
|
276 |
| throws Exception |
|
277 |
| { |
|
278 |
0
| Document doc = getTester().getDialog().getResponse().getDOM();
|
|
279 |
0
| DOMTreeWalker walker = new DOMTreeWalker(doc.getDocumentElement());
|
|
280 |
0
| return getTableRowText(walker, table, row, cell);
|
|
281 |
| } |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
0
| public String getTableRowText(DOMTreeWalker walker, Element table, int row, int cell)
|
|
293 |
| { |
|
294 |
0
| walker.gotoElement(table);
|
|
295 |
0
| for(int i = 0; i < row+1; i++)
|
|
296 |
| { |
|
297 |
0
| walker.getNextElement(0,"tr");
|
|
298 |
| } |
|
299 |
0
| for(int i = 0; i < cell+1; i++)
|
|
300 |
| { |
|
301 |
0
| walker.getNextElement(0,"td");
|
|
302 |
| } |
|
303 |
0
| return walker.getNextText();
|
|
304 |
| } |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
| public class TableContentPredicate implements HTMLElementPredicate |
|
310 |
| { |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
0
| public boolean matchesCriteria(Object htmlElement, Object criteria)
|
|
315 |
| { |
|
316 |
0
| if(!(htmlElement instanceof WebTable))
|
|
317 |
| { |
|
318 |
0
| return false;
|
|
319 |
| } |
|
320 |
0
| WebTable table = (WebTable)htmlElement;
|
|
321 |
0
| String value = table.getText();
|
|
322 |
0
| if(value != null && value.contains((String)criteria))
|
|
323 |
| { |
|
324 |
0
| return true;
|
|
325 |
| } |
|
326 |
0
| return false;
|
|
327 |
| } |
|
328 |
| } |
|
329 |
| } |