001 package edu.samplu.common; 002 003 import java.io.BufferedReader; 004 import java.io.InputStreamReader; 005 import java.net.HttpURLConnection; 006 import java.net.URL; 007 import java.util.List; 008 import java.util.concurrent.TimeUnit; 009 010 //import com.saucelabs.common.SauceOnDemandAuthentication; 011 //import com.saucelabs.common.SauceOnDemandSessionIdProvider; 012 //import com.saucelabs.junit.SauceOnDemandTestWatcher; 013 //import com.saucelabs.saucerest.SauceREST; 014 import org.junit.After; 015 import org.junit.Assert; 016 import org.junit.Before; 017 import org.junit.BeforeClass; 018 import org.junit.Rule; 019 import org.junit.rules.TestName; 020 import org.openqa.selenium.By; 021 import org.openqa.selenium.JavascriptExecutor; 022 import org.openqa.selenium.WebDriver; 023 import org.openqa.selenium.WebElement; 024 import org.openqa.selenium.chrome.ChromeDriverService; 025 import org.openqa.selenium.remote.RemoteWebDriver; 026 027 import static com.thoughtworks.selenium.SeleneseTestBase.fail; 028 import static org.junit.Assert.assertEquals; 029 030 /** 031 * Class to upgrade UpgradedSeleniumITBase tests to WebDriver. 032 * @deprecated Use WebDriverITBase for new tests. 033 * @author Kuali Rice Team (rice.collab@kuali.org) 034 */ 035 public abstract class WebDriverLegacyITBase { //} implements SauceOnDemandSessionIdProvider { 036 037 public static final int DEFAULT_WAIT_SEC = 60; 038 public static final String SAUCE_USER = "SAUCE_USER"; 039 public static final String SAUCE_KEY = "SAUCE_KEY"; 040 041 public abstract String getTestUrl(); 042 043 protected WebDriver driver; 044 protected String user = "admin"; 045 protected boolean passed = false; 046 static ChromeDriverService chromeDriverService; 047 048 // public SauceOnDemandAuthentication authentication = new SauceOnDemandAuthentication(SAUCE_USER, SAUCE_KEY); 049 // 050 // public @Rule SauceOnDemandTestWatcher resultReportingTestWatcher = new SauceOnDemandTestWatcher(this, authentication); 051 052 public @Rule TestName testName= new TestName(); 053 054 String sessionId = null; 055 056 public String getSessionId() { 057 return sessionId; 058 } 059 060 @BeforeClass 061 public static void createAndStartService() throws Exception { 062 chromeDriverService = WebDriverUtil.createAndStartService(); 063 if (chromeDriverService != null) chromeDriverService.start(); 064 } 065 066 /** 067 * Setup the WebDriver test, login and load the tested web page 068 * 069 * @throws Exception 070 */ 071 @Before 072 public void setUp() throws Exception { 073 // {"test":"1","user":"1"} 074 // String userResponse = getHTML("http://testuserpool.appspot.com/userpool?test=" + this.toString().trim()); 075 // user = userResponse.substring(userResponse.lastIndexOf(":" ) + 2, userResponse.lastIndexOf("\"")); 076 driver = WebDriverUtil.setUp(getUserName(), ITUtil.getBaseUrlString() + "/" + getTestUrl(), 077 getClass().getSimpleName(), testName); 078 this.sessionId = ((RemoteWebDriver)driver).getSessionId().toString(); 079 } 080 081 @After 082 public void tearDown() throws Exception { 083 // SauceREST client = new SauceREST(SAUCE_USER, SAUCE_KEY); 084 // /* Using a map of udpates: 085 // * (http://saucelabs.com/docs/sauce-ondemand#alternative-annotation-methods) 086 // * 087 // * Map<String, Object> updates = new HashMap<String, Object>(); 088 // * updates.put("name", "this job has a name"); 089 // * updates.put("passed", true); 090 // * updates.put("build", "c234"); 091 // * client.updateJobInfo("<your-job-id>", updates); 092 // */ 093 // if (passed) { 094 // client.jobPassed(sessionId); 095 // } else { 096 // client.jobFailed(sessionId); 097 // } 098 // 099 // getHTML("http://testuserpool.appspot.com/userpool?test=" + this.toString() + "&user=" + user); 100 driver.close(); 101 driver.quit(); 102 } 103 104 protected String getHTML(String urlToRead) { 105 URL url; 106 HttpURLConnection conn; 107 BufferedReader rd; 108 String line; 109 String result = ""; 110 try { 111 url = new URL(urlToRead); 112 conn = (HttpURLConnection) url.openConnection(); 113 conn.setRequestMethod("GET"); 114 rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 115 while ((line = rd.readLine()) != null) { 116 result += line; 117 } 118 rd.close(); 119 } catch (Exception e) { 120 e.printStackTrace(); 121 } 122 return result; 123 } 124 125 protected void passed() { 126 passed = true; 127 } 128 129 protected void assertElementPresentByName(String name) { 130 driver.findElement(By.name(name)); 131 } 132 133 protected void assertElementPresentByName(String name,String message) { 134 try{ 135 driver.findElement(By.name(name)); 136 }catch(Exception e){ 137 Assert.fail(name+ " not present "+ message); 138 } 139 } 140 141 protected void assertElementPresentByXpath(String locator) { 142 driver.findElement(By.xpath(locator)); 143 } 144 145 protected void assertElementPresentByXpath(String locator,String message) { 146 try{ 147 driver.findElement(By.xpath(locator)); 148 }catch(Exception e){ 149 Assert.fail(locator+ " not present "+ message); 150 } 151 } 152 153 protected void assertElementPresent(String locator) { 154 driver.findElement(By.cssSelector(locator)); 155 } 156 157 protected void assertTextPresent(String text) { 158 assertTextPresent(text, ""); 159 } 160 161 protected void assertTextPresent(String text, String message) { 162 if (!driver.getPageSource().contains(text)) { 163 Assert.fail(text + " not present " + message); 164 } 165 } 166 167 protected void blanketApproveTest() throws InterruptedException { 168 ITUtil.checkForIncidentReport(driver.getPageSource(), "methodToCall.blanketApprove", ""); 169 waitAndClickByName("methodToCall.blanketApprove", "No blanket approve button does the user " + getUserName() + " have permission?"); 170 Thread.sleep(2000); 171 172 if (driver.findElements(By.xpath(ITUtil.DIV_ERROR_LOCATOR)).size()>0) { 173 String errorText = driver.findElement(By.xpath(ITUtil.DIV_ERROR_LOCATOR)).getText(); 174 if (errorText != null && errorText.contains("error(s) found on page.")) { 175 errorText = ITUtil.blanketApprovalCleanUpErrorText(errorText); 176 if (driver.findElements(By.xpath(ITUtil.DIV_EXCOL_LOCATOR)).size()>0) { // not present if errors are at the bottom of the page (see left-errmsg below) 177 errorText = ITUtil.blanketApprovalCleanUpErrorText(driver.findElement(By.xpath(ITUtil.DIV_EXCOL_LOCATOR)).getText()); // replacing errorText as DIV_EXCOL_LOCATOR includes the error count 178 } 179 180 // if (selenium.isElementPresent("//div[@class='left-errmsg']/div")) { 181 // errorText = errorText + " " + selenium.getText("//div[@class='left-errmsg']/div/div[1]"); 182 // } 183 Assert.fail(errorText); 184 } 185 } 186 ITUtil.checkForIncidentReport(driver.getPageSource(), "//img[@alt='doc search']", "Blanket Approve failure"); 187 waitAndClickByXpath("//img[@alt='doc search']"); 188 assertEquals("Kuali Portal Index", driver.getTitle()); 189 selectFrame("iframeportlet"); 190 waitAndClickByXpath("//input[@name='methodToCall.search' and @value='search']"); 191 } 192 193 protected void checkForIncidentReport() { 194 checkForIncidentReport("", ""); 195 } 196 197 protected void checkForIncidentReport(String locator) { 198 checkForIncidentReport(locator, ""); 199 } 200 201 protected void checkForIncidentReport(String locator, String message) { 202 WebDriverUtil.checkForIncidentReport(driver, locator, message); 203 } 204 205 protected String getAttribute(By by, String attribute) throws InterruptedException { 206 waitFor(by); 207 return driver.findElement(by).getAttribute(attribute); 208 } 209 210 /** 211 * Get value of any attribute by using element name 212 * 213 *@param name name of an element 214 *@param attribute the name of an attribute whose value is to be retrieved 215 */ 216 protected String getAttributeByName(String name,String attribute) throws InterruptedException { 217 return getAttribute(By.name(name),attribute); 218 } 219 220 /** 221 * Get value of any attribute by using element xpath 222 * 223 *@param locator locating mechanism of an element 224 *@param attribute the name of an attribute whose value is to be retrieved 225 */ 226 protected String getAttributeByXpath(String locator,String attribute) throws InterruptedException { 227 return getAttribute(By.xpath(locator),attribute); 228 } 229 230 protected String getBaseUrlString() { 231 return ITUtil.getBaseUrlString(); 232 } 233 234 protected String getText(By by) throws InterruptedException { 235 return driver.findElement(by).getText(); 236 } 237 238 protected String getTextByName(String name) throws InterruptedException { 239 return getText(By.name(name)); 240 } 241 242 protected String getText(String locator) throws InterruptedException { 243 return getText(By.cssSelector(locator)); 244 } 245 246 protected String getTextByXpath(String locator) throws InterruptedException { 247 return getText(By.xpath(locator)); 248 } 249 250 protected String getTitle() { 251 return driver.getTitle(); 252 } 253 254 /** 255 * Override in test to define a user other than admin 256 * @return 257 */ 258 public String getUserName() { 259 return user; 260 } 261 262 protected boolean isElementPresent(By by) { 263 return (driver.findElements(by)).size()>0; 264 } 265 266 protected boolean isElementPresentByName(String name) { 267 return isElementPresent(By.name(name)); 268 } 269 270 protected boolean isElementPresentByXpath(String locator) { 271 return isElementPresent(By.xpath(locator)); 272 } 273 274 protected void open(String url) { 275 driver.get(url); 276 } 277 278 protected void selectFrame(String locator) { 279 driver.switchTo().frame(locator); 280 } 281 282 protected void selectTopFrame() { 283 driver.switchTo().defaultContent(); 284 } 285 286 protected void selectWindow(String locator) { 287 driver.switchTo().window(locator); 288 } 289 290 protected String waitForDocId() throws InterruptedException { 291 waitForElementPresentByXpath("//div[@id='headerarea']/div/table/tbody/tr[1]/td[1]"); 292 return driver.findElement(By.xpath("//div[@id='headerarea']/div/table/tbody/tr[1]/td[1]")).getText(); 293 } 294 295 protected void waitForElementPresent(String locator) throws InterruptedException { 296 waitFor(By.cssSelector(locator)); 297 } 298 299 protected void waitForElementPresentByXpath(String locator) throws InterruptedException { 300 waitFor(By.xpath(locator)); 301 } 302 303 protected void waitForElementPresentByName(String name) throws InterruptedException { 304 waitFor(By.name(name)); 305 } 306 307 protected void waitForTitleToEqualKualiPortalIndex() throws InterruptedException { 308 waitForTitleToEqualKualiPortalIndex(""); 309 } 310 311 protected void waitForTitleToEqualKualiPortalIndex(String message) throws InterruptedException { 312 boolean failed = false; 313 for (int second = 0;; second++) { 314 Thread.sleep(1000); 315 if (second >= 60) failed = true; 316 try { if (failed || ITUtil.KUALI_PORTAL_TITLE.equals(driver.getTitle())) break; } catch (Exception e) {} 317 } 318 WebDriverUtil.checkForIncidentReport(driver, message); // after timeout to be sure page is loaded 319 if (failed) fail("timeout of " + 60 + " seconds " + message); 320 } 321 322 protected void waitAndClick(String locator) throws InterruptedException { 323 waitAndClick(locator, ""); 324 } 325 326 protected void waitForPageToLoad() { 327 // noop webdriver doesn't it need it, except when it does... 328 } 329 330 protected void waitFor(By by) throws InterruptedException { 331 waitFor(by, ""); 332 } 333 334 protected void waitFor(By by, String message) throws InterruptedException { 335 // for (int second = 0;; second++) { 336 Thread.sleep(1000); 337 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 338 339 // if (second >= DEFAULT_WAIT_SEC) fail(by.toString() + " " + message + " " + DEFAULT_WAIT_SEC + " sec timeout."); 340 try { driver.findElement(by); 341 //break; 342 } catch (Exception e) {} 343 driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS); 344 // } 345 } 346 347 protected void waitAndClick(By by) throws InterruptedException { 348 waitAndClick(by, ""); 349 } 350 351 protected void waitAndClick(By by, String message) throws InterruptedException { 352 waitFor(by, message); 353 try { 354 (driver.findElement(by)).click(); 355 } catch (Exception e) { 356 fail(e.getMessage() + " " + by.toString() + " " + message); 357 e.printStackTrace(); 358 } 359 } 360 361 protected void waitAndClick(String locator, String message) throws InterruptedException { 362 waitAndClick(By.cssSelector(locator), message); 363 } 364 365 protected void waitAndClickByLinkText(String text) throws InterruptedException { 366 waitAndClick(By.linkText(text),""); 367 } 368 369 protected void waitAndClickByLinkText(String text, String message) throws InterruptedException { 370 waitAndClick(By.linkText(text), message); 371 } 372 373 protected void waitAndClickByName(String name) throws InterruptedException { 374 waitAndClick(By.name(name), ""); 375 } 376 377 protected void waitAndClickByXpath(String xpath) throws InterruptedException { 378 waitAndClick(By.xpath(xpath)); 379 } 380 381 protected void waitAndClickByName(String name, String message) throws InterruptedException { 382 waitAndClick(By.name(name), message); 383 } 384 385 protected void waitAndClickByXpath(String xpath, String message) throws InterruptedException { 386 waitAndClick(By.xpath(xpath), message); 387 } 388 389 protected void waitAndType(By by, String text) throws InterruptedException { 390 waitFor(by, ""); 391 try { 392 (driver.findElement(by)).sendKeys(text); 393 } catch (Exception e) { 394 fail(e.getMessage() + " " + by.toString() + " " + text); 395 e.printStackTrace(); 396 } 397 } 398 399 protected void waitAndType(By by, String text, String message) throws InterruptedException { 400 waitFor(by, ""); 401 try { 402 (driver.findElement(by)).sendKeys(text); 403 } catch (Exception e) { 404 fail(e.getMessage() + " " + by.toString() + " " + text + " "+message); 405 e.printStackTrace(); 406 } 407 } 408 409 protected void waitAndTypeByXpath(String locator, String text) throws InterruptedException { 410 waitAndType(By.xpath(locator), text); 411 } 412 413 protected void waitAndTypeByXpath(String locator, String text, String message) throws InterruptedException { 414 waitAndType(By.xpath(locator), text, message); 415 } 416 417 protected void waitAndTypeByName(String name, String text) throws InterruptedException { 418 waitAndType(By.name(name), text); 419 } 420 421 protected void selectByXpath(String locator, String select) throws InterruptedException { 422 select(By.xpath(locator), select); 423 } 424 425 protected void selectByName(String name, String select) throws InterruptedException { 426 select(By.name(name), select); 427 } 428 429 protected void select(By by, String select) throws InterruptedException { 430 WebElement select1 = driver.findElement(by); 431 List<WebElement> options = select1.findElements(By.tagName("option")); 432 for(WebElement option : options){ 433 if(option.getText().equals(select)){ 434 option.click(); 435 break; 436 } 437 } 438 } 439 440 protected String[] getSelectOptions(By by) throws InterruptedException { 441 WebElement select1 = driver.findElement(by); 442 List<WebElement> options = select1.findElements(By.tagName("option")); 443 String[] optionValues = new String[options.size()]; 444 int counter=0; 445 for(WebElement option : options){ 446 optionValues[counter] = option.getAttribute("value"); 447 counter++; 448 } 449 return optionValues; 450 } 451 452 protected String[] getSelectOptionsByName(String name) throws InterruptedException { 453 return getSelectOptions(By.name(name)); 454 } 455 456 protected String[] getSelectOptionsByXpath(String locator) throws InterruptedException { 457 return getSelectOptions(By.xpath(locator)); 458 } 459 460 protected int getCssCount(String selector) { 461 return getCssCount(By.cssSelector(selector)); 462 } 463 464 protected int getCssCount(By by) { 465 return (driver.findElements(by)).size(); 466 } 467 468 protected void checkErrorMessageItem(String message) 469 { 470 final String error_locator = "//li[@class='uif-errorMessageItem']"; 471 assertElementPresentByXpath(error_locator); 472 String errorText=null; 473 try { 474 errorText = getTextByXpath(error_locator); 475 } catch (InterruptedException e) { 476 e.printStackTrace(); 477 } 478 if (errorText != null && errorText.contains("errors")) { 479 Assert.fail(errorText + message); 480 } 481 482 } 483 484 protected boolean isVisibleByXpath(String locator) { 485 return isVisible(By.xpath(locator)); 486 } 487 488 protected boolean isVisible(String locator) { 489 return driver.findElement(By.cssSelector(locator)).isDisplayed(); 490 } 491 492 protected boolean isVisible(By by) { 493 return driver.findElement(by).isDisplayed(); 494 } 495 496 protected void waitNotVisibleByXpath(String locator) throws InterruptedException { 497 for (int second = 0;; second++) { 498 if (second >= 15) { 499 Assert.fail("timeout"); 500 } 501 502 if (!isVisibleByXpath(locator)) { 503 break; 504 } 505 506 Thread.sleep(1000); 507 } 508 } 509 510 protected void waitIsVisibleByXpath(String locator) throws InterruptedException { 511 for (int second = 0;; second++) { 512 if (second >= 15) { 513 Assert.fail("timeout"); 514 } 515 if (isVisibleByXpath(locator)) { 516 break; 517 } 518 Thread.sleep(1000); 519 } 520 } 521 522 protected void colapseExpandByXpath(String clickLocator, String visibleLocator) throws InterruptedException { 523 waitAndClickByXpath(clickLocator); 524 waitNotVisibleByXpath(visibleLocator); 525 526 waitAndClickByXpath(clickLocator); 527 waitIsVisibleByXpath(visibleLocator); 528 } 529 530 protected void expandColapseByXpath(String clickLocator, String visibleLocator) throws InterruptedException { 531 waitAndClickByXpath(clickLocator); 532 waitIsVisibleByXpath(visibleLocator); 533 534 waitAndClickByXpath(clickLocator); 535 waitNotVisibleByXpath(visibleLocator); 536 } 537 538 protected void fireEvent(String name, String event) { 539 ((JavascriptExecutor)driver).executeScript("document.getElementsByName('"+name+"')[0]."+event+"()"); 540 } 541 542 protected void clearTextByName(String name) throws InterruptedException { 543 clearText(By.name(name)); 544 } 545 546 protected void clearTextByXpath(String locator) throws InterruptedException { 547 clearText(By.xpath(locator)); 548 } 549 550 protected void clearText(By by) throws InterruptedException { 551 driver.findElement(by).clear(); 552 } 553 }