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.

SelectorPath.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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.debug.internal;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. import com.google.gwt.dom.client.Element;
  21. import com.vaadin.client.ServerConnector;
  22. import com.vaadin.client.componentlocator.ComponentLocator;
  23. import com.vaadin.client.componentlocator.SelectorPredicate;
  24. /**
  25. * A single segment of a selector path pointing to an Element.
  26. * <p>
  27. * This class should be considered internal to the framework and may change at
  28. * any time.
  29. * <p>
  30. *
  31. * @since 7.1.x
  32. */
  33. public class SelectorPath {
  34. private final String path;
  35. private final Element element;
  36. private final ComponentLocator locator;
  37. private static Map<String, Integer> counter = new HashMap<String, Integer>();
  38. private static Map<String, String> legacyNames = new HashMap<String, String>();
  39. static {
  40. legacyNames.put("FilterSelect", "ComboBox");
  41. legacyNames.put("ScrollTable", "Table");
  42. }
  43. protected SelectorPath(ServerConnector c, Element e) {
  44. element = e;
  45. locator = new ComponentLocator(c.getConnection());
  46. path = locator.getPathForElement(e);
  47. }
  48. public String getPath() {
  49. return path;
  50. }
  51. public Element getElement() {
  52. return element;
  53. }
  54. public ComponentLocator getLocator() {
  55. return locator;
  56. }
  57. /**
  58. * Generate ElementQuery code for Java. Fallback to By.vaadin(path) if
  59. * dealing with LegacyLocator
  60. *
  61. * @return String containing Java code for finding the element described by
  62. * path
  63. */
  64. public String getElementQuery() {
  65. if (path.isEmpty() || locator.isValidForLegacyLocator(path)) {
  66. return getLegacyLocatorQuery();
  67. }
  68. String[] fragments;
  69. String tmpPath = path;
  70. List<SelectorPredicate> postFilters = SelectorPredicate
  71. .extractPostFilterPredicates(path);
  72. if (postFilters.size() > 0) {
  73. tmpPath = tmpPath.substring(1, tmpPath.lastIndexOf(')'));
  74. }
  75. // Generate an ElementQuery
  76. fragments = tmpPath.split("/");
  77. String elementQueryString = "";
  78. int index = 0;
  79. for (SelectorPredicate p : postFilters) {
  80. if (p.getIndex() > 0) {
  81. index = p.getIndex();
  82. }
  83. }
  84. for (int i = 1; i < fragments.length; ++i) {
  85. if (fragments[i].isEmpty()) {
  86. // Recursive searches cause empty fragments
  87. continue;
  88. }
  89. // if i == 1 or previous fragment was empty, search is recursive
  90. boolean recursive = (i > 1 ? fragments[i - 1].isEmpty() : false);
  91. // if elementQueryString is not empty, join the next query with .
  92. String queryFragment = (!elementQueryString.isEmpty() ? "." : "");
  93. // if search is not recursive, add another $ in front of fragment
  94. queryFragment += (!recursive ? "$" : "")
  95. + generateFragment(fragments[i]);
  96. elementQueryString += queryFragment;
  97. }
  98. if (!hasId(fragments[fragments.length - 1])) {
  99. if (index == 0) {
  100. elementQueryString += ".first()";
  101. } else {
  102. elementQueryString += ".get(" + index + ")";
  103. }
  104. }
  105. // Return full Java variable assignment and eQuery
  106. return generateJavaVariable(fragments[fragments.length - 1])
  107. + elementQueryString + ";";
  108. }
  109. /**
  110. * Finds out if the given query fragment has a defined id
  111. *
  112. * @param fragment
  113. * Query fragment
  114. * @return true if has id
  115. */
  116. private boolean hasId(String fragment) {
  117. for (SelectorPredicate p : SelectorPredicate
  118. .extractPredicates(fragment)) {
  119. if (p.getName().equals("id")) {
  120. return true;
  121. }
  122. }
  123. return false;
  124. }
  125. /**
  126. * Generates a recursive ElementQuery for given path fragment
  127. *
  128. * @param fragment
  129. * Query fragment
  130. * @return ElementQuery java code as a String
  131. */
  132. private String generateFragment(String fragment) {
  133. // Get Element.class -name
  134. String elementClass = getComponentName(fragment) + "Element.class";
  135. String queryFragment = "$(" + elementClass + ")";
  136. for (SelectorPredicate p : SelectorPredicate
  137. .extractPredicates(fragment)) {
  138. // Add in predicates like .caption and .id
  139. queryFragment += "." + p.getName() + "(\"" + p.getValue() + "\")";
  140. }
  141. return queryFragment;
  142. }
  143. /**
  144. * Returns the name of the component described by given query fragment
  145. *
  146. * @param fragment
  147. * Query fragment
  148. * @return Class part of fragment
  149. */
  150. protected String getComponentName(String fragment) {
  151. return fragment.split("\\[")[0];
  152. }
  153. /**
  154. * Generates a legacy locator for SelectorPath.
  155. *
  156. * @return String containing Java code for element search and assignment
  157. */
  158. private String getLegacyLocatorQuery() {
  159. String name;
  160. if (!path.isEmpty()) {
  161. String[] frags = path.split("/");
  162. name = getComponentName(frags[frags.length - 1]).substring(1);
  163. } else {
  164. name = "root";
  165. }
  166. if (legacyNames.containsKey(name)) {
  167. name = legacyNames.get(name);
  168. }
  169. name = getNameWithCount(name);
  170. // Use direct path and elementX naming style.
  171. return "WebElement " + name.substring(0, 1).toLowerCase()
  172. + name.substring(1) + " = getDriver().findElement(By.vaadin(\""
  173. + path + "\"));";
  174. }
  175. /**
  176. * Get variable name with counter for given component name.
  177. *
  178. * @param name
  179. * Component name
  180. * @return name followed by count
  181. */
  182. protected String getNameWithCount(String name) {
  183. if (!counter.containsKey(name)) {
  184. counter.put(name, 0);
  185. }
  186. counter.put(name, counter.get(name) + 1);
  187. name += counter.get(name);
  188. return name;
  189. }
  190. /**
  191. * Generate Java variable assignment from given selector fragment
  192. *
  193. * @param pathFragment
  194. * Selector fragment
  195. * @return piece of java code
  196. */
  197. private String generateJavaVariable(String pathFragment) {
  198. // Get element type and predicates from fragment
  199. List<SelectorPredicate> predicates = SelectorPredicate
  200. .extractPredicates(pathFragment);
  201. String elementType = pathFragment.split("\\[")[0];
  202. String name = getNameFromPredicates(predicates, elementType);
  203. if (name.equals(elementType)) {
  204. name = getNameWithCount(name);
  205. }
  206. // Replace unusable characters
  207. name = name.replaceAll("\\W", "");
  208. // Lowercase the first character of name
  209. return elementType + "Element " + name.substring(0, 1).toLowerCase()
  210. + name.substring(1) + " = ";
  211. }
  212. /**
  213. * Get variable name based on predicates. Fallback to elementType
  214. *
  215. * @param predicates
  216. * Predicates related to element
  217. * @param elementType
  218. * Element type
  219. * @return name for Variable
  220. */
  221. private String getNameFromPredicates(List<SelectorPredicate> predicates,
  222. String elementType) {
  223. String name = elementType;
  224. for (SelectorPredicate p : predicates) {
  225. if ("caption".equals(p.getName())) {
  226. // Caption + elementType is a suitable name
  227. name = p.getValue() + elementType;
  228. } else if ("id".equals(p.getName())) {
  229. // Just id. This is unique, use it.
  230. return p.getValue();
  231. }
  232. }
  233. return name;
  234. }
  235. }