KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jwebunit > NavigationTest


1 package net.sourceforge.jwebunit;
2
3 import junit.framework.AssertionFailedError;
4
5 /**
6  * Test url navigation methods on WebTestCase (starting at a url, navigating links).
7  *
8  * @author Jim Weaver
9  * @author Wilkes Joiner
10  */

11 public class NavigationTest extends JWebUnitTest {
12
13     public void setUp() throws Exception JavaDoc {
14         super.setUp();
15     }
16
17     public void testBeginAt() {
18         defineResource("/blah.html", "");
19         beginAt("/blah.html");
20     }
21
22     public void testForwardSlashConfusion() throws Exception JavaDoc {
23         defineWebPage("/app/blah", "here");
24         getTestContext().setBaseUrl(hostPath + "/app");
25         beginAt("/blah.html");
26         beginAt("blah.html");
27         getTestContext().setBaseUrl(hostPath + "/app/");
28         beginAt("/blah.html");
29         beginAt("blah.html");
30     }
31
32     public void testInvalidBeginAt() {
33         defineResource("/", "");
34         try {
35             beginAt("/nosuchresource.html");
36         } catch (Throwable JavaDoc t) {
37             return;
38         }
39         fail("Expected exception");
40     }
41
42     public void testClickLinkWithText() {
43         gotoLinkTestPage();
44         clickLinkWithText("an active link");
45         assertTitleEquals("targetPage");
46     }
47
48     public void testClickLinkWithTextN() {
49         gotoLinkTestPage();
50         clickLinkWithText("an active link", 0);
51         assertTitleEquals("targetPage");
52         beginAt("/pageWithLink.html");
53         clickLinkWithText("an active link", 1);
54         assertTitleEquals("targetPage2");
55         beginAt("/pageWithLink.html");
56         try {
57             clickLinkWithText("an active link", 2);
58             fail();
59         } catch (AssertionFailedError expected) {
60             assertEquals(
61                 "Link with text [an active link] and index 2 "
62                     + "not found in response.",
63                 expected.getMessage());
64         }
65         assertTitleEquals("pageWithLink");
66     }
67
68     public void testClickLinkWithTextAfterText() {
69         defineWebPage("pageWithLink",
70             "First: <a HREF=\"/targetPage1.html\">link text</a>\n" +
71             "Second: <a HREF=\"/targetPage2.html\">link text</a>)\n");
72         defineWebPage("targetPage1", "");
73         defineWebPage("targetPage2", "");
74
75         beginAt("/pageWithLink.html");
76         clickLinkWithTextAfterText("link text", "First:");
77         assertTitleEquals("targetPage1");
78
79         beginAt("/pageWithLink.html");
80         clickLinkWithTextAfterText("link text", "Second:");
81         assertTitleEquals("targetPage2");
82     }
83
84     public void testClickLinkWithImage() {
85         gotoLinkTestPage();
86         clickLinkWithImage("graphic.jpg");
87         assertTitleEquals("targetPage2");
88     }
89
90     public void testClickLinkByID() {
91         gotoLinkTestPage();
92         clickLink("activeID");
93         assertTitleEquals("targetPage");
94     }
95
96     private void gotoLinkTestPage() {
97         defineWebPage("pageWithLink",
98             "<a HREF=\"/targetPage.html\" id=\"activeID\">an <b>active</b> link</A>\n" +
99             "<a HREF=\"/targetPage2.html\"><img SRC=\"graphic.jpg\"/></a>)\n" +
100             "<a HREF=\"/targetPage2.html\">an active <i>link</i></a>\n");
101         defineWebPage("targetPage", "");
102         defineWebPage("targetPage2", "");
103         beginAt("/pageWithLink.html");
104         assertTitleEquals("pageWithLink");
105     }
106
107     public void testInvalidClickLink() {
108         gotoLinkTestPage();
109         try {
110             clickLinkWithText("no such link");
111         } catch (Throwable JavaDoc t) {
112             return;
113         }
114         fail("Expected exception");
115     }
116
117     public void testGotoPage() {
118         defineWebPage("page1", "page1");
119         defineWebPage("page2", "page2");
120         beginAt("/page1.html");
121         assertTitleEquals("page1");
122         gotoPage("page2.html");
123         assertTitleEquals("page2");
124     }
125 }
126
Popular Tags