From: Henri Sara Date: Thu, 31 Oct 2013 09:56:04 +0000 (+0200) Subject: Produce TB4 style selectors in debug window (#12694) X-Git-Tag: 7.2.0.beta1~169^2~5^2~5^2^2~5 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=29ec642eb1321f58bbcfbde79c1f1362cf1e3f9b;p=vaadin-framework.git Produce TB4 style selectors in debug window (#12694) Change-Id: Ie2e5cc48c493d065221a30fa6d2058f7a61f0274 --- diff --git a/client/src/com/vaadin/client/debug/internal/SelectorPath.java b/client/src/com/vaadin/client/debug/internal/SelectorPath.java index d4502daeda..2ad77a246b 100644 --- a/client/src/com/vaadin/client/debug/internal/SelectorPath.java +++ b/client/src/com/vaadin/client/debug/internal/SelectorPath.java @@ -16,18 +16,21 @@ package com.vaadin.client.debug.internal; +import com.google.gwt.core.client.JsArrayString; import com.google.gwt.regexp.shared.MatchResult; import com.google.gwt.regexp.shared.RegExp; import com.google.gwt.user.client.Element; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.ComponentConnector; +import com.vaadin.client.FastStringSet; import com.vaadin.client.ServerConnector; import com.vaadin.client.Util; import com.vaadin.client.componentlocator.ComponentLocator; import com.vaadin.client.componentlocator.VaadinFinderLocatorStrategy; import com.vaadin.client.metadata.NoDataException; import com.vaadin.client.metadata.Property; +import com.vaadin.client.metadata.TypeDataStore; import com.vaadin.client.ui.AbstractConnector; import com.vaadin.client.ui.SubPartAware; @@ -108,7 +111,8 @@ public abstract class SelectorPath { * in a test. * * @param context - * the context to use (usually "getDriver()" or a variable name) + * the context to use (usually a variable name) or null for + * default * @return string to add in a JUnit test */ public abstract String getJUnitSelector(String context); @@ -551,7 +555,10 @@ public abstract class SelectorPath { @Override public String getJUnitSelector(String context) { - return context + ".findElement(By.xpath(\"" + getPath() + "\"))"; + // use driver by default + String contextString = null != context ? context : "getDriver()"; + return contextString + ".findElement(By.xpath(\"" + getPath() + + "\"))"; } @Override @@ -604,7 +611,8 @@ public abstract class SelectorPath { @Override public String getJUnitSelector(String context) { - return context + ".findElement(By.id(\"" + getId() + "\"))"; + String contextPart = null != context ? ", " + context : ""; + return "getElementById(\"" + getId() + "\"" + contextPart + ")"; } @Override @@ -633,11 +641,6 @@ public abstract class SelectorPath { */ protected abstract String getPath(); - @Override - public String getJUnitSelector(String context) { - return context + ".findElement(By.vaadin(\"" + getPath() + "\"))"; - } - @Override public Element findElement() { if (null != getParent()) { @@ -745,6 +748,50 @@ public abstract class SelectorPath { this.subPart = subPart; } + @Override + public String getJUnitSelector(String context) { + String componentClass = getComponentClass(); + String contextPart = null != context ? ", " + context : ""; + // TODO update after subpart API finished + if (null != getSubPart() || null == componentClass) { + return "getElementByPath(\"" + getPath() + "\"" + contextPart + + ")"; + } else if (null != getWidgetCaption()) { + return "getElementByCaption(" + componentClass + ".class, \"" + + getWidgetCaption() + "\"" + contextPart + ")"; + } else if (getWidgetIndex() >= 0) { + return "getElementByIndex(" + componentClass + ".class, " + + getWidgetIndex() + contextPart + ")"; + } else { + return "getElement(" + componentClass + ".class" + contextPart + + ")"; + } + } + + /** + * Returns the Vaadin server side component class to use for a widget + * class. + * + * @return fully qualified server side class name, null if unable to + * determine it + */ + private String getComponentClass() { + ComponentConnector connector = Util.findPaintable(getLocator() + .getClient(), findElement()); + Class connectorClass = connector + .getClass(); + FastStringSet identifiers = TypeDataStore.get().findIdentifiersFor( + connectorClass); + JsArrayString ids = identifiers.dump(); + if (ids.length() == 1) { + return ids.get(0); + } else { + return null; + } + } + + // these are used only to locate components on the client side by path + @Override protected String getPath() { return "/" + getWidgetClass() + getIndexString(false) @@ -800,6 +847,12 @@ public abstract class SelectorPath { this.path = path; } + @Override + public String getJUnitSelector(String context) { + String contextPart = null != context ? ", " + context : ""; + return "getElementByPath(\"" + getPath() + "\"" + contextPart + ")"; + } + /** * Returns the By.vaadin(...) path relative to the parent element. * diff --git a/client/src/com/vaadin/client/debug/internal/TestBenchSection.java b/client/src/com/vaadin/client/debug/internal/TestBenchSection.java index efab5ac11f..462309768f 100644 --- a/client/src/com/vaadin/client/debug/internal/TestBenchSection.java +++ b/client/src/com/vaadin/client/debug/internal/TestBenchSection.java @@ -67,7 +67,7 @@ public class TestBenchSection implements Section { this.parent = parent; String parentString = (parent != null) ? ("element" + parent.selectorIndex) - : "getDriver()"; + : null; String html = "
" @@ -215,7 +215,9 @@ public class TestBenchSection implements Section { SelectorPath path = SelectorPath.findTestBenchSelector(connector, element); - addSelectorWidgets(path); + if (null != path) { + addSelectorWidgets(path); + } } private SelectorWidget addSelectorWidgets(SelectorPath path) { diff --git a/client/src/com/vaadin/client/metadata/TypeDataStore.java b/client/src/com/vaadin/client/metadata/TypeDataStore.java index aa37d75dc8..649f018f95 100644 --- a/client/src/com/vaadin/client/metadata/TypeDataStore.java +++ b/client/src/com/vaadin/client/metadata/TypeDataStore.java @@ -69,6 +69,22 @@ public class TypeDataStore { return class1; } + // this is a very inefficient implementation for getting all the identifiers + // for a class + public FastStringSet findIdentifiersFor(Class type) { + FastStringSet result = FastStringSet.create(); + + JsArrayString keys = identifiers.getKeys(); + for (int i = 0; i < keys.length(); i++) { + String key = keys.get(i); + if (identifiers.get(key) == type) { + result.add(key); + } + } + + return result; + } + public static Type getType(Class clazz) { return new Type(clazz); }