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