001    /*
002     * Copyright 2006-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package edu.samplu.common;
017    
018    import com.thoughtworks.selenium.Selenium;
019    import org.junit.After;
020    import org.junit.Assert;
021    import org.junit.Before;
022    import org.openqa.selenium.NoSuchFrameException;
023    import org.openqa.selenium.WebDriver;
024    import org.openqa.selenium.WebDriverBackedSelenium;
025    
026    import static junit.framework.Assert.fail;
027    import static org.junit.Assert.assertTrue;
028    
029    /**
030     * @deprecated Use WebDriverITBase for new tests.
031     * @author Kuali Rice Team (rice.collab@kuali.org)
032     */
033    public abstract class UpgradedSeleniumITBase {
034        private Selenium selenium;
035        protected WebDriver driver;
036    
037        protected String getBaseUrlString() {
038            return ITUtil.getBaseUrlString();
039        }
040    
041        /**
042         * Returns the URL to be used with this test
043         *
044         * @return URL of the test
045         */
046        public abstract String getTestUrl();
047    
048        /**
049         * Override in test to define a user other than admin
050         * @return
051         */
052        public String getUserName() {
053            return "admin";
054        }
055    
056        @Before
057        public void setUp() throws Exception {
058            driver = ITUtil.getWebDriver();
059            if (!getTestUrl().startsWith("/")) {
060                fail("getTestUrl does not start with /"); // TODO add it?
061            }
062            selenium = new WebDriverBackedSelenium(driver, ITUtil.getBaseUrlString() + getTestUrl());
063    
064            // Login
065            selenium.open(ITUtil.getBaseUrlString() + getTestUrl());
066            ITUtil.loginSe(selenium, getUserName());
067        }
068    
069        protected void assertDocFinal(String docId) {
070            ITUtil.assertDocFinal(selenium, docId);
071        }
072    
073        protected void assertElementPresent(String locator) {
074            assertTrue(isElementPresent(locator));
075        }
076    
077        protected void assertTextPresent(String text) {
078            assertTextPresent("", text);
079        }
080    
081        protected void assertTextPresent(String message, String text) {
082            assertTrue(text + " text not present " + message, selenium.isTextPresent(text));
083        }
084    
085        protected void blanketApproveTest() throws InterruptedException {
086            ITUtil.blanketApprove(selenium);
087        }
088    
089        protected void close() {
090            selenium.close();
091        }
092    
093        protected void check(String locator) {
094            selenium.check(locator);
095        }
096        
097        protected void checkErrorMessageItem(String message) {
098            ITUtil.checkErrorMessageItem(selenium, message);
099        }
100    
101        protected void checkForIncidentReport(String locator) {
102            checkForIncidentReport(locator, "");
103        }
104    
105        protected void checkForIncidentReport(String locator, String message) {
106            ITUtil.checkForIncidentReport(selenium, message);
107        }
108    
109        protected void chooseCancelOnNextConfirmation() {
110            selenium.chooseCancelOnNextConfirmation();
111        }
112    
113        protected void colapseExpand(String clickLocator, String visibleLocator) throws InterruptedException {
114            waitAndClick(clickLocator);
115            waitNotVisible(visibleLocator);
116    
117            waitAndClick(clickLocator);
118            waitIsVisible(visibleLocator);
119        }
120    
121        protected void expandColapse(String clickLocator, String visibleLocator) throws InterruptedException {
122            waitAndClick(clickLocator);
123            waitIsVisible(visibleLocator);
124    
125            waitAndClick(clickLocator);
126            waitNotVisible(visibleLocator);
127        }
128    
129        protected void fireEvent(String locator, String event) {
130            selenium.fireEvent(locator, event);
131        }
132    
133        protected void focus(String locator) {
134            selenium.focus(locator);
135        }
136    
137        protected void focusAndType(String fieldLocator, String typeText) {
138            selenium.focus(fieldLocator);
139            selenium.type(fieldLocator, typeText);
140        }
141    
142        protected String[] getAllWindowTitles() {
143            return selenium.getAllWindowTitles();
144        }
145    
146        protected String getAttribute(String attributeLocator) {
147            return selenium.getAttribute(attributeLocator);
148        }
149    
150        protected String getConfirmation() {
151            return selenium.getConfirmation();
152        }
153    
154        protected Number getCssCount(String cssCountRows) {
155            return selenium.getCssCount(cssCountRows);
156        }
157    
158        protected String getSelectedLabel(String locator) {
159            return selenium.getSelectedLabel(locator);
160        }
161    
162        protected String[] getSelectOptions(String locator) {
163            return selenium.getSelectOptions(locator);
164        }
165        
166        protected String getEval(String script) {
167            return selenium.getEval(script);
168        }
169        
170        protected String getLocation() {
171            return selenium.getLocation();
172        }
173        
174        protected String getText(String locator) {
175            return selenium.getText(locator);
176        }
177    
178        protected String getTitle() {
179            return selenium.getTitle();
180        }
181    
182        protected String getValue(String locator) {
183            return selenium.getValue(locator);
184        }
185    
186        protected boolean isElementPresent(String locator) {
187            return selenium.isElementPresent(locator);
188        }
189        
190        protected boolean isTextPresent(String locator) {
191            return selenium.isTextPresent(locator);
192        }
193    
194        protected boolean isVisible(String locator) {
195            return selenium.isVisible(locator);
196        }
197    
198        protected void keyDown(String locator, String key) {
199            selenium.keyDown(locator, key);
200        }
201    
202        protected void keyPress(String locator, String key) {
203            selenium.keyPress(locator, key);
204        }
205    
206        protected void keyUp(String locator, String key) {
207            selenium.keyUp(locator, key);
208        }
209    
210        protected void mouseOver(String locator) {
211            selenium.mouseOver(locator);
212        }
213        
214        protected void mouseOut(String locator) {
215            selenium.mouseOut(locator);
216        }
217        
218        protected void open(String url) {
219            selenium.open(url);
220        }
221        
222        protected void removeAllSelections(String locator) {
223            selenium.removeAllSelections(locator);
224        }
225        
226        protected void select(String locator, String select) {
227            selenium.select(locator, select);
228        }
229    
230        protected void selectFrame(String frameName) {
231            try {
232                selenium.selectFrame(frameName);
233            } catch (NoSuchFrameException nsfe) {
234                // do nothing, don't fail on a missing iframe
235            }
236        }
237    
238        protected void setSpeed(String speed) {
239            selenium.setSpeed(speed);
240        }
241    
242        protected void selectWindow(String windowName) {
243            selenium.selectWindow(windowName);
244        }
245    
246        protected void uncheck(String locator) {
247            selenium.uncheck(locator);
248        }
249        
250        protected void waitAndClick(String locator) throws InterruptedException {
251            waitAndClick(locator, "");
252        }
253    
254        protected void waitAndClick(String locator, String message) throws InterruptedException {
255            ITUtil.waitAndClick(selenium, locator, message);
256        }
257    
258        protected String waitForDocId() throws InterruptedException {
259            ITUtil.waitForElement(selenium, AdminMenuITBase.DOC_ID_LOCATOR);
260            return selenium.getText(AdminMenuITBase.DOC_ID_LOCATOR);
261        }
262    
263        protected void waitAndType(String elementLocator, String text) throws InterruptedException {
264            waitAndType(elementLocator, text, "");
265        }
266    
267        protected void waitAndType(String elementLocator, String text, String message) throws InterruptedException {
268            ITUtil.waitAndType(selenium, elementLocator, text, message);
269        }
270    
271        protected void waitNotVisible(String visibleLocator) throws InterruptedException {
272            for (int second = 0;; second++) {
273                if (second >= 15) {
274                    Assert.fail("timeout");
275                }
276    
277                if (!isVisible(visibleLocator)) {
278                    break;
279                }
280    
281                Thread.sleep(1000);
282            }
283        }
284    
285        protected void waitIsVisible(String visibleLocator) throws InterruptedException {
286            for (int second = 0;; second++) {
287                if (second >= 15) {
288                    Assert.fail("timeout");
289                }
290                if (isVisible(visibleLocator)) {
291                    break;
292                }
293                Thread.sleep(1000);
294            }
295        }
296    
297        protected void waitForElementPresent(String locator) throws InterruptedException {
298            waitForElementPresent(locator, "");
299        }
300    
301        // TODO this should be message first
302        protected void waitForElementPresent(String locator, String message) throws InterruptedException {
303            ITUtil.waitForElement(selenium, locator, message);
304        }
305        
306        protected void waitForElementVisible(String locator, String message) throws InterruptedException {
307            ITUtil.waitForElementVisible(selenium, locator, message);
308        }
309    
310        protected void waitForPageToLoad() {
311            waitForPageToLoad(ITUtil.DEFAULT_WAIT_FOR_PAGE_TO_LOAD_TIMEOUT);
312        }
313    
314        protected void waitForPageToLoad50000() {
315            waitForPageToLoad("50000");
316        }
317    
318        protected void waitForPageToLoad(String number) {
319            selenium.waitForPageToLoad(number);
320        }
321        
322        protected void waitForTitleToEqualKualiPortalIndex() throws InterruptedException {
323            waitForTitleToEqualKualiPortalIndex("");
324        }
325    
326        protected void waitForTitleToEqualKualiPortalIndex(String message) throws InterruptedException {
327            ITUtil.waitForTitleToEqual(selenium, ITUtil.KUALI_PORTAL_TITLE, message);
328        }
329    
330        protected void windowFocus() {
331            selenium.windowFocus();
332        }
333    
334        /**
335         * Useful to set -Dremote.driver.dontTearDown=f  -Dremote.driver.dontTearDown=n to not shutdown the browser when
336         * working on tests.
337         * @throws Exception
338         */
339        @After
340        public void tearDown() throws Exception {
341            if (ITUtil.dontTearDownPropertyNotSet()) {
342                selenium.stop();
343                driver.quit(); // TODO not tested with chrome, the service stop might need this check too
344            }
345        }
346    }