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;
039:
040: import com.gargoylesoftware.htmlunit.html.HtmlPage;
041:
042: /**
043: * Tests for {@link WebAssert}.
044: *
045: * @version $Revision: 2132 $
046: * @author Daniel Gredler
047: */
048: public class WebAssertTest extends WebTestCase {
049:
050: /**
051: * Creates a new instance.
052: *
053: * @param name the test name
054: */
055: public WebAssertTest(final String name) {
056: super (name);
057: }
058:
059: /**
060: * @throws Exception if an error occurs
061: */
062: public void testAssertTitleEquals() throws Exception {
063:
064: final String html = "<html><head><title>foo</title></head><body>bar</body></html>";
065: final HtmlPage page = loadPage(html);
066:
067: WebAssert.assertTitleEquals(page, "foo");
068:
069: boolean caught = false;
070: try {
071: WebAssert.assertTitleEquals(page, "bar");
072: } catch (final AssertionError e) {
073: caught = true;
074: }
075: assertTrue(caught);
076: }
077:
078: /**
079: * @throws Exception if an error occurs
080: */
081: public void testAssertTitleContains() throws Exception {
082:
083: final String html = "<html><head><title>foo</title></head><body>bar</body></html>";
084: final HtmlPage page = loadPage(html);
085:
086: WebAssert.assertTitleContains(page, "o");
087:
088: boolean caught = false;
089: try {
090: WebAssert.assertTitleContains(page, "a");
091: } catch (final AssertionError e) {
092: caught = true;
093: }
094: assertTrue(caught);
095: }
096:
097: /**
098: * @throws Exception if an error occurs
099: */
100: public void testAssertTitleMatches() throws Exception {
101:
102: final String html = "<html><head><title>foo</title></head><body>bar</body></html>";
103: final HtmlPage page = loadPage(html);
104:
105: WebAssert.assertTitleMatches(page, "f..");
106:
107: boolean caught = false;
108: try {
109: WebAssert.assertTitleMatches(page, "b..");
110: } catch (final AssertionError e) {
111: caught = true;
112: }
113: assertTrue(caught);
114: }
115:
116: /**
117: * @throws Exception if an error occurs
118: */
119: public void testAssertElementPresent() throws Exception {
120:
121: final String html = "<html><body><div id='a'>bar</div></body></html>";
122: final HtmlPage page = loadPage(html);
123:
124: WebAssert.assertElementPresent(page, "a");
125:
126: boolean caught = false;
127: try {
128: WebAssert.assertElementPresent(page, "b");
129: } catch (final AssertionError e) {
130: caught = true;
131: }
132: assertTrue(caught);
133: }
134:
135: /**
136: * @throws Exception if an error occurs
137: */
138: public void testAssertElementPresentByXPath() throws Exception {
139:
140: final String html = "<html><body><div id='a'>bar</div></body></html>";
141: final HtmlPage page = loadPage(html);
142:
143: WebAssert.assertElementPresentByXPath(page, "html/body/div");
144:
145: boolean caught = false;
146: try {
147: WebAssert.assertElementPresentByXPath(page, "ul");
148: } catch (final AssertionError e) {
149: caught = true;
150: }
151: assertTrue(caught);
152: }
153:
154: /**
155: * @throws Exception if an error occurs
156: */
157: public void testAssertElementNotPresent() throws Exception {
158:
159: final String html = "<html><body><div id='a'>bar</div></body></html>";
160: final HtmlPage page = loadPage(html);
161:
162: WebAssert.assertElementNotPresent(page, "b");
163:
164: boolean caught = false;
165: try {
166: WebAssert.assertElementNotPresent(page, "a");
167: } catch (final AssertionError e) {
168: caught = true;
169: }
170: assertTrue(caught);
171: }
172:
173: /**
174: * @throws Exception if an error occurs
175: */
176: public void testAssertElementNotPresentByXPath() throws Exception {
177:
178: final String html = "<html><body><div id='a'>bar</div></body></html>";
179: final HtmlPage page = loadPage(html);
180:
181: WebAssert.assertElementNotPresentByXPath(page, "ul");
182:
183: boolean caught = false;
184: try {
185: WebAssert.assertElementNotPresentByXPath(page,
186: "html/body/div");
187: } catch (final AssertionError e) {
188: caught = true;
189: }
190: assertTrue(caught);
191: }
192:
193: /**
194: * @throws Exception if an error occurs
195: */
196: public void testAssertTextPresent() throws Exception {
197:
198: final String html = "<html><body><div id='a'>bar</div></body></html>";
199: final HtmlPage page = loadPage(html);
200:
201: WebAssert.assertTextPresent(page, "bar");
202:
203: boolean caught = false;
204: try {
205: WebAssert.assertTextPresent(page, "baz");
206: } catch (final AssertionError e) {
207: caught = true;
208: }
209: assertTrue(caught);
210: }
211:
212: /**
213: * @throws Exception if an error occurs
214: */
215: public void testAssertTextPresentInElement() throws Exception {
216:
217: final String html = "<html><body><div id='a'>bar</div></body></html>";
218: final HtmlPage page = loadPage(html);
219:
220: WebAssert.assertTextPresentInElement(page, "bar", "a");
221:
222: boolean caught = false;
223: try {
224: WebAssert.assertTextPresentInElement(page, "baz", "a");
225: } catch (final AssertionError e) {
226: caught = true;
227: }
228: assertTrue(caught);
229:
230: caught = false;
231: try {
232: WebAssert.assertTextPresentInElement(page, "bar", "b");
233: } catch (final AssertionError e) {
234: caught = true;
235: }
236: assertTrue(caught);
237: }
238:
239: /**
240: * @throws Exception if an error occurs
241: */
242: public void testAssertTextNotPresent() throws Exception {
243:
244: final String html = "<html><body><div id='a'>bar</div></body></html>";
245: final HtmlPage page = loadPage(html);
246:
247: WebAssert.assertTextNotPresent(page, "baz");
248:
249: boolean caught = false;
250: try {
251: WebAssert.assertTextNotPresent(page, "bar");
252: } catch (final AssertionError e) {
253: caught = true;
254: }
255: assertTrue(caught);
256: }
257:
258: /**
259: * @throws Exception if an error occurs
260: */
261: public void testAssertTextNotPresentInElement() throws Exception {
262:
263: final String html = "<html><body><div id='a'>bar</div></body></html>";
264: final HtmlPage page = loadPage(html);
265:
266: WebAssert.assertTextNotPresentInElement(page, "baz", "a");
267:
268: boolean caught = false;
269: try {
270: WebAssert.assertTextNotPresentInElement(page, "bar", "a");
271: } catch (final AssertionError e) {
272: caught = true;
273: }
274: assertTrue(caught);
275:
276: caught = false;
277: try {
278: WebAssert.assertTextNotPresentInElement(page, "bar", "b");
279: } catch (final AssertionError e) {
280: caught = true;
281: }
282: assertTrue(caught);
283: }
284:
285: /**
286: * @throws Exception if an error occurs
287: */
288: public void testAssertLinkPresent() throws Exception {
289:
290: final String html = "<html><body><a href='foo.html' id='x'>bar</a></body></html>";
291: final HtmlPage page = loadPage(html);
292:
293: WebAssert.assertLinkPresent(page, "x");
294:
295: boolean caught = false;
296: try {
297: WebAssert.assertLinkPresent(page, "z");
298: } catch (final AssertionError e) {
299: caught = true;
300: }
301: assertTrue(caught);
302: }
303:
304: /**
305: * @throws Exception if an error occurs
306: */
307: public void testAssertLinkNotPresent() throws Exception {
308:
309: final String html = "<html><body><a href='foo.html' id='x'>bar</a></body></html>";
310: final HtmlPage page = loadPage(html);
311:
312: WebAssert.assertLinkNotPresent(page, "z");
313:
314: boolean caught = false;
315: try {
316: WebAssert.assertLinkNotPresent(page, "x");
317: } catch (final AssertionError e) {
318: caught = true;
319: }
320: assertTrue(caught);
321: }
322:
323: /**
324: * @throws Exception if an error occurs
325: */
326: public void testAssertLinkPresentWithText() throws Exception {
327:
328: final String html = "<html><body><a href='foo.html' id='x'>bar</a></body></html>";
329: final HtmlPage page = loadPage(html);
330:
331: WebAssert.assertLinkPresentWithText(page, "r");
332:
333: boolean caught = false;
334: try {
335: WebAssert.assertLinkPresentWithText(page, "x");
336: } catch (final AssertionError e) {
337: caught = true;
338: }
339: assertTrue(caught);
340: }
341:
342: /**
343: * @throws Exception if an error occurs
344: */
345: public void testAssertLinkNotPresentWithText() throws Exception {
346:
347: final String html = "<html><body><a href='foo.html' id='x'>bar</a></body></html>";
348: final HtmlPage page = loadPage(html);
349:
350: WebAssert.assertLinkNotPresentWithText(page, "x");
351:
352: boolean caught = false;
353: try {
354: WebAssert.assertLinkNotPresentWithText(page, "r");
355: } catch (final AssertionError e) {
356: caught = true;
357: }
358: assertTrue(caught);
359: }
360:
361: /**
362: * @throws Exception if an error occurs
363: */
364: public void testAssertFormPresent() throws Exception {
365:
366: final String html = "<html><body><form name='f'>bar</form></body></html>";
367: final HtmlPage page = loadPage(html);
368:
369: WebAssert.assertFormPresent(page, "f");
370:
371: boolean caught = false;
372: try {
373: WebAssert.assertFormPresent(page, "x");
374: } catch (final AssertionError e) {
375: caught = true;
376: }
377: assertTrue(caught);
378: }
379:
380: /**
381: * @throws Exception if an error occurs
382: */
383: public void testAssertFormNotPresent() throws Exception {
384:
385: final String html = "<html><body><form name='f'>bar</form></body></html>";
386: final HtmlPage page = loadPage(html);
387:
388: WebAssert.assertFormNotPresent(page, "x");
389:
390: boolean caught = false;
391: try {
392: WebAssert.assertFormNotPresent(page, "f");
393: } catch (final AssertionError e) {
394: caught = true;
395: }
396: assertTrue(caught);
397: }
398:
399: /**
400: * @throws Exception if an error occurs
401: */
402: public void testAssertInputPresent() throws Exception {
403:
404: final String html = "<html><body><form name='f'><input name='i' value='x'/></form></body></html>";
405: final HtmlPage page = loadPage(html);
406:
407: WebAssert.assertInputPresent(page, "i");
408:
409: boolean caught = false;
410: try {
411: WebAssert.assertInputPresent(page, "q");
412: } catch (final AssertionError e) {
413: caught = true;
414: }
415: assertTrue(caught);
416: }
417:
418: /**
419: * @throws Exception if an error occurs
420: */
421: public void testAssertInputNotPresent() throws Exception {
422:
423: final String html = "<html><body><form name='f'><input name='i' value='x'/></form></body></html>";
424: final HtmlPage page = loadPage(html);
425:
426: WebAssert.assertInputNotPresent(page, "q");
427:
428: boolean caught = false;
429: try {
430: WebAssert.assertInputNotPresent(page, "i");
431: } catch (final AssertionError e) {
432: caught = true;
433: }
434: assertTrue(caught);
435: }
436:
437: /**
438: * @throws Exception if an error occurs
439: */
440: public void testAssertInputContainsValue() throws Exception {
441:
442: final String html = "<html><body><form name='f'><input name='i' value='x'/></form></body></html>";
443: final HtmlPage page = loadPage(html);
444:
445: WebAssert.assertInputContainsValue(page, "i", "x");
446:
447: boolean caught = false;
448: try {
449: WebAssert.assertInputContainsValue(page, "i", "z");
450: } catch (final AssertionError e) {
451: caught = true;
452: }
453: assertTrue(caught);
454:
455: caught = false;
456: try {
457: WebAssert.assertInputContainsValue(page, "q", "x");
458: } catch (final AssertionError e) {
459: caught = true;
460: }
461: assertTrue(caught);
462: }
463:
464: /**
465: * @throws Exception if an error occurs
466: */
467: public void testAssertInputDoesNotContainValue() throws Exception {
468:
469: final String html = "<html><body><form name='f'><input name='i' value='x'/></form></body></html>";
470: final HtmlPage page = loadPage(html);
471:
472: WebAssert.assertInputDoesNotContainValue(page, "i", "z");
473:
474: boolean caught = false;
475: try {
476: WebAssert.assertInputDoesNotContainValue(page, "i", "x");
477: } catch (final AssertionError e) {
478: caught = true;
479: }
480: assertTrue(caught);
481:
482: caught = false;
483: try {
484: WebAssert.assertInputDoesNotContainValue(page, "q", "x");
485: } catch (final AssertionError e) {
486: caught = true;
487: }
488: assertTrue(caught);
489: }
490:
491: /**
492: * @throws Exception if an error occurs
493: */
494: public void testAssertAllTabIndexAttributesSet() throws Exception {
495:
496: final String html1 = "<html><body><a href='#' tabindex='1'>foo</a></body></html>";
497: final HtmlPage page1 = loadPage(html1);
498:
499: WebAssert.assertAllTabIndexAttributesSet(page1);
500:
501: final String html2 = "<html><body><a href='#'>foo</a></body></html>";
502: final HtmlPage page2 = loadPage(html2);
503:
504: boolean caught = false;
505: try {
506: WebAssert.assertAllTabIndexAttributesSet(page2);
507: } catch (final AssertionError e) {
508: caught = true;
509: }
510: assertTrue(caught);
511:
512: final String html3 = "<html><body><a href='#' tabindex='x'>foo</a></body></html>";
513: final HtmlPage page3 = loadPage(html3);
514:
515: caught = false;
516: try {
517: WebAssert.assertAllTabIndexAttributesSet(page3);
518: } catch (final AssertionError e) {
519: caught = true;
520: }
521: assertTrue(caught);
522: }
523:
524: /**
525: * @throws Exception if an error occurs
526: */
527: public void testAssertAllAccessKeyAttributesUnique()
528: throws Exception {
529:
530: final String html1 = "<html><body><a accesskey='k'>foo</a></body></html>";
531: final HtmlPage page1 = loadPage(html1);
532:
533: WebAssert.assertAllAccessKeyAttributesUnique(page1);
534:
535: final String html2 = "<html><body><a accesskey='k'>foo</a><a accesskey='k'>bar</a></body></html>";
536: final HtmlPage page2 = loadPage(html2);
537:
538: boolean caught = false;
539: try {
540: WebAssert.assertAllAccessKeyAttributesUnique(page2);
541: } catch (final AssertionError e) {
542: caught = true;
543: }
544: assertTrue(caught);
545: }
546:
547: /**
548: * @throws Exception if an error occurs
549: */
550: public void testAssertAllIdAttributesUnique() throws Exception {
551:
552: final String html1 = "<html><body><a id='k'>foo</a></body></html>";
553: final HtmlPage page1 = loadPage(html1);
554:
555: WebAssert.assertAllIdAttributesUnique(page1);
556:
557: final String html2 = "<html><body><a id='k'>foo</a><a id='k'>bar</a></body></html>";
558: final HtmlPage page2 = loadPage(html2);
559:
560: boolean caught = false;
561: try {
562: WebAssert.assertAllIdAttributesUnique(page2);
563: } catch (final AssertionError e) {
564: caught = true;
565: }
566: assertTrue(caught);
567: }
568:
569: }
|