001 /** 002 * Copyright 2005-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 org.junit.After; 019 import org.junit.Assert; 020 import org.junit.Before; 021 import org.junit.BeforeClass; 022 import org.junit.Rule; 023 import org.junit.rules.TestName; 024 import org.openqa.selenium.By; 025 import org.openqa.selenium.JavascriptExecutor; 026 import org.openqa.selenium.NoSuchFrameException; 027 import org.openqa.selenium.WebDriver; 028 import org.openqa.selenium.WebElement; 029 import org.openqa.selenium.chrome.ChromeDriverService; 030 import org.openqa.selenium.interactions.Actions; 031 import org.openqa.selenium.remote.RemoteWebDriver; 032 033 import java.io.BufferedReader; 034 import java.io.InputStreamReader; 035 import java.net.HttpURLConnection; 036 import java.net.URL; 037 import java.util.List; 038 import java.util.Set; 039 import java.util.concurrent.TimeUnit; 040 041 import static com.thoughtworks.selenium.SeleneseTestBase.fail; 042 import static org.junit.Assert.assertEquals; 043 044 /** 045 * Class to upgrade UpgradedSeleniumITBase tests to WebDriver. 046 * @deprecated Use WebDriverITBase for new tests. 047 * @author Kuali Rice Team (rice.collab@kuali.org) 048 */ 049 public abstract class WebDriverLegacyITBase { //implements com.saucelabs.common.SauceOnDemandSessionIdProvider { 050 051 public static final int DEFAULT_WAIT_SEC = 60; 052 public static final String REMOTE_PUBLIC_USERPOOL_PROPERTY = "remote.public.userpool"; 053 public static final String REMOTE_PUBLIC_USER_PROPERTY = "remote.public.user"; 054 055 public abstract String getTestUrl(); 056 057 protected WebDriver driver; 058 protected String user = "admin"; 059 protected boolean passed = false; 060 static ChromeDriverService chromeDriverService; 061 062 public @Rule TestName testName= new TestName(); 063 064 String sessionId = null; 065 066 public String getSessionId() { 067 return sessionId; 068 } 069 070 @BeforeClass 071 public static void createAndStartService() throws Exception { 072 chromeDriverService = WebDriverUtil.createAndStartService(); 073 if (chromeDriverService != null) chromeDriverService.start(); 074 } 075 076 /** 077 * Setup the WebDriver test, login and load the tested web page 078 * 079 * @throws Exception 080 */ 081 @Before 082 public void setUp() throws Exception { 083 // {"test":"1","user":"1"} 084 try { 085 if (System.getProperty(REMOTE_PUBLIC_USER_PROPERTY) != null) { 086 user = System.getProperty(REMOTE_PUBLIC_USER_PROPERTY); 087 } else if (System.getProperty(REMOTE_PUBLIC_USERPOOL_PROPERTY) != null) { // deprecated 088 String userResponse = getHTML(ITUtil.prettyHttp(System.getProperty(REMOTE_PUBLIC_USERPOOL_PROPERTY) + "?test=" + this.toString().trim())); 089 user = userResponse.substring(userResponse.lastIndexOf(":" ) + 2, userResponse.lastIndexOf("\"")); 090 } 091 driver = WebDriverUtil.setUp(getUserName(), ITUtil.getBaseUrlString() + getTestUrl(), getClass().getSimpleName(), testName); 092 this.sessionId = ((RemoteWebDriver)driver).getSessionId().toString(); 093 } catch (Exception e) { 094 fail("Exception in setUp " + e.getMessage()); 095 e.printStackTrace(); 096 } 097 ITUtil.login(driver, user); 098 } 099 100 @After 101 public void tearDown() throws Exception { 102 try { 103 // if (System.getProperty(SauceLabsWebDriverHelper.SAUCE_PROPERTY) != null) { 104 // SauceLabsWebDriverHelper.tearDown(passed, sessionId, System.getProperty(SauceLabsWebDriverHelper.SAUCE_USER_PROPERTY), System.getProperty(SauceLabsWebDriverHelper.SAUCE_KEY_PROPERTY)); 105 // } 106 if (System.getProperty(REMOTE_PUBLIC_USERPOOL_PROPERTY) != null) { 107 getHTML(ITUtil.prettyHttp(System.getProperty(REMOTE_PUBLIC_USERPOOL_PROPERTY) + "?test=" + this.toString() + "&user=" + user)); 108 } 109 } catch (Exception e) { 110 System.out.println("Exception in tearDown " + e.getMessage()); 111 e.printStackTrace(); 112 } finally { 113 if (driver != null) { 114 if (ITUtil.dontTearDownPropertyNotSet()) { 115 driver.close(); 116 driver.quit(); 117 } 118 } else { 119 System.out.println("WebDriver is null, if using saucelabs, has sauceleabs been uncommented in WebDriverUtil.java? If using a remote hub did you include the port?"); 120 } 121 } 122 } 123 124 protected String getHTML(String urlToRead) { 125 URL url; 126 HttpURLConnection conn; 127 BufferedReader rd; 128 String line; 129 String result = ""; 130 try { 131 url = new URL(urlToRead); 132 conn = (HttpURLConnection) url.openConnection(); 133 conn.setRequestMethod("GET"); 134 rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 135 while ((line = rd.readLine()) != null) { 136 result += line; 137 } 138 rd.close(); 139 } catch (Exception e) { 140 e.printStackTrace(); 141 } 142 return result; 143 } 144 145 protected void passed() { 146 passed = true; 147 } 148 149 protected void assertElementPresentByName(String name) { 150 driver.findElement(By.name(name)); 151 } 152 153 protected void assertElementPresentByName(String name,String message) { 154 try{ 155 driver.findElement(By.name(name)); 156 }catch(Exception e){ 157 Assert.fail(name+ " not present "+ message); 158 } 159 } 160 161 protected void assertElementPresentByXpath(String locator) { 162 driver.findElement(By.xpath(locator)); 163 } 164 165 protected void assertElementPresentByXpath(String locator,String message) { 166 try{ 167 driver.findElement(By.xpath(locator)); 168 }catch(Exception e){ 169 Assert.fail(locator+ " not present "+ message); 170 } 171 } 172 173 protected void assertElementPresentByLinkText(String linkText) { 174 driver.findElement(By.linkText(linkText)); 175 } 176 177 protected void assertElementPresent(String locator) { 178 driver.findElement(By.cssSelector(locator)); 179 } 180 181 protected void assertTextPresent(String text) { 182 assertTextPresent(text, ""); 183 } 184 185 protected void assertTextPresent(String text, String message) { 186 if (!driver.getPageSource().contains(text)) { 187 Assert.fail(text + " not present " + message); 188 } 189 } 190 191 protected void blanketApproveTest() throws InterruptedException { 192 ITUtil.checkForIncidentReport(driver.getPageSource(), "methodToCall.blanketApprove", ""); 193 waitAndClickByName("methodToCall.blanketApprove", "No blanket approve button does the user " + getUserName() + " have permission?"); 194 Thread.sleep(2000); 195 196 if (driver.findElements(By.xpath(ITUtil.DIV_ERROR_LOCATOR)).size()>0) { 197 String errorText = driver.findElement(By.xpath(ITUtil.DIV_ERROR_LOCATOR)).getText(); 198 if (errorText != null && errorText.contains("error(s) found on page.")) { 199 errorText = ITUtil.blanketApprovalCleanUpErrorText(errorText); 200 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) 201 errorText = ITUtil.blanketApprovalCleanUpErrorText(driver.findElement(By.xpath(ITUtil.DIV_EXCOL_LOCATOR)).getText()); // replacing errorText as DIV_EXCOL_LOCATOR includes the error count 202 } 203 204 // if (selenium.isElementPresent("//div[@class='left-errmsg']/div")) { 205 // errorText = errorText + " " + selenium.getText("//div[@class='left-errmsg']/div/div[1]"); 206 // } 207 Assert.fail(errorText); 208 } 209 } 210 ITUtil.checkForIncidentReport(driver.getPageSource(), "//img[@alt='doc search']", "Blanket Approve failure"); 211 waitAndClickByXpath("//img[@alt='doc search']"); 212 assertEquals("Kuali Portal Index", driver.getTitle()); 213 selectFrame("iframeportlet"); 214 waitAndClickByXpath("//input[@name='methodToCall.search' and @value='search']"); 215 } 216 217 protected void checkForIncidentReport() { 218 checkForIncidentReport("", ""); 219 } 220 221 protected void checkForIncidentReport(String locator) { 222 checkForIncidentReport(locator, ""); 223 } 224 225 protected void checkForIncidentReport(String locator, String message) { 226 WebDriverUtil.checkForIncidentReport(driver, locator, message); 227 } 228 229 protected void clearText(By by) throws InterruptedException { 230 driver.findElement(by).clear(); 231 } 232 233 protected void clearText(String selector) throws InterruptedException { 234 clearText(By.cssSelector(selector)); 235 } 236 237 protected void clearTextByName(String name) throws InterruptedException { 238 clearText(By.name(name)); 239 } 240 241 protected void clearTextByXpath(String locator) throws InterruptedException { 242 clearText(By.xpath(locator)); 243 } 244 245 protected String getAttribute(By by, String attribute) throws InterruptedException { 246 waitFor(by); 247 return driver.findElement(by).getAttribute(attribute); 248 } 249 250 /** 251 * Get value of any attribute by using element name 252 * 253 *@param name name of an element 254 *@param attribute the name of an attribute whose value is to be retrieved 255 */ 256 protected String getAttributeByName(String name,String attribute) throws InterruptedException { 257 return getAttribute(By.name(name),attribute); 258 } 259 260 /** 261 * Get value of any attribute by using element xpath 262 * 263 *@param locator locating mechanism of an element 264 *@param attribute the name of an attribute whose value is to be retrieved 265 */ 266 protected String getAttributeByXpath(String locator,String attribute) throws InterruptedException { 267 return getAttribute(By.xpath(locator),attribute); 268 } 269 270 protected String getBaseUrlString() { 271 return ITUtil.getBaseUrlString(); 272 } 273 274 protected String getText(By by) throws InterruptedException { 275 return driver.findElement(by).getText(); 276 } 277 278 protected String getTextByName(String name) throws InterruptedException { 279 return getText(By.name(name)); 280 } 281 282 protected String getText(String locator) throws InterruptedException { 283 return getText(By.cssSelector(locator)); 284 } 285 286 protected String getTextByXpath(String locator) throws InterruptedException { 287 return getText(By.xpath(locator)); 288 } 289 290 protected String getTitle() { 291 return driver.getTitle(); 292 } 293 294 /** 295 * Override in test to define a user other than admin 296 * @return 297 */ 298 public String getUserName() { 299 return user; 300 } 301 302 /** 303 * Handles simple nested frame content; validates that a frame and nested frame exists before switching to it 304 */ 305 protected void gotoNestedFrame() { 306 driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS); 307 driver.switchTo().defaultContent(); 308 if(driver.findElements(By.xpath("//iframe")).size() > 0) { 309 WebElement containerFrame = driver.findElement(By.xpath("//iframe")); 310 driver.switchTo().frame(containerFrame); 311 } 312 if(driver.findElements(By.xpath("//iframe")).size() > 0) { 313 WebElement contentFrame = driver.findElement(By.xpath("//iframe")); 314 driver.switchTo().frame(contentFrame); 315 } 316 driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_SEC, TimeUnit.SECONDS); 317 } 318 319 protected boolean isElementPresent(By by) { 320 return (driver.findElements(by)).size()>0; 321 } 322 323 protected boolean isElementPresent(String locator) { 324 return (driver.findElements(By.cssSelector(locator))).size()>0; 325 } 326 327 protected boolean isElementPresentByName(String name) { 328 return isElementPresent(By.name(name)); 329 } 330 331 protected boolean isElementPresentByXpath(String locator) { 332 return isElementPresent(By.xpath(locator)); 333 } 334 335 protected boolean isElementPresentByLinkText(String locator) { 336 return isElementPresent(By.linkText(locator)); 337 } 338 339 protected void open(String url) { 340 driver.get(url); 341 } 342 343 protected void selectFrame(String locator) { 344 if ("iframeportlet".equals(locator)) { 345 gotoNestedFrame(); 346 } else { 347 try { 348 driver.switchTo().frame(locator); 349 } catch (NoSuchFrameException nsfe) { 350 // don't fail 351 } 352 } 353 } 354 355 protected void selectTopFrame() { 356 driver.switchTo().defaultContent(); 357 } 358 359 protected void selectWindow(String locator) { 360 driver.switchTo().window(locator); 361 } 362 363 protected void close() { 364 driver.close(); 365 } 366 367 protected void testCancelConfirmation() throws InterruptedException { 368 waitAndCancelConfirmation(); 369 passed(); 370 } 371 372 protected void testCreateNewSearchReturnValueCancelConfirmation() throws InterruptedException, Exception { 373 selectFrame("iframeportlet"); 374 waitAndCreateNew(); 375 waitAndSearch(); 376 waitAndReturnValue(); 377 waitAndCancelConfirmation(); 378 passed(); 379 } 380 381 protected void testSearchEditCancel() throws InterruptedException { 382 selectFrame("iframeportlet"); 383 waitAndSearch(); 384 waitAndEdit(); 385 testCancelConfirmation(); 386 } 387 388 protected void testVerifyAddDeleteFiscalOfficerLegacy() throws Exception { 389 selectFrame("iframeportlet"); 390 waitAndTypeByName("document.documentHeader.documentDescription", ITUtil.DTS_TWO); 391 waitAndTypeByName("newCollectionLines['document.newMaintainableObject.dataObject.fiscalOfficer.accounts'].number", "1234567890"); 392 waitAndTypeByName("newCollectionLines['document.newMaintainableObject.dataObject.fiscalOfficer.accounts'].foId", "2"); 393 394 waitAndClickByXpath("//button[@data-loadingmessage='Adding Line...']"); 395 396 assertElementPresentByName("document.newMaintainableObject.dataObject.fiscalOfficer.accounts[0].number", "https://jira.kuali.org/browse/KULRICE-8564"); 397 398 assertEquals("1234567890", getAttributeByName("document.newMaintainableObject.dataObject.fiscalOfficer.accounts[0].number","value")); 399 assertEquals("2", getAttributeByName("document.newMaintainableObject.dataObject.fiscalOfficer.accounts[0].foId","value")); 400 401 waitAndClickByXpath("//button[@data-loadingmessage='Deleting Line...']"); 402 403 assertElementPresentByName("document.newMaintainableObject.dataObject.fiscalOfficer.accounts[0].number"); 404 passed(); 405 } 406 407 protected void waitAndCancelConfirmation() throws InterruptedException { 408 waitAndClickByName("methodToCall.cancel"); 409 waitAndClickByName("methodToCall.processAnswer.button0"); 410 } 411 412 protected void waitAndCreateNew() throws InterruptedException { 413 waitAndClickByXpath("//img[@alt='create new']"); 414 // waitAndClickByXpath("//a[@title='Create a new record']"); 415 } 416 417 protected void waitAndEdit() throws InterruptedException { 418 waitAndClickByLinkText("edit"); 419 } 420 421 protected void waitAndReturnValue() throws InterruptedException { 422 waitAndClickByLinkText("return value"); 423 } 424 425 protected void waitAndSearch() throws InterruptedException { 426 waitAndClickByXpath("//input[@value='search']"); 427 // waitAndClickByXpath("//input[@name='methodToCall.search']"); 428 // waitAndClick("input[alt='search']"); 429 // waitAndClickByXpath("//input[@name='methodToCall.search' and @value='search']"); 430 } 431 432 protected String waitForDocId() throws InterruptedException { 433 waitForElementPresentByXpath("//div[@id='headerarea']/div/table/tbody/tr[1]/td[1]"); 434 return driver.findElement(By.xpath("//div[@id='headerarea']/div/table/tbody/tr[1]/td[1]")).getText(); 435 } 436 437 protected void waitForElementPresent(String locator) throws InterruptedException { 438 waitFor(By.cssSelector(locator)); 439 } 440 441 protected void waitForElementPresentByXpath(String locator) throws InterruptedException { 442 waitFor(By.xpath(locator)); 443 } 444 445 protected void waitForElementPresentByName(String name) throws InterruptedException { 446 waitFor(By.name(name)); 447 } 448 449 protected void waitForTitleToEqualKualiPortalIndex() throws InterruptedException { 450 waitForTitleToEqualKualiPortalIndex(""); 451 } 452 453 protected void waitForTitleToEqualKualiPortalIndex(String message) throws InterruptedException { 454 Thread.sleep(2000); 455 // This started failing in CI.... 456 // boolean failed = false; 457 // for (int second = 0;; second++) { 458 // Thread.sleep(1000); 459 // if (second >= 60) failed = true; 460 // try { if (failed || ITUtil.KUALI_PORTAL_TITLE.equals(driver.getTitle())) break; } catch (Exception e) {} 461 // } 462 // WebDriverUtil.checkForIncidentReport(driver, message); // after timeout to be sure page is loaded 463 // if (failed) fail("timeout of " + 60 + " seconds " + message); 464 } 465 466 protected void waitAndClick(String locator) throws InterruptedException { 467 waitAndClick(locator, ""); 468 } 469 470 protected void waitForPageToLoad() { 471 // noop webdriver doesn't it need it, except when it does... 472 } 473 474 protected void waitFor(By by) throws InterruptedException { 475 waitFor(by, ""); 476 } 477 478 protected void waitFor(By by, String message) throws InterruptedException { 479 // for (int second = 0;; second++) { 480 Thread.sleep(1000); 481 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 482 483 // if (second >= DEFAULT_WAIT_SEC) fail(by.toString() + " " + message + " " + DEFAULT_WAIT_SEC + " sec timeout."); 484 try { driver.findElement(by); 485 //break; 486 } catch (Exception e) {} 487 driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS); 488 // } 489 } 490 491 protected void waitAndClick(By by) throws InterruptedException { 492 waitAndClick(by, ""); 493 } 494 495 protected void waitAndClick(By by, String message) throws InterruptedException { 496 waitFor(by, message); 497 try { 498 (driver.findElement(by)).click(); 499 } catch (Exception e) { 500 fail(e.getMessage() + " " + by.toString() + " " + message + " " + driver.getCurrentUrl() ); 501 e.printStackTrace(); 502 } 503 } 504 505 protected void waitAndClick(String locator, String message) throws InterruptedException { 506 waitAndClick(By.cssSelector(locator), message); 507 } 508 509 protected void waitAndClickByLinkText(String text) throws InterruptedException { 510 waitAndClick(By.linkText(text),""); 511 } 512 513 protected void waitAndClickByLinkText(String text, String message) throws InterruptedException { 514 waitAndClick(By.linkText(text), message); 515 } 516 517 protected void waitAndClickByName(String name) throws InterruptedException { 518 waitAndClick(By.name(name), ""); 519 } 520 521 protected void waitAndClickByXpath(String xpath) throws InterruptedException { 522 waitAndClick(By.xpath(xpath)); 523 } 524 525 protected void waitAndClickByName(String name, String message) throws InterruptedException { 526 waitAndClick(By.name(name), message); 527 } 528 529 protected void waitAndClickByXpath(String xpath, String message) throws InterruptedException { 530 waitAndClick(By.xpath(xpath), message); 531 } 532 533 protected void waitAndType(By by, String text) throws InterruptedException { 534 waitFor(by, ""); 535 try { 536 (driver.findElement(by)).sendKeys(text); 537 } catch (Exception e) { 538 fail(e.getMessage() + " " + by.toString() + " unable to type text '" + text + "' current url " + driver.getCurrentUrl() 539 + "\n" + ITUtil.deLinespace(driver.getPageSource()) 540 ); 541 e.printStackTrace(); 542 } 543 } 544 545 protected void waitAndType(By by, String text, String message) throws InterruptedException { 546 waitFor(by, ""); 547 try { 548 (driver.findElement(by)).sendKeys(text); 549 } catch (Exception e) { 550 fail(e.getMessage() + " " + by.toString() + " unable to type text '" + text + "' " + message + " current url " + driver.getCurrentUrl() 551 + "\n" + ITUtil.deLinespace(driver.getPageSource()) 552 ); 553 e.printStackTrace(); 554 } 555 } 556 557 protected void waitAndType(String selector, String text) throws InterruptedException { 558 waitAndType(By.cssSelector(selector), text); 559 } 560 561 protected void waitAndTypeByXpath(String locator, String text) throws InterruptedException { 562 waitAndType(By.xpath(locator), text); 563 } 564 565 protected void waitAndTypeByXpath(String locator, String text, String message) throws InterruptedException { 566 waitAndType(By.xpath(locator), text, message); 567 } 568 569 protected void waitAndTypeByName(String name, String text) throws InterruptedException { 570 waitAndType(By.name(name), text); 571 } 572 573 protected void selectByXpath(String locator, String selectText) throws InterruptedException { 574 select(By.xpath(locator), selectText); 575 } 576 577 protected void selectByName(String name, String selectText) throws InterruptedException { 578 select(By.name(name), selectText); 579 } 580 581 protected void select(By by, String selectText) throws InterruptedException { 582 WebElement select1 = driver.findElement(by); 583 List<WebElement> options = select1.findElements(By.tagName("option")); 584 for(WebElement option : options){ 585 if(option.getText().equals(selectText)){ 586 option.click(); 587 break; 588 } 589 } 590 } 591 592 protected void selectOptionByName(String name, String optionValue) throws InterruptedException { 593 selectOption(By.name(name), optionValue); 594 } 595 596 protected void selectOptionByXpath(String locator, String optionValue) throws InterruptedException { 597 selectOption(By.name(locator), optionValue); 598 } 599 600 protected void selectOption(By by, String optionValue) throws InterruptedException { 601 WebElement select1 = driver.findElement(by); 602 List<WebElement> options = select1.findElements(By.tagName("option")); 603 for(WebElement option : options){ 604 if(option.getAttribute("value").equals(optionValue)){ 605 option.click(); 606 break; 607 } 608 } 609 } 610 611 protected String[] getSelectOptions(By by) throws InterruptedException { 612 WebElement select1 = driver.findElement(by); 613 List<WebElement> options = select1.findElements(By.tagName("option")); 614 String[] optionValues = new String[options.size()]; 615 int counter=0; 616 for(WebElement option : options){ 617 optionValues[counter] = option.getAttribute("value"); 618 counter++; 619 } 620 return optionValues; 621 } 622 623 protected String[] getSelectOptionsByName(String name) throws InterruptedException { 624 return getSelectOptions(By.name(name)); 625 } 626 627 protected String[] getSelectOptionsByXpath(String locator) throws InterruptedException { 628 return getSelectOptions(By.xpath(locator)); 629 } 630 631 protected int getCssCount(String selector) { 632 return getCssCount(By.cssSelector(selector)); 633 } 634 635 protected int getCssCount(By by) { 636 return (driver.findElements(by)).size(); 637 } 638 639 protected void checkErrorMessageItem(String message) 640 { 641 final String error_locator = "//li[@class='uif-errorMessageItem']"; 642 assertElementPresentByXpath(error_locator); 643 String errorText=null; 644 try { 645 errorText = getTextByXpath(error_locator); 646 } catch (InterruptedException e) { 647 e.printStackTrace(); 648 } 649 if (errorText != null && errorText.contains("errors")) { 650 Assert.fail(errorText + message); 651 } 652 653 } 654 655 protected boolean isVisible(String locator) { 656 return driver.findElement(By.cssSelector(locator)).isDisplayed(); 657 } 658 659 protected boolean isVisible(By by) { 660 return driver.findElement(by).isDisplayed(); 661 } 662 663 protected boolean isVisibleByXpath(String locator) { 664 return isVisible(By.xpath(locator)); 665 } 666 667 protected void waitNotVisible(By by) throws InterruptedException { 668 for (int second = 0;; second++) { 669 if (second >= 60) { 670 Assert.fail("timeout"); 671 } 672 673 if (!isVisible(by)) { 674 break; 675 } 676 677 Thread.sleep(1000); 678 } 679 } 680 681 protected void waitNotVisibleByXpath(String locator) throws InterruptedException { 682 waitNotVisible(By.xpath(locator)); 683 } 684 685 protected void waitIsVisible(By by) throws InterruptedException { 686 for (int second = 0;; second++) { 687 if (second >= 60) { 688 Assert.fail("timeout"); 689 } 690 if (isVisible(by)) { 691 break; 692 } 693 Thread.sleep(1000); 694 } 695 } 696 697 protected void waitForElementVisible(String elementLocator, String message) throws InterruptedException { 698 boolean failed = false; 699 for (int second = 0;; second++) { 700 if (second >= 60) failed = true; 701 try { if (failed || (driver.findElements(By.cssSelector(elementLocator))).size()>0) break; } catch (Exception e) {} 702 Thread.sleep(1000); 703 } 704 checkForIncidentReport(elementLocator); // after timeout to be sure page is loaded 705 if (failed) fail("timeout of 60 seconds waiting for " + elementLocator + " " + message + " " + driver.getCurrentUrl()); 706 } 707 708 protected void waitIsVisible(String locator) throws InterruptedException { 709 waitIsVisible(By.cssSelector(locator)); 710 } 711 712 protected void waitIsVisibleByXpath(String locator) throws InterruptedException { 713 waitIsVisible(By.xpath(locator)); 714 } 715 716 protected void colapseExpandByXpath(String clickLocator, String visibleLocator) throws InterruptedException { 717 waitAndClickByXpath(clickLocator); 718 waitNotVisibleByXpath(visibleLocator); 719 720 waitAndClickByXpath(clickLocator); 721 waitIsVisibleByXpath(visibleLocator); 722 } 723 724 protected void expandColapseByXpath(String clickLocator, String visibleLocator) throws InterruptedException { 725 waitAndClickByXpath(clickLocator); 726 waitIsVisibleByXpath(visibleLocator); 727 728 waitAndClickByXpath(clickLocator); 729 waitNotVisibleByXpath(visibleLocator); 730 } 731 732 public void switchToWindow(String title) { 733 Set<String> windows = driver.getWindowHandles(); 734 735 for (String window : windows) { 736 driver.switchTo().window(window); 737 if (driver.getTitle().contains(title)) { 738 return; 739 } 740 } 741 } 742 743 public String [] getAllWindowTitles() { 744 return (String[]) driver.getWindowHandles().toArray(); 745 } 746 747 748 protected void check(By by) throws InterruptedException { 749 WebElement element =driver.findElement(by); 750 if(!element.isSelected()){ 751 element.click(); 752 } 753 } 754 755 protected void checkByName(String name) throws InterruptedException { 756 check(By.name(name)); 757 } 758 759 protected void checkByXpath(String locator) throws InterruptedException { 760 check(By.xpath(locator)); 761 } 762 763 protected void uncheck(By by) throws InterruptedException { 764 WebElement element =driver.findElement(by); 765 if(element.isSelected()){ 766 element.click(); 767 } 768 } 769 770 protected void uncheckByName(String name) throws InterruptedException { 771 uncheck(By.name(name)); 772 } 773 774 protected void uncheckByXpath(String locator) throws InterruptedException { 775 uncheck(By.xpath(locator)); 776 } 777 778 protected void fireEvent(String name, String event) { 779 ((JavascriptExecutor)driver).executeScript( 780 "var elements=document.getElementsByName(\""+name+"\");"+ 781 "for (var i = 0; i < elements.length; i++){"+ 782 "elements[i]."+event+"();}" 783 ); 784 } 785 786 protected void fireEvent(String name, String value, String event) { 787 ((JavascriptExecutor)driver).executeScript( 788 "var elements=document.getElementsByName(\""+name+"\");"+ 789 "for (var i = 0; i < elements.length; i++){"+ 790 "if(elements[i].value=='"+value+"')"+ 791 "elements[i]."+event+"();}" 792 ); 793 } 794 795 public void fireMouseOverEventByName(String name) { 796 this.fireMouseOverEvent(By.name(name)); 797 } 798 799 public void fireMouseOverEventByXpath(String locator) { 800 this.fireMouseOverEvent(By.xpath(locator)); 801 } 802 803 public void fireMouseOverEvent(By by) { 804 Actions builder = new Actions(driver); 805 Actions hover = builder.moveToElement(driver.findElement(by)); 806 hover.perform(); 807 808 } 809 }