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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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(new VaadinFinderLocatorStrategy(
  48. client), new LegacyLocatorStrategy(client));
  49. }
  50. /**
  51. * Generates a String locator which uniquely identifies the target element.
  52. * The {@link #getElementByPath(String)} method can be used for the inverse
  53. * operation, i.e. locating an element based on the return value from this
  54. * method.
  55. * <p>
  56. * Note that getElementByPath(getPathForElement(element)) == element is not
  57. * always true as #getPathForElement(Element) can return a path to another
  58. * element if the widget determines an action on the other element will give
  59. * the same result as the action on the target element.
  60. * </p>
  61. *
  62. * @since 5.4
  63. * @param targetElement
  64. * The element to generate a path for.
  65. * @return A String locator that identifies the target element or null if a
  66. * String locator could not be created.
  67. * @deprecated As of 7.2, call and override
  68. * {@link #getPathForElement(Element)} instead
  69. */
  70. @Deprecated
  71. public String getPathForElement(
  72. com.google.gwt.user.client.Element targetElement) {
  73. for (LocatorStrategy strategy : locatorStrategies) {
  74. String path = strategy.getPathForElement(targetElement);
  75. if (null != path) {
  76. return path;
  77. }
  78. }
  79. return null;
  80. }
  81. /**
  82. * Generates a String locator which uniquely identifies the target element.
  83. * The {@link #getElementByPath(String)} method can be used for the inverse
  84. * operation, i.e. locating an element based on the return value from this
  85. * method.
  86. * <p>
  87. * Note that getElementByPath(getPathForElement(element)) == element is not
  88. * always true as #getPathForElement(Element) can return a path to another
  89. * element if the widget determines an action on the other element will give
  90. * the same result as the action on the target element.
  91. * </p>
  92. *
  93. * @since 7.2
  94. * @param targetElement
  95. * The element to generate a path for.
  96. * @return A String locator that identifies the target element or null if a
  97. * String locator could not be created.
  98. */
  99. public String getPathForElement(Element targetElement) {
  100. if (targetElement != null) {
  101. return getPathForElement(DOM.asOld(targetElement));
  102. }
  103. return null;
  104. }
  105. /**
  106. * Locates an element using a String locator (path) which identifies a DOM
  107. * element. The {@link #getPathForElement(Element)} method can be used for
  108. * the inverse operation, i.e. generating a string expression for a DOM
  109. * element.
  110. *
  111. * @since 5.4
  112. * @param path
  113. * The String locator which identifies the target element.
  114. * @return The DOM element identified by {@code path} or null if the element
  115. * could not be located.
  116. */
  117. public com.google.gwt.user.client.Element getElementByPath(String path) {
  118. for (LocatorStrategy strategy : locatorStrategies) {
  119. if (strategy.validatePath(path)) {
  120. Element element = strategy.getElementByPath(path);
  121. if (null != element) {
  122. return DOM.asOld(element);
  123. }
  124. }
  125. }
  126. return null;
  127. }
  128. /**
  129. * Locates elements using a String locator (path) which identifies DOM
  130. * elements.
  131. *
  132. * @since 7.2
  133. * @param path
  134. * The String locator which identifies target elements.
  135. * @return The JavaScriptArray of DOM elements identified by {@code path} or
  136. * empty array if elements could not be located.
  137. */
  138. public JsArray<Element> getElementsByPath(String path) {
  139. JsArray<Element> jsElements = JavaScriptObject.createArray().cast();
  140. for (LocatorStrategy strategy : locatorStrategies) {
  141. if (strategy.validatePath(path)) {
  142. List<Element> elements = strategy.getElementsByPath(path);
  143. if (elements.size() > 0) {
  144. for (Element e : elements) {
  145. jsElements.push(e);
  146. }
  147. return jsElements;
  148. }
  149. }
  150. }
  151. return jsElements;
  152. }
  153. /**
  154. * Locates elements using a String locator (path) which identifies DOM
  155. * elements. The path starts from the specified root element.
  156. *
  157. * @see #getElementByPath(String)
  158. *
  159. * @since 7.2
  160. * @param path
  161. * The path of elements to be found
  162. * @param root
  163. * The root element where the path is anchored
  164. * @return The JavaScriptArray of DOM elements identified by {@code path} or
  165. * empty array if elements could not be located.
  166. */
  167. public JsArray<Element> getElementsByPathStartingAt(String path,
  168. Element root) {
  169. JsArray<Element> jsElements = JavaScriptObject.createArray().cast();
  170. for (LocatorStrategy strategy : locatorStrategies) {
  171. if (strategy.validatePath(path)) {
  172. List<Element> elements = strategy.getElementsByPathStartingAt(
  173. path, root);
  174. if (elements.size() > 0) {
  175. for (Element e : elements) {
  176. jsElements.push(e);
  177. }
  178. return jsElements;
  179. }
  180. }
  181. }
  182. return jsElements;
  183. }
  184. /**
  185. * Locates an element using a String locator (path) which identifies a DOM
  186. * element. The path starts from the specified root element.
  187. *
  188. * @see #getElementByPath(String)
  189. *
  190. * @since 7.2
  191. *
  192. * @param path
  193. * The path of the element to be found
  194. * @param root
  195. * The root element where the path is anchored
  196. * @return The DOM element identified by {@code path} or null if the element
  197. * could not be located.
  198. */
  199. public com.google.gwt.user.client.Element getElementByPathStartingAt(
  200. String path, Element root) {
  201. for (LocatorStrategy strategy : locatorStrategies) {
  202. if (strategy.validatePath(path)) {
  203. Element element = strategy.getElementByPathStartingAt(path,
  204. root);
  205. if (null != element) {
  206. return DOM.asOld(element);
  207. }
  208. }
  209. }
  210. return null;
  211. }
  212. /**
  213. * Returns the {@link ApplicationConnection} used by this locator.
  214. * <p>
  215. * This method is primarily for internal use by the framework.
  216. *
  217. * @return the application connection
  218. */
  219. public ApplicationConnection getClient() {
  220. return client;
  221. }
  222. /**
  223. * Check if a given selector is valid for LegacyLocatorStrategy.
  224. *
  225. * @param path
  226. * Vaadin selector path
  227. * @return true if passes path validation with LegacyLocatorStrategy
  228. */
  229. public boolean isValidForLegacyLocator(String path) {
  230. for (LocatorStrategy ls : locatorStrategies) {
  231. if (ls instanceof LegacyLocatorStrategy) {
  232. return ls.validatePath(path);
  233. }
  234. }
  235. return false;
  236. }
  237. }