Skip to content

Allow textContains and regex selectors via official On.* MatchPattern - #3

Draft
tonycoder-hub wants to merge 1 commit into
masterfrom
cursor/textcontains-regex-selectors-d296
Draft

Allow textContains and regex selectors via official On.* MatchPattern#3
tonycoder-hub wants to merge 1 commit into
masterfrom
cursor/textcontains-regex-selectors-d296

Conversation

@tonycoder-hub

Copy link
Copy Markdown
Owner

Fixes codematrixer/hmdriver2#53: d(textContains=...) was rejected by UiObject.__verify as textContains is not allowed.

Cause

The allowlist in _uiobject.py only accepted exact On.* names (text, description, id, …). HarmonyOS uitest / @ohos.UiTest does not expose On.textContains or On.textMatches. Fuzzy and regex matching are the official two-arg form:

On.text(txt: string, pattern?: MatchPattern): On
On.description(val: string, pattern?: MatchPattern): On
On.id(id: string, pattern: MatchPattern): On   // API 18+
On.type(tp: string, pattern: MatchPattern): On // API 18+

MatchPattern (OpenHarmony js-apis-uitest): EQUALS=0, CONTAINS=1, STARTS_WITH=2, ENDS_WITH=3, REG_EXP=4 (API 18+), REG_EXP_ICASE=5 (API 18+).

Change

  • Accept common Python aliases (textContains, textMatches, descriptionContains, …).
  • Map them to the real Hypium invoke: On.text(value, 1) / On.text(value, 4), etc.
  • Keep existing exact selectors as single-arg On.text(value) so the wire format does not change.
  • Reject conflicting pairs that map to the same On.* (e.g. text + textContains).
  • Do not invent fake On.textContains method names.

Tests

tests/test_selector.py covers the allowlist and selector construction with a fake client (no device). 16 passed.

d(textContains="tab")       # On.text("tab", CONTAINS)
d(textMatches=r"^tab_.*")   # On.text("^tab_.*", REG_EXP)
d(descriptionContains="icon")
d(type="Button", textContains="Toast")

Regex (REG_EXP) requires uitest API 18+ on the device. Older images still get CONTAINS / STARTS_WITH / ENDS_WITH via On.text.

Open in Web Open in Cursor 

UiObject.__verify rejected textContains as "not allowed". HarmonyOS
uitest has no On.textContains; fuzzy/regex matching is On.text(value,
MatchPattern) (CONTAINS=1, REG_EXP=4, etc.). Map Python aliases to that
two-arg API, keep exact selectors single-arg, and add no-device tests.

Fixes codematrixer#53

Co-authored-by: Tony Jin <407243179@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

需要支持模糊搜索匹配和正则匹配

2 participants