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 017 package edu.samplu.common; 018 019 import static com.thoughtworks.selenium.SeleneseTestBase.fail; 020 import static org.junit.Assert.assertEquals; 021 022 import java.util.ArrayList; 023 import java.util.List; 024 import java.util.Set; 025 import java.util.concurrent.TimeUnit; 026 027 import org.junit.After; 028 import org.junit.AfterClass; 029 import org.junit.Before; 030 import org.junit.BeforeClass; 031 import org.openqa.selenium.Alert; 032 import org.openqa.selenium.By; 033 import org.openqa.selenium.JavascriptExecutor; 034 import org.openqa.selenium.WebDriver; 035 import org.openqa.selenium.chrome.ChromeDriverService; 036 037 038 /** 039 * Base class for Selenium Webdriver integration tests 040 * 041 * @author Kuali Rice Team (rice.collab@kuali.org) 042 */ 043 public abstract class WebDriverITBase { 044 045 public WebDriver driver; 046 static ChromeDriverService chromeDriverService; 047 048 /** 049 * Returns the URL to be used with this test 050 * 051 * @return URL of the test 052 */ 053 public abstract String getTestUrl(); 054 055 /** 056 * Override in test to define a user other than admin 057 * @return 058 */ 059 public String getUserName() { 060 return "admin"; 061 } 062 063 @BeforeClass 064 public static void createAndStartService() throws Exception { 065 chromeDriverService = WebDriverUtil.createAndStartService(); 066 if (chromeDriverService != null) chromeDriverService.start(); 067 } 068 069 070 /** 071 * Setup the WebDriver test, login and load the tested web page 072 * 073 * @throws Exception 074 */ 075 @Before 076 public void setUp() throws Exception { 077 driver = WebDriverUtil.setUp(getUserName(), ITUtil.getBaseUrlString() + "/" + getTestUrl()); 078 } 079 080 /** 081 * Tear down the WebDriver test 082 * 083 * @throws Exception 084 */ 085 @After 086 public void tearDown() throws Exception { 087 if (ITUtil.dontTearDownPropertyNotSet()) { 088 driver.quit(); // TODO not tested with chrome, the service stop might need this check too 089 } 090 } 091 092 /** 093 * Tear down the WebDriver test 094 * 095 * @throws Exception 096 */ 097 @AfterClass 098 public static void stopService() throws Exception { 099 if (chromeDriverService != null) { 100 chromeDriverService.stop(); 101 } 102 } 103 104 /** 105 * Check if an element is present 106 * 107 * <p> 108 * This test takes a while due to the 'implicit wait' time. 109 * </p> 110 * 111 * @param by The locating mechanism of the element 112 * @return true if the element is present, false otherwise 113 */ 114 public boolean isElementPresent(By by) { 115 if (driver.findElements(by).isEmpty()) { 116 return false; 117 } else { 118 return true; 119 } 120 } 121 122 /** 123 * Quickly check if an element is present 124 * 125 * <p> 126 * Just like {@link #isElementPresent(org.openqa.selenium.By)} but with a short 'implicit wait' time. Use this only 127 * if it is guaranteed that all elements are rendered. 128 * </p> 129 * 130 * @param by The locating mechanism of the element 131 * @return true if the element is present, false otherwise 132 */ 133 public boolean isElementPresentQuick(By by) { 134 driver.manage().timeouts().implicitlyWait(WebDriverUtil.SHORT_IMPLICIT_WAIT_TIME, TimeUnit.SECONDS); 135 boolean result = isElementPresent(by); 136 driver.manage().timeouts().implicitlyWait(WebDriverUtil.DEFAULT_IMPLICIT_WAIT_TIME, TimeUnit.SECONDS); 137 return result; 138 } 139 140 /** 141 * Assert that clicking an element causes a popup window with a specific URL 142 * 143 * @param by The locating mechanism of the element to be clicked 144 * @param windowName The name of the popup window 145 * @param url The URL of the popup window 146 */ 147 public void assertPopUpWindowUrl(By by, String windowName, String url) { 148 driver.findElement(by).click(); 149 String parentWindowHandle = driver.getWindowHandle(); 150 // wait page to be loaded 151 driver.switchTo().window(windowName).findElements(By.tagName("head")); 152 assertEquals(url, driver.getCurrentUrl()); 153 driver.switchTo().window(parentWindowHandle); 154 } 155 156 /** 157 * 158 * 159 * @param by The locating mechanism of the element 160 */ 161 protected void waitFor(By by) throws InterruptedException { 162 waitFor(by, ""); 163 } 164 165 /** 166 * 167 * 168 * @param by The locating mechanism of the element 169 * @param message User defined message to display 170 */ 171 protected void waitFor(By by, String message) throws InterruptedException { 172 // for (int second = 0;; second++) { 173 Thread.sleep(1000); 174 // if (second >= DEFAULT_WAIT_SEC) fail(by.toString() + " " + message + " " + DEFAULT_WAIT_SEC + " sec timeout."); 175 try { driver.findElement(by); 176 //break; 177 } catch (Exception e) {} 178 // } 179 } 180 181 /** 182 * 183 * 184 * @param by The locating mechanism of the element 185 * @param text The text to type 186 */ 187 protected void waitAndType(By by, String text) throws InterruptedException { 188 waitFor(by, ""); 189 try { 190 (driver.findElement(by)).sendKeys(text); 191 } catch (Exception e) { 192 fail(e.getMessage() + " " + by.toString() + " " + text); 193 e.printStackTrace(); 194 } 195 } 196 197 /** 198 * 199 * 200 * @param by The locating mechanism of the element 201 * @param text The text to type 202 * @param message User defined message to display 203 */ 204 protected void waitAndType(By by, String text, String message) throws InterruptedException { 205 waitFor(by, ""); 206 try { 207 (driver.findElement(by)).sendKeys(text); 208 } catch (Exception e) { 209 fail(e.getMessage() + " " + by.toString() + " " + text + " "+message); 210 e.printStackTrace(); 211 } 212 } 213 214 /** 215 * 216 * 217 * @param locator The locating mechanism of the element 218 * @param text The text to type 219 */ 220 protected void waitAndTypeByXpath(String locator, String text) throws InterruptedException { 221 waitAndType(By.xpath(locator), text); 222 } 223 224 /** 225 * 226 * 227 * @param locator The locating mechanism of the element 228 * @param text The text to type 229 * @param message User defined message to display 230 */ 231 protected void waitAndTypeByXpath(String locator, String text, String message) throws InterruptedException { 232 waitAndType(By.xpath(locator), text, message); 233 } 234 235 /** 236 * 237 * 238 * @param name The name of the element 239 * @param text The text to type 240 */ 241 protected void waitAndTypeByName(String name, String text) throws InterruptedException { 242 waitAndType(By.name(name), text); 243 } 244 245 /** 246 * Clear the text written in an input field by name of an element 247 * 248 * @param name The name of the element 249 */ 250 protected void clearTextByName(String name) throws InterruptedException { 251 clearText(By.name(name)); 252 } 253 254 /** 255 * Clear the text written in an input field by xpath of an element 256 * 257 * @param locator The locating mechanism of the element 258 */ 259 protected void clearTextByXpath(String locator) throws InterruptedException { 260 clearText(By.xpath(locator)); 261 } 262 263 /** 264 * Clear the text written in an input field by xpath of an element 265 * 266 * @param by method used for finding the element 267 */ 268 protected void clearText(By by) throws InterruptedException { 269 driver.findElement(by).clear(); 270 } 271 272 /** 273 * Dismiss the javascript alert (clicking Cancel) 274 * 275 */ 276 protected void dismissAlert() 277 { 278 Alert alert = driver.switchTo().alert(); 279 //update is executed 280 alert.dismiss(); 281 } 282 283 /** 284 * Accept the javascript alert (clicking OK) 285 * 286 */ 287 protected void acceptAlert() 288 { 289 Alert alert = driver.switchTo().alert(); 290 //update is executed 291 alert.accept(); 292 } 293 294 protected String getEval(String script) 295 { 296 JavascriptExecutor js = (JavascriptExecutor) driver; 297 return (String)js.executeScript(script); 298 } 299 300 /** 301 * Switch to new window 302 * 303 */ 304 protected void switchWindow() 305 { 306 Set<String> winSet = driver.getWindowHandles(); 307 List<String> winList = new ArrayList<String>(winSet); 308 String newTab = winList.get(winList.size() - 1); 309 driver.switchTo().window(newTab); 310 } 311 312 /** 313 * Get value of any attribute by using element name 314 * 315 *@param name name of an element 316 *@param attribute the name of an attribute whose value is to be retrieved 317 */ 318 protected String getAttributeByName(String name,String attribute) throws InterruptedException { 319 return getAttribute(By.name(name),attribute); 320 } 321 322 /** 323 * Get value of any attribute by using element xpath 324 * 325 *@param locator locating mechanism of an element 326 *@param attribute the name of an attribute whose value is to be retrieved 327 */ 328 protected String getAttributeByXpath(String locator,String attribute) throws InterruptedException { 329 return getAttribute(By.xpath(locator),attribute); 330 } 331 332 /** 333 * Get value of any attribute of an element 334 * 335 * @param by method used for finding the element 336 *@param attribute the name of an attribute whose value is to be retrieved 337 */ 338 protected String getAttribute(By by,String attribute) throws InterruptedException { 339 return driver.findElement(by).getAttribute(attribute); 340 } 341 342 /** 343 * 344 * 345 * @param text text of the link 346 */ 347 protected void waitAndClickByLinkText(String text) throws InterruptedException { 348 waitAndClick(By.linkText(text),""); 349 } 350 351 /** 352 * 353 * 354 * @param text text of the link 355 * @param message user defined message to display 356 */ 357 protected void waitAndClickByLinkText(String text, String message) throws InterruptedException { 358 waitAndClick(By.linkText(text), message); 359 } 360 361 /** 362 * 363 * 364 * @param by method used for finding the element 365 */ 366 protected void waitAndClick(By by) throws InterruptedException { 367 waitAndClick(by, ""); 368 } 369 370 /** 371 * 372 * 373 * @param by method used for finding the element 374 * @param message user defined message to display 375 */ 376 protected void waitAndClick(By by, String message) throws InterruptedException { 377 waitFor(by, message); 378 try { 379 (driver.findElement(by)).click(); 380 } catch (Exception e) { 381 fail(e.getMessage() + " " + by.toString() + " " + message); 382 e.printStackTrace(); 383 } 384 } 385 386 /** 387 * 388 * 389 * @param locator mechanism to locate element by xpath 390 */ 391 protected void waitAndClick(String locator) throws InterruptedException { 392 waitAndClick(locator, ""); 393 } 394 395 /** 396 * 397 * 398 * @param locator mechanism to locate element by xpath 399 * @param message user defined message to display 400 */ 401 protected void waitAndClick(String locator, String message) throws InterruptedException { 402 waitAndClick(By.cssSelector(locator), message); 403 } 404 405 /** 406 * 407 * 408 * @param locator mechanism to locate element by xpath 409 */ 410 protected void waitForElementPresent(String locator) throws InterruptedException { 411 waitFor(By.cssSelector(locator)); 412 } 413 414 /** 415 * 416 * 417 * @param locator mechanism to locate element by xpath 418 */ 419 protected void waitForElementPresentByXpath(String locator) throws InterruptedException { 420 waitFor(By.xpath(locator)); 421 } 422 423 /** 424 * 425 * 426 * @param name name of an element 427 */ 428 protected void waitForElementPresentByName(String name) throws InterruptedException { 429 waitFor(By.name(name)); 430 } 431 432 protected void checkForIncidentReport() { 433 checkForIncidentReport("", ""); 434 } 435 436 protected void checkForIncidentReport(String locator) { 437 checkForIncidentReport(locator, ""); 438 } 439 440 protected void checkForIncidentReport(String locator, String message) { 441 WebDriverUtil.checkForIncidentReport(driver, locator, message); 442 } 443 444 445 } 446