View Javadoc

1   /*
2    * Copyright 2006-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package edu.samplu.common;
17  
18  import com.thoughtworks.selenium.Selenium;
19  import org.junit.After;
20  import org.junit.Assert;
21  import org.junit.Before;
22  import org.openqa.selenium.WebDriver;
23  import org.openqa.selenium.WebDriverBackedSelenium;
24  
25  import static junit.framework.Assert.fail;
26  import static org.junit.Assert.assertTrue;
27  
28  /**
29   * @deprecated Use WebDriverITBase for new tests.
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  public abstract class UpgradedSeleniumITBase {
33      private Selenium selenium;
34      protected WebDriver driver;
35  
36      protected String getBaseUrlString() {
37          return ITUtil.getBaseUrlString();
38      }
39  
40      /**
41       * Returns the URL to be used with this test
42       *
43       * @return URL of the test
44       */
45      public abstract String getTestUrl();
46  
47      /**
48       * Override in test to define a user other than admin
49       * @return
50       */
51      public String getUserName() {
52          return "admin";
53      }
54  
55      @Before
56      public void setUp() throws Exception {
57          driver = ITUtil.getWebDriver();
58          if (!getTestUrl().startsWith("/")) {
59              fail("getTestUrl does not start with /"); // TODO add it?
60          }
61          selenium = new WebDriverBackedSelenium(driver, ITUtil.getBaseUrlString() + getTestUrl());
62  
63          // Login
64          selenium.open(ITUtil.getBaseUrlString() + getTestUrl());
65          ITUtil.loginSe(selenium, getUserName());
66      }
67  
68      protected void assertDocFinal(String docId) {
69          ITUtil.assertDocFinal(selenium, docId);
70      }
71  
72      protected void assertElementPresent(String locator) {
73          assertTrue(isElementPresent(locator));
74      }
75  
76      protected void assertTextPresent(String text) {
77          assertTextPresent("", text);
78      }
79  
80      protected void assertTextPresent(String message, String text) {
81          assertTrue(text + " text not present " + message, selenium.isTextPresent(text));
82      }
83  
84      protected void blanketApproveTest() throws InterruptedException {
85          ITUtil.blanketApprove(selenium);
86      }
87  
88      protected void close() {
89          selenium.close();
90      }
91  
92      protected void check(String locator) {
93          selenium.check(locator);
94      }
95      
96      protected void checkErrorMessageItem(String message) {
97          ITUtil.checkErrorMessageItem(selenium, message);
98      }
99  
100     protected void checkForIncidentReport(String locator) {
101         checkForIncidentReport(locator, "");
102     }
103 
104     protected void checkForIncidentReport(String locator, String message) {
105         ITUtil.checkForIncidentReport(selenium, message);
106     }
107 
108     protected void chooseCancelOnNextConfirmation() {
109         selenium.chooseCancelOnNextConfirmation();
110     }
111 
112     protected void colapseExpand(String clickLocator, String visibleLocator) throws InterruptedException {
113         waitAndClick(clickLocator);
114         waitNotVisible(visibleLocator);
115 
116         waitAndClick(clickLocator);
117         waitIsVisible(visibleLocator);
118     }
119 
120     protected void expandColapse(String clickLocator, String visibleLocator) throws InterruptedException {
121         waitAndClick(clickLocator);
122         waitIsVisible(visibleLocator);
123 
124         waitAndClick(clickLocator);
125         waitNotVisible(visibleLocator);
126     }
127 
128     protected void fireEvent(String locator, String event) {
129         selenium.fireEvent(locator, event);
130     }
131 
132     protected void focus(String locator) {
133         selenium.focus(locator);
134     }
135 
136     protected void focusAndType(String fieldLocator, String typeText) {
137         selenium.focus(fieldLocator);
138         selenium.type(fieldLocator, typeText);
139     }
140 
141     protected String[] getAllWindowTitles() {
142         return selenium.getAllWindowTitles();
143     }
144 
145     protected String getAttribute(String attributeLocator) {
146         return selenium.getAttribute(attributeLocator);
147     }
148 
149     protected String getConfirmation() {
150         return selenium.getConfirmation();
151     }
152 
153     protected Number getCssCount(String cssCountRows) {
154         return selenium.getCssCount(cssCountRows);
155     }
156 
157     protected String getSelectedLabel(String locator) {
158         return selenium.getSelectedLabel(locator);
159     }
160 
161     protected String[] getSelectOptions(String locator) {
162         return selenium.getSelectOptions(locator);
163     }
164     
165     protected String getEval(String script) {
166         return selenium.getEval(script);
167     }
168     
169     protected String getLocation() {
170         return selenium.getLocation();
171     }
172     
173     protected String getText(String locator) {
174         return selenium.getText(locator);
175     }
176 
177     protected String getTitle() {
178         return selenium.getTitle();
179     }
180 
181     protected String getValue(String locator) {
182         return selenium.getValue(locator);
183     }
184 
185     protected boolean isElementPresent(String locator) {
186         return selenium.isElementPresent(locator);
187     }
188     
189     protected boolean isTextPresent(String locator) {
190         return selenium.isTextPresent(locator);
191     }
192 
193     protected boolean isVisible(String locator) {
194         return selenium.isVisible(locator);
195     }
196 
197     protected void keyDown(String locator, String key) {
198         selenium.keyDown(locator, key);
199     }
200 
201     protected void keyPress(String locator, String key) {
202         selenium.keyPress(locator, key);
203     }
204 
205     protected void keyUp(String locator, String key) {
206         selenium.keyUp(locator, key);
207     }
208 
209     protected void mouseOver(String locator) {
210         selenium.mouseOver(locator);
211     }
212     
213     protected void mouseOut(String locator) {
214         selenium.mouseOut(locator);
215     }
216     
217     protected void open(String url) {
218         selenium.open(url);
219     }
220     
221     protected void removeAllSelections(String locator) {
222         selenium.removeAllSelections(locator);
223     }
224     
225     protected void select(String locator, String select) {
226         selenium.select(locator, select);
227     }
228 
229     protected void selectFrame(String frameName) {
230         selenium.selectFrame(frameName);
231     }
232 
233     protected void setSpeed(String speed) {
234         selenium.setSpeed(speed);
235     }
236 
237     protected void selectWindow(String windowName) {
238         selenium.selectWindow(windowName);
239     }
240 
241     protected void uncheck(String locator) {
242         selenium.uncheck(locator);
243     }
244     
245     protected void waitAndClick(String locator) throws InterruptedException {
246         waitAndClick(locator, "");
247     }
248 
249     protected void waitAndClick(String locator, String message) throws InterruptedException {
250         ITUtil.waitAndClick(selenium, locator, message);
251     }
252 
253     protected String waitForDocId() throws InterruptedException {
254         ITUtil.waitForElement(selenium, AdminMenuITBase.DOC_ID_LOCATOR);
255         return selenium.getText(AdminMenuITBase.DOC_ID_LOCATOR);
256     }
257 
258     protected void waitAndType(String elementLocator, String text) throws InterruptedException {
259         waitAndType(elementLocator, text, "");
260     }
261 
262     protected void waitAndType(String elementLocator, String text, String message) throws InterruptedException {
263         ITUtil.waitAndType(selenium, elementLocator, text, message);
264     }
265 
266     protected void waitNotVisible(String visibleLocator) throws InterruptedException {
267         for (int second = 0;; second++) {
268             if (second >= 15) {
269                 Assert.fail("timeout");
270             }
271 
272             if (!isVisible(visibleLocator)) {
273                 break;
274             }
275 
276             Thread.sleep(1000);
277         }
278     }
279 
280     protected void waitIsVisible(String visibleLocator) throws InterruptedException {
281         for (int second = 0;; second++) {
282             if (second >= 15) {
283                 Assert.fail("timeout");
284             }
285             if (isVisible(visibleLocator)) {
286                 break;
287             }
288             Thread.sleep(1000);
289         }
290     }
291 
292     protected void waitForElementPresent(String locator) throws InterruptedException {
293         waitForElementPresent(locator, "");
294     }
295 
296     // TODO this should be message first
297     protected void waitForElementPresent(String locator, String message) throws InterruptedException {
298         ITUtil.waitForElement(selenium, locator, message);
299     }
300     
301     protected void waitForElementVisible(String locator, String message) throws InterruptedException {
302         ITUtil.waitForElementVisible(selenium, locator, message);
303     }
304 
305     protected void waitForPageToLoad() {
306         waitForPageToLoad(ITUtil.DEFAULT_WAIT_FOR_PAGE_TO_LOAD_TIMEOUT);
307     }
308 
309     protected void waitForPageToLoad50000() {
310         waitForPageToLoad("50000");
311     }
312 
313     protected void waitForPageToLoad(String number) {
314         selenium.waitForPageToLoad(number);
315     }
316     
317     protected void waitForTitleToEqualKualiPortalIndex() throws InterruptedException {
318         waitForTitleToEqualKualiPortalIndex("");
319     }
320 
321     protected void waitForTitleToEqualKualiPortalIndex(String message) throws InterruptedException {
322         ITUtil.waitForTitleToEqual(selenium, ITUtil.KUALI_PORTAL_TITLE, message);
323     }
324 
325     protected void windowFocus() {
326         selenium.windowFocus();
327     }
328 
329     /**
330      * Useful to set -Dremote.driver.dontTearDown=f  -Dremote.driver.dontTearDown=n to not shutdown the browser when
331      * working on tests.
332      * @throws Exception
333      */
334     @After
335     public void tearDown() throws Exception {
336         if (ITUtil.dontTearDownPropertyNotSet()) {
337             selenium.stop();
338             driver.quit(); // TODO not tested with chrome, the service stop might need this check too
339         }
340     }
341 }