Repository navigation
feat(uiautomator2-server): add a class chain query parser - #173
mykola-mokhnach wants to merge 3 commits into
Conversation
Parses `**/Type[`pred`][$descendant pred$][n]/Type...` into an immutable ClassChain of ChainSteps, reusing PredicateParser for the predicates. Parser and AST only, nothing uses it yet. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…lass chains Map predicate error offsets back to the query when doubled markers were collapsed, chain the predicate exception as the cause, accept supplementary-plane identifier characters in class names, and tidy the Javadoc and line lengths. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…lass chains `..` selects the parent, `^T` ancestors, `>T`/`<T` following/preceding siblings and `~T` all siblings. Positions count along the axis. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Locator examples, to give a feel for the grammar. Nothing here runs yet: the parser is not wired to a strategy, and Everyday lookups
StructureAxesBeyond what XPath or UiSelector do wellNotes
|
|
The axes look like a very powerful addition! They are a little unclear to me though, so I'd like some clarification: I assume this would this find the first Would this iterate up the tree starting from This is the most confusing one to me. Does it select all siblings of Regarding this exclusion axis, I'm not sure about using the tilde symbol ( Also, can the path chain continue after a request with an axis? e.g. |
|
@eglitise thanks, these are fair questions and my wording in the examples was sloppy. In order:
Equivalent XPathThe XPath below is written for the XPath 2.0 engine. The axes: The rest of the examples from the earlier commentTwo examples have no practical XPath equivalent or need a check:
In XPath, class names with |
|
Thanks for the clarification, it's a lot more clear to me now. I agree that the exclamation point symbol does not make sense for this usecase. The tilde symbol isn't particularly intuitive still, but I also can't think of any other symbol available on a standard keyboard that would work better. |
| pos += 2; // "[" and the marker | ||
| StringBuilder text = new StringBuilder(); | ||
| // source index of each predicate character, so errors point into the query | ||
| int[] sourceIndex = new int[source.length() + 1]; |
There was a problem hiding this comment.
Could we translate the predicate offset only when PredicateParser.parse throws? Each filter allocates an int[] sized to the entire query, so total allocation grows quadratically for chains with many short filters. Saving the predicate start and scanning past doubled markers on error would preserve the reported offsets and avoid allocating the array on successful parses.
| @Override | ||
| public String toString() { | ||
| String marker = String.valueOf(scope.getMarker()); | ||
| String escaped = predicate.toString().replace(marker, marker + marker); |
There was a problem hiding this comment.
Could we add a round-trip test at the predicate depth limit? For example, TRUEPREDICATE AND followed by 63 copies of NOT and a final TRUEPREDICATE parses at depth 64. The logical predicate formatter then adds outer parentheses around the AND, so reparsing the serialized filter reaches depth 65 and fails. Omitting redundant parentheses at the filter root would preserve the round trip for this case.
Second part of phase 1 of the class chain locator plan: the class chain parser. It follows #170 (predicate parser) and is parser and AST only, so there is no behaviour change. The evaluator and the
-android class chainstrategy come next.Grammar
Same shape as WebDriverAgent's
FBClassChainQueryParser:**/at the start or/**/between steps makes the next step a descendant lookup, a plain/means direct children.[pred]filters the candidates themselves,[$pred$]keeps candidates that have a matching descendant. The filters keep their source order.0is invalid, and it can be set once.`or$inside a predicate stands for the character itself.Axes beyond WDA, so
fromParentand sibling lookups have an equivalent. They are steps after a plain/:..^TT>T<T~TTon both sides, never the node itself (not a negation)They are rejected right after
**/or/**/, but a descendant step can follow them (../**/T). Example:**/*[`text == 'Price'`]/>android.widget.TextView[1].Differences from WDA:
android.widget.Button,Outer$Inner), checked as dotted Java identifiers. There are no short names and noXCUIElementTypeAny, only*.PredicateParser, so an unknown attribute or a malformed value fails at parse time, with the predicate's own reason.PredicateParser, not WDA's token state machine.AST
ClassChainis an immutable list ofChainStep. ItstoString()is a canonical form that parses back to the same chain.ChainStepholdsgetAxis()(CHILD,DESCENDANT,PARENT,ANCESTOR,FOLLOWING_SIBLING,PRECEDING_SIBLING,SIBLING;isDescendant()is a shortcut),getClassName()(null for*),getFilters()(scopeSELForDESCENDANTplus aPredicateNode) andgetPosition().Errors
ClassChainSyntaxExceptionextendsInvalidSelectorException:Cannot parse class chain query '<q>'. <reason> at position N(0-based). A predicate error is reported asInvalid predicate: <reason>at the exact position in the query, also when the predicate contains doubled markers. ThePredicateSyntaxExceptionis chained as the cause.Tests
30 tests in
ClassChainParserTests: the valid and invalid vectors of WDA'sFBClassChainTests, plus the axes,$and dotted class names, escapes, the canonical round trip, error positions and immutability. The full server unit suite passes.