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.WebDriver; 023 import org.openqa.selenium.WebDriverBackedSelenium; 024 025 import static junit.framework.Assert.fail; 026 import static org.junit.Assert.assertTrue; 027 028 /** 029 * @deprecated Use WebDriverITBase for new tests. 030 * @author Kuali Rice Team (rice.collab@kuali.org) 031 */ 032 public abstract class UpgradedSeleniumITBase { 033 private Selenium selenium; 034 protected WebDriver driver; 035 036 protected String getBaseUrlString() { 037 return ITUtil.getBaseUrlString(); 038 } 039 040 /** 041 * Returns the URL to be used with this test 042 * 043 * @return URL of the test 044 */ 045 public abstract String getTestUrl(); 046 047 /** 048 * Override in test to define a user other than admin 049 * @return 050 */ 051 public String getUserName() { 052 return "admin"; 053 } 054 055 @Before 056 public void setUp() throws Exception { 057 driver = ITUtil.getWebDriver(); 058 if (!getTestUrl().startsWith("/")) { 059 fail("getTestUrl does not start with /"); // TODO add it? 060 } 061 selenium = new WebDriverBackedSelenium(driver, ITUtil.getBaseUrlString() + getTestUrl()); 062 063 // Login 064 selenium.open(ITUtil.getBaseUrlString() + getTestUrl()); 065 ITUtil.loginSe(selenium, getUserName()); 066 } 067 068 protected void assertDocFinal(String docId) { 069 ITUtil.assertDocFinal(selenium, docId); 070 } 071 072 protected void assertElementPresent(String locator) { 073 assertTrue(isElementPresent(locator)); 074 } 075 076 protected void assertTextPresent(String text) { 077 assertTextPresent("", text); 078 } 079 080 protected void assertTextPresent(String message, String text) { 081 assertTrue(text + " text not present " + message, selenium.isTextPresent(text)); 082 } 083 084 protected void blanketApproveTest() throws InterruptedException { 085 ITUtil.blanketApprove(selenium); 086 } 087 088 protected void close() { 089 selenium.close(); 090 } 091 092 protected void check(String locator) { 093 selenium.check(locator); 094 } 095 096 protected void checkErrorMessageItem(String message) { 097 ITUtil.checkErrorMessageItem(selenium, message); 098 } 099 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 }