1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.jwebunit.tests;
20
21 import static net.sourceforge.jwebunit.junit.JWebUnit.assertLinkNotPresent;
22 import static net.sourceforge.jwebunit.junit.JWebUnit.assertLinkPresent;
23 import static net.sourceforge.jwebunit.junit.JWebUnit.assertTextNotPresent;
24 import static net.sourceforge.jwebunit.junit.JWebUnit.assertTextPresent;
25 import static net.sourceforge.jwebunit.junit.JWebUnit.beginAt;
26 import static net.sourceforge.jwebunit.junit.JWebUnit.clickButtonWithText;
27 import static net.sourceforge.jwebunit.junit.JWebUnit.setBaseUrl;
28 import static net.sourceforge.jwebunit.junit.JWebUnit.setExpectedJavaScriptAlert;
29 import static net.sourceforge.jwebunit.junit.JWebUnit.setExpectedJavaScriptConfirm;
30 import static net.sourceforge.jwebunit.junit.JWebUnit.setExpectedJavaScriptPrompt;
31 import static org.junit.Assert.fail;
32
33 import org.junit.Test;
34
35
36
37
38
39 public class JavaScriptTest extends JWebUnitAPITestCase {
40
41 public void setUp() throws Exception {
42 super.setUp();
43 setBaseUrl(HOST_PATH + "/JavaScriptTest");
44 }
45
46 @Test public void testDocumentWrite() {
47 beginAt("DocumentWrite.html");
48 assertTextPresent("Hello World");
49 }
50
51 @Test public void testAlert() {
52 setExpectedJavaScriptAlert("Foo Bar");
53 beginAt("Alert.html");
54 }
55
56 @Test public void testInvalidAlertOnPageLoad() {
57 setExpectedJavaScriptAlert("invalid");
58 try {
59 beginAt("Alert.html");
60 fail();
61 } catch (RuntimeException e) {
62
63 }
64 }
65
66 @Test public void testMultipleAlerts() {
67 setExpectedJavaScriptAlert(new String[] {"Alert 1", "Alert 2"});
68 beginAt("MultipleAlerts.html");
69 }
70
71 @Test public void testConfirm() {
72 setExpectedJavaScriptConfirm("Foo Bar", true);
73 beginAt("Confirm.html");
74 assertLinkPresent("Toto");
75 assertLinkNotPresent("Titi");
76 }
77
78 @Test public void testPrompt() {
79 setExpectedJavaScriptPrompt("Foo Bar", "toto");
80 beginAt("Prompt.html");
81 assertTextPresent("Toto");
82 }
83
84 @Test public void testPromptCanceled() {
85 setExpectedJavaScriptPrompt("Foo Bar", null);
86 beginAt("Prompt.html");
87 assertTextPresent("Cancel");
88 }
89
90
91
92
93
94
95 @Test public void testUserAgent() {
96 beginAt("userAgent.html");
97 assertTextPresent("Mozilla");
98 }
99
100
101
102
103
104
105
106
107
108 @Test public void testPrototypeJs() throws InterruptedException {
109 beginAt("prototype.html");
110 clickButtonWithText("do ajax");
111
112 Thread.sleep(500);
113 assertTextPresent("hello, world!");
114 assertTextNotPresent("not loaded");
115 }
116
117 }