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.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   * @author henryju
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      		//OK
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       * Test that the <code>navigator.userAgent</code> is actually available. 
92       * 
93       * @see bug 1724695
94       */
95      @Test public void testUserAgent() {
96      	beginAt("userAgent.html");
97      	assertTextPresent("Mozilla");	// the default browser is a Mozilla browser
98      }
99      
100     /**
101      * Test prototype.js integration and make sure that it works
102      * in JWebUnit
103      * 
104      * @see bug 2208784 
105      * @author Jevon
106      * @throws InterruptedException 
107      */
108     @Test public void testPrototypeJs() throws InterruptedException {
109     	beginAt("prototype.html");
110     	clickButtonWithText("do ajax");
111     	// we wait a while for the ajax to return
112     	Thread.sleep(500);
113     	assertTextPresent("hello, world!");
114     	assertTextNotPresent("not loaded");
115     }
116     
117 }