mirror of
https://github.com/priyanshujain/sanderling.git
synced 2026-10-05 04:27:09 +00:00
fix(hierarchy): parse iOS [x1,y1][x2,y2] bounds + match iOS merged desc labels
This commit is contained in:
1 parent
900a7652c1
commit
92524a6ded
2 files changed
+55
-6
No files matched your search
@@ -183,7 +183,8 @@ func match(element *Element, kind, value string) bool {
|
|||||||
case "text":
|
case "text":
|
||||||
return element.Text == value
|
return element.Text == value
|
||||||
case "desc":
|
case "desc":
|
||||||
return element.Description == value
|
// Exact match, or iOS merged label "desc, child text".
|
||||||
|
return element.Description == value || strings.HasPrefix(element.Description, value+", ")
|
||||||
case "descPrefix":
|
case "descPrefix":
|
||||||
return strings.HasPrefix(element.Description, value)
|
return strings.HasPrefix(element.Description, value)
|
||||||
default:
|
default:
|
||||||
@@ -191,14 +192,14 @@ func match(element *Element, kind, value string) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// boundsPattern matches "[l,t,r,b]" (4-value Maestro format).
|
// boundsPattern matches "[l,t,r,b]" (4-value Android/Maestro format).
|
||||||
var boundsPattern = regexp.MustCompile(`^\[(-?\d+),(-?\d+),(-?\d+),(-?\d+)\]$`)
|
var boundsPattern = regexp.MustCompile(`^\[(-?\d+),(-?\d+),(-?\d+),(-?\d+)\]$`)
|
||||||
|
|
||||||
|
// boundsPatternTwo matches "[x1,y1][x2,y2]" (iOS XCUITest format).
|
||||||
|
var boundsPatternTwo = regexp.MustCompile(`^\[(-?\d+),(-?\d+)\]\[(-?\d+),(-?\d+)\]$`)
|
||||||
|
|
||||||
func parseBounds(text string) (Bounds, error) {
|
func parseBounds(text string) (Bounds, error) {
|
||||||
m := boundsPattern.FindStringSubmatch(text)
|
if m := boundsPattern.FindStringSubmatch(text); m != nil {
|
||||||
if m == nil {
|
|
||||||
return Bounds{}, fmt.Errorf("bounds %q: not in [L,T,R,B] form", text)
|
|
||||||
}
|
|
||||||
coords := make([]int, 4)
|
coords := make([]int, 4)
|
||||||
for i := range 4 {
|
for i := range 4 {
|
||||||
v, err := strconv.Atoi(m[i+1])
|
v, err := strconv.Atoi(m[i+1])
|
||||||
@@ -209,3 +210,16 @@ func parseBounds(text string) (Bounds, error) {
|
|||||||
}
|
}
|
||||||
return Bounds{Left: coords[0], Top: coords[1], Right: coords[2], Bottom: coords[3]}, nil
|
return Bounds{Left: coords[0], Top: coords[1], Right: coords[2], Bottom: coords[3]}, nil
|
||||||
}
|
}
|
||||||
|
if m := boundsPatternTwo.FindStringSubmatch(text); m != nil {
|
||||||
|
coords := make([]int, 4)
|
||||||
|
for i := range 4 {
|
||||||
|
v, err := strconv.Atoi(m[i+1])
|
||||||
|
if err != nil {
|
||||||
|
return Bounds{}, err
|
||||||
|
}
|
||||||
|
coords[i] = v
|
||||||
|
}
|
||||||
|
return Bounds{Left: coords[0], Top: coords[1], Right: coords[2], Bottom: coords[3]}, nil
|
||||||
|
}
|
||||||
|
return Bounds{}, fmt.Errorf("bounds %q: not in [L,T,R,B] or [x1,y1][x2,y2] form", text)
|
||||||
|
}
|
||||||
@@ -164,3 +164,38 @@ func TestAccessibilityTextFallback(t *testing.T) {
|
|||||||
t.Fatal("expected match via accessibilityText fallback")
|
t.Fatal("expected match via accessibilityText fallback")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIOSMergedLabel(t *testing.T) {
|
||||||
|
// iOS merges contentDescription with child text: "add_account_button, + Add account"
|
||||||
|
input := `{
|
||||||
|
"attributes": {"accessibilityText": "add_account_button, + Add account", "bounds": "[20,777][382,825]"},
|
||||||
|
"children": []
|
||||||
|
}`
|
||||||
|
tree, _ := Parse(input)
|
||||||
|
el := tree.Find("desc:add_account_button")
|
||||||
|
if el == nil {
|
||||||
|
t.Fatal("expected desc: to match iOS merged label")
|
||||||
|
}
|
||||||
|
if el.Bounds.Left != 20 || el.Bounds.Top != 777 || el.Bounds.Right != 382 || el.Bounds.Bottom != 825 {
|
||||||
|
t.Errorf("unexpected bounds: %+v", el.Bounds)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIOSBoundsFormat(t *testing.T) {
|
||||||
|
input := `{
|
||||||
|
"attributes": {"accessibilityText": "account_card:abc123, Tim, $100", "bounds": "[20,130][382,202]"},
|
||||||
|
"children": []
|
||||||
|
}`
|
||||||
|
tree, _ := Parse(input)
|
||||||
|
el := tree.Find("descPrefix:account_card:")
|
||||||
|
if el == nil {
|
||||||
|
t.Fatal("expected descPrefix to match iOS account card")
|
||||||
|
}
|
||||||
|
if el.Bounds.Left != 20 || el.Bounds.Top != 130 || el.Bounds.Right != 382 || el.Bounds.Bottom != 202 {
|
||||||
|
t.Errorf("unexpected bounds: %+v", el.Bounds)
|
||||||
|
}
|
||||||
|
cx, cy := el.Bounds.Center()
|
||||||
|
if cx != 201 || cy != 166 {
|
||||||
|
t.Errorf("unexpected center: (%d, %d)", cx, cy)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user