You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ComponentLocator.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.componentlocator;
  17. import java.util.Arrays;
  18. import java.util.List;
  19. import com.google.gwt.core.client.JavaScriptObject;
  20. import com.google.gwt.core.client.JsArray;
  21. import com.google.gwt.dom.client.Element;
  22. import com.google.gwt.user.client.DOM;
  23. import com.vaadin.client.ApplicationConnection;
  24. /**
  25. * ComponentLocator provides methods for generating a String locator for a given
  26. * DOM element and for locating a DOM element using a String locator.
  27. * <p>
  28. * The main use for this class is locating components for automated testing
  29. * purposes.
  30. *
  31. * @since 7.2, moved from {@link com.vaadin.client.ComponentLocator}
  32. */
  33. public class ComponentLocator {
  34. private final List<LocatorStrategy> locatorStrategies;
  35. /**
  36. * Reference to ApplicationConnection instance.
  37. */
  38. private final ApplicationConnection client;
  39. /**
  40. * Construct a ComponentLocator for the given ApplicationConnection.
  41. *
  42. * @param client
  43. * ApplicationConnection instance for the application.
  44. */
  45. public ComponentLocator(ApplicationConnection client) {
  46. this.client = client;
  47. locatorStrategies = Arrays.asList(
  48. new VaadinFinderLocatorStrategy(client),
  49. new LegacyLocatorStrategy(client));
  50. }
  51. /**
  52. * Generates a String locator which uniquely identifies the target element.
  53. * The {@link #getElementByPath(String)} method can be used for the inverse
  54. * operation, i.e. locating an element based on the return value from this
  55. * method.
  56. * <p>
  57. * Note that getElementByPath(getPathForElement(element)) == element is not
  58. * always true as #getPathForElement(Element) can return a path to another
  59. * element if the widget determines an action on the other element will give
  60. * the same result as the action on the target element.
  61. * </p>
  62. *
  63. * @since 5.4
  64. * @param targetElement
  65. * The element to generate a path for.
  66. * @return A String locator that identifies the target element or null if a
  67. * String locator could not be created.
  68. * @deprecated As of 7.2, call and override
  69. * {@link #getPathForElement(Element)} instead
  70. */
  71. @Deprecated
  72. public String getPathForElement(
  73. com.google.gwt.user.client.Element targetElement) {
  74. for (LocatorStrategy strategy : locatorStrategies) {
  75. String path = strategy.getPathForElement(targetElement);
  76. if (null != path) {
  77. return path;
  78. }
  79. }
  80. return null;
  81. }
  82. /**
  83. * Generates a String locator which uniquely identifies the target element.
  84. * The {@link #getElementByPath(String)} method can be used for the inverse
  85. * operation, i.e. locating an element based on the return value from this
  86. * method.
  87. * <p>
  88. * Note that getElementByPath(getPathForElement(element)) == element is not
  89. * always true as #getPathForElement(Element) can return a path to another
  90. * element if the widget determines an action on the other element will give
  91. * the same result as the action on the target element.
  92. * </p>
  93. *
  94. * @since 7.2
  95. * @param targetElement
  96. * The element to generate a path for.
  97. * @return A String locator that identifies the target element or null if a
  98. * String locator could not be created.
  99. */
  100. public String getPathForElement(Element targetElement) {
  101. if (targetElement != null) {
  102. return getPathForElement(DOM.asOld(targetElement));
  103. }
  104. return null;
  105. }
  106. /**
  107. * Locates an element using a String locator (path) which identifies a DOM
  108. * element. The {@link #getPathForElement(Element)} method can be used for
  109. * the inverse operation, i.e. generating a string expression for a DOM
  110. * element.
  111. *
  112. * @since 5.4
  113. * @param path
  114. * The String locator which identifies the target element.
  115. * @return The DOM element identified by {@code path} or null if the element
  116. * could not be located.
  117. */
  118. public com.google.gwt.user.client.Element getElementByPath(String path) {
  119. for (LocatorStrategy strategy : locatorStrategies) {
  120. if (strategy.validatePath(path)) {
  121. Element element = strategy.getElementByPath(path);
  122. if (null != element) {
  123. return DOM.asOld(element);
  124. }
  125. }
  126. }
  127. return null;
  128. }
  129. /**
  130. * Locates elements using a String locator (path) which identifies DOM
  131. * elements.
  132. *
  133. * @since 7.2
  134. * @param path
  135. * The String locator which identifies target elements.
  136. * @return The JavaScriptArray of DOM elements identified by {@code path} or
  137. * empty array if elements could not be located.
  138. */
  139. public JsArray<Element> getElementsByPath(String path) {
  140. JsArray<Element> jsElements = JavaScriptObject.createArray().cast();
  141. for (LocatorStrategy strategy : locatorStrategies) {
  142. if (strategy.validatePath(path)) {
  143. List<Element> elements = strategy.getElementsByPath(path);
  144. if (elements.size() > 0) {
  145. for (Element e : elements) {
  146. jsElements.push(e);
  147. }
  148. return jsElements;
  149. }
  150. }
  151. }
  152. return jsElements;
  153. }
  154. /**
  155. * Locates elements using a String locator (path) which identifies DOM
  156. * elements. The path starts from the specified root element.
  157. *
  158. * @see #getElementByPath(String)
  159. *
  160. * @since 7.2
  161. * @param path
  162. * The path of elements to be found
  163. * @param root
  164. * The root element where the path is anchored
  165. * @return The JavaScriptArray of DOM elements identified by {@code path} or
  166. * empty array if elements could not be located.
  167. */
  168. public JsArray<Element> getElementsByPathStartingAt(String path,
  169. Element root) {
  170. JsArray<Element> jsElements = JavaScriptObject.createArray().cast();
  171. for (LocatorStrategy strategy : locatorStrategies) {
  172. if (strategy.validatePath(path)) {
  173. List<Element> elements = strategy
  174. .getElementsByPathStartingAt(path, root);
  175. if (elements.size() > 0) {
  176. for (Element e : elements) {
  177. jsElements.push(e);
  178. }
  179. return jsElements;
  180. }
  181. }
  182. }
  183. return jsElements;
  184. }
  185. /**
  186. * Locates an element using a String locator (path) which identifies a DOM
  187. * element. The path starts from the specified root element.
  188. *
  189. * @see #getElementByPath(String)
  190. *
  191. * @since 7.2
  192. *
  193. * @param path
  194. * The path of the element to be found
  195. * @param root
  196. * The root element where the path is anchored
  197. * @return The DOM element identified by {@code path} or null if the element
  198. * could not be located.
  199. */
  200. public com.google.gwt.user.client.Element getElementByPathStartingAt(
  201. String path, Element root) {
  202. for (LocatorStrategy strategy : locatorStrategies) {
  203. if (strategy.validatePath(path)) {
  204. Element element = strategy.getElementByPathStartingAt(path,
  205. root);
  206. if (null != element) {
  207. return DOM.asOld(element);
  208. }
  209. }
  210. }
  211. return null;
  212. }
  213. /**
  214. * Returns the {@link ApplicationConnection} used by this locator.
  215. * <p>
  216. * This method is primarily for internal use by the framework.
  217. *
  218. * @return the application connection
  219. */
  220. public ApplicationConnection getClient() {
  221. return client;
  222. }
  223. /**
  224. * Check if a given selector is valid for LegacyLocatorStrategy.
  225. *
  226. * @param path
  227. * Vaadin selector path
  228. * @return true if passes path validation with LegacyLocatorStrategy
  229. */
  230. public boolean isValidForLegacyLocator(String path) {
  231. for (LocatorStrategy ls : locatorStrategies) {
  232. if (ls instanceof LegacyLocatorStrategy) {
  233. return ls.validatePath(path);
  234. }
  235. }
  236. return false;
  237. }
  238. }