/server/src/webtest/java/org/neo4j/server/webdriver/WebdriverLibrary.java

https://github.com/boyaxxx/community · Java · 114 lines · 75 code · 20 blank · 19 comment · 0 complexity · 4dcfdfea80588a889b257796ad547676 MD5 · raw file

  1. /**
  2. * Copyright (c) 2002-2011 "Neo Technology,"
  3. * Network Engine for Objects in Lund AB [http://neotechnology.com]
  4. *
  5. * This file is part of Neo4j.
  6. *
  7. * Neo4j is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package org.neo4j.server.webdriver;
  21. import static org.hamcrest.Matchers.not;
  22. import static org.junit.Assert.fail;
  23. import static org.neo4j.server.webdriver.BrowserTitleIs.browserTitleIs;
  24. import static org.neo4j.server.webdriver.BrowserUrlIs.browserUrlIs;
  25. import static org.neo4j.server.webdriver.ElementVisible.elementVisible;
  26. import java.lang.reflect.InvocationTargetException;
  27. import org.hamcrest.Matcher;
  28. import org.openqa.selenium.By;
  29. import org.openqa.selenium.Keys;
  30. import org.openqa.selenium.TimeoutException;
  31. import org.openqa.selenium.WebDriver;
  32. import org.openqa.selenium.WebElement;
  33. public class WebdriverLibrary
  34. {
  35. protected final WebDriver d;
  36. public WebdriverLibrary(WebDriverFacade df) throws InvocationTargetException, IllegalAccessException, InstantiationException {
  37. d = df.getWebDriver();
  38. }
  39. public WebDriver getWebDriver() {
  40. return d;
  41. }
  42. public void clickOn(String text) {
  43. clickOnByXpath("//*[contains(.,'"+text+"')]");
  44. }
  45. public void clickOnButton(String text) {
  46. clickOnByXpath("//div[contains(.,'"+text+"') and contains(@class, 'button')]");
  47. }
  48. public void clickOnLink(String text) {
  49. clickOnByXpath( "//a[contains(.,'"+text+"')]");
  50. }
  51. public void clickOnByXpath( String xpath )
  52. {
  53. getElement( By.xpath( xpath ) ).click();
  54. }
  55. public void waitForUrlToBe(String url) {
  56. waitUntil( browserUrlIs( url ), "Url did not change to expected value within a reasonable time." );
  57. }
  58. public void waitForTitleToBe(String title) {
  59. waitUntil( browserTitleIs( title ), "Title did not change to expected value within a reasonable time." );
  60. }
  61. public void waitForElementToAppear(By by) {
  62. waitUntil( elementVisible( by ), "Element ("+by.toString()+") did not appear within a reasonable time." );
  63. }
  64. public void waitForElementToDisappear(By by) {
  65. waitUntil( not( elementVisible( by )), "Element did not disappear within a reasonable time." );
  66. }
  67. public void clearInput(ElementReference el) {
  68. int len = el.getValue().length();
  69. while(len-- >= 0) {
  70. el.sendKeys( Keys.BACK_SPACE );
  71. }
  72. }
  73. public void waitUntil(Matcher<WebDriver> matcher, String errorMessage) {
  74. waitUntil( matcher, errorMessage, 10000);
  75. }
  76. public void waitUntil(Matcher<WebDriver> matcher, String errorMessage, long timeout) {
  77. try {
  78. Condition<WebDriver> cond = new WebdriverCondition<WebDriver>( getWebDriver(), matcher, d);
  79. cond.waitUntilFulfilled(timeout, errorMessage);
  80. } catch( TimeoutException e) {
  81. fail(errorMessage);
  82. }
  83. }
  84. public ElementReference getElement(By by) {
  85. return new ElementReference(this, by);
  86. }
  87. public WebElement getWebElement(By by) {
  88. waitForElementToAppear(by);
  89. return d.findElement( by );
  90. }
  91. public void refresh() {
  92. d.get( d.getCurrentUrl() );
  93. }
  94. }