mirror of
https://github.com/priyanshujain/sanderling.git
synced 2026-10-03 19:47:10 +00:00
* feat(ios): add simulator management package * feat(testrun): add iOS platform path (simctl launch + direct TCP) * feat(cli): add --ios-device flag and IosDevice option * feat(sdk-ios): add Kotlin Native iOS SDK (TCP agent + POSIX socket + dispatch pauser) * feat(folio-ios): wire SanderlingIos.start() in MainViewController * feat(folio-ios): add test-ios justfile recipe * fix(sdk-ios): remove unavailable C macros; manual byte swap + no-cast warnings * fix(testrun): simctl-first launch order for iOS; Maestro init after SDK connects * feat(proto): add env map to LaunchRequest * feat(driver): add env param to Launch interface + all implementations * feat(testrun): launch iOS app via XCTest with env vars instead of simctl * feat(sidecar): add IosDriverBackend using Maestro IOSDriver + env pass-through * feat(sidecar): wire env map in DriverService + IosDriverBackend in Main * fix(sidecar): use LocalIOSDevice (WDA+simctl) + stop before relaunch * fix(sidecar): include exception type in gRPC error description * fix(sidecar): pick free WDA port instead of hardcoded 9100 Use SocketUtils.nextFreePort to pick a free port in the 22000-23000 range rather than hardcoding 9100, which only worked if a previous WDA session left a listener there. * fix(sdk-ios): check semaphore wait result and throw on snapshot timeout dispatch_semaphore_wait returns nonzero on timeout; ignoring the return value caused pauseAndSnapshot to silently return an empty map, sending a garbage empty STATE frame to the host. Now throws so the agent loop reconnects instead. * fix(folio-ios): register snapshot extractors before starting agent SanderlingIos.start() was called before the snapshot objects were initialized, so a PAUSE message arriving early produced an empty snapshot. Move start() to after all extractors are registered. * chore(ios): remove dead LaunchApp function LaunchApp had no callers sincebff3a49switched iOS launch to go through the sidecar driver. Remove the dead code and unused os import. * test(ios): add unit tests for pickSimulator and iOS flag parsing Tests for all pickSimulator branches (by name, by UDID, unknown, empty list, iPhone preference, fallback to first). Also tests BootedUDID on a canceled context and verifies --platform ios and --ios-device flags are accepted by parseTestArgs. * fix(ios): propagate error from BootedUDID instead of silently swallowing * fix(sidecar): IosDriverBackend.healthy() returns true; WDA liveness checked in open() * fix(sidecar): warm up WDA after health check to absorb startup race * fix(runner): surface clear message on WDA connection drop * docs(testrun): note WDA warmup location above WaitForHealth * fix(sidecar): extract warmup + add one-shot WDA reconnect on IOException * fix(runner): fatal on permanent WDA drop during hierarchy fetch * fix(sidecar): walk cause chain in withReconnect to catch Maestro-wrapped IOException * fix(sidecar): explicit Unit return in pressKey and waitForIdle withReconnect lambdas * fix(sidecar): serialize WDA reconnect with ReentrantLock to prevent concurrent xcodebuild races * fix web examples package config * Revert "fix web examples package config" This reverts commit70c10ade27. * chore: gitignore built sanderling binary * fix(hierarchy): parse iOS [x1,y1][x2,y2] bounds + match iOS merged desc labels * refactor(folio-spec): replace bloated spec with two focused properties Login is opportunistic. Two concrete properties: 1. every new account starts with balance 0 2. every new txn changes ledger balance by exactly its signed amount Actions: directed login -> addAccount -> addTxn -> back weighted flow.
226 lines
6.2 KiB
Go
226 lines
6.2 KiB
Go
// Package hierarchy parses the TreeNode JSON produced by the Maestro sidecar
|
|
// and resolves selectors against it.
|
|
//
|
|
// Selector grammar (v0.1):
|
|
//
|
|
// id:<suffix> — match resource-id ending with ":id/<suffix>" or equal to <suffix>
|
|
// text:<value> — exact match on the node's text
|
|
// desc:<value> — exact match on the node's content-desc
|
|
// descPrefix:<prefix> — startsWith match on the node's content-desc (e.g. Compose testTag + UUID)
|
|
package hierarchy
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Bounds is an inclusive rectangle in device pixels.
|
|
type Bounds struct {
|
|
Left int `json:"left"`
|
|
Top int `json:"top"`
|
|
Right int `json:"right"`
|
|
Bottom int `json:"bottom"`
|
|
}
|
|
|
|
// Center returns the center point of the bounds.
|
|
func (b Bounds) Center() (int, int) {
|
|
return (b.Left + b.Right) / 2, (b.Top + b.Bottom) / 2
|
|
}
|
|
|
|
// Width returns the bounds' width.
|
|
func (b Bounds) Width() int { return b.Right - b.Left }
|
|
|
|
// Height returns the bounds' height.
|
|
func (b Bounds) Height() int { return b.Bottom - b.Top }
|
|
|
|
// Element is a flattened view of one hierarchy node.
|
|
type Element struct {
|
|
ResourceID string `json:"resourceId,omitempty"`
|
|
Text string `json:"text,omitempty"`
|
|
Description string `json:"description,omitempty"`
|
|
Class string `json:"class,omitempty"`
|
|
Package string `json:"package,omitempty"`
|
|
// Screen holds the current route/screen name when set by the driver on the
|
|
// root element (web platform only; empty for native platforms).
|
|
Screen string `json:"screen,omitempty"`
|
|
Clickable bool `json:"clickable,omitempty"`
|
|
Enabled bool `json:"enabled,omitempty"`
|
|
Checked bool `json:"checked,omitempty"`
|
|
Focused bool `json:"focused,omitempty"`
|
|
Selected bool `json:"selected,omitempty"`
|
|
Bounds Bounds `json:"bounds"`
|
|
}
|
|
|
|
// Tree is a flat collection of every node in a hierarchy dump, in pre-order.
|
|
type Tree struct {
|
|
Elements []*Element `json:"elements"`
|
|
}
|
|
|
|
// treeNodeJSON mirrors the Maestro TreeNode JSON structure.
|
|
type treeNodeJSON struct {
|
|
Attributes map[string]string `json:"attributes"`
|
|
Children []treeNodeJSON `json:"children"`
|
|
Clickable *bool `json:"clickable"`
|
|
Enabled *bool `json:"enabled"`
|
|
Focused *bool `json:"focused"`
|
|
Checked *bool `json:"checked"`
|
|
Selected *bool `json:"selected"`
|
|
}
|
|
|
|
// Parse parses a Maestro TreeNode JSON hierarchy.
|
|
func Parse(text string) (*Tree, error) {
|
|
text = strings.TrimSpace(text)
|
|
if text == "" {
|
|
return &Tree{}, nil
|
|
}
|
|
var root treeNodeJSON
|
|
if err := json.Unmarshal([]byte(text), &root); err != nil {
|
|
return nil, fmt.Errorf("hierarchy: %w", err)
|
|
}
|
|
tree := &Tree{}
|
|
walkNode(&root, tree)
|
|
return tree, nil
|
|
}
|
|
|
|
func walkNode(node *treeNodeJSON, tree *Tree) {
|
|
element := elementFromNode(node)
|
|
tree.Elements = append(tree.Elements, element)
|
|
for i := range node.Children {
|
|
walkNode(&node.Children[i], tree)
|
|
}
|
|
}
|
|
|
|
func elementFromNode(node *treeNodeJSON) *Element {
|
|
attrs := node.Attributes
|
|
element := &Element{}
|
|
|
|
element.ResourceID = attrs["resource-id"]
|
|
if element.ResourceID == "" {
|
|
element.ResourceID = attrs["identifier"]
|
|
}
|
|
element.Text = attrs["text"]
|
|
element.Description = attrs["content-desc"]
|
|
if element.Description == "" {
|
|
element.Description = attrs["accessibilityText"]
|
|
}
|
|
element.Class = attrs["class"]
|
|
element.Package = attrs["package"]
|
|
element.Screen = attrs["sanderling-screen"]
|
|
|
|
if node.Clickable != nil {
|
|
element.Clickable = *node.Clickable
|
|
}
|
|
if node.Enabled != nil {
|
|
element.Enabled = *node.Enabled
|
|
}
|
|
if node.Focused != nil {
|
|
element.Focused = *node.Focused
|
|
}
|
|
if node.Checked != nil {
|
|
element.Checked = *node.Checked
|
|
}
|
|
if node.Selected != nil {
|
|
element.Selected = *node.Selected
|
|
}
|
|
|
|
if b, ok := attrs["bounds"]; ok && b != "" {
|
|
bounds, err := parseBounds(b)
|
|
if err == nil {
|
|
element.Bounds = bounds
|
|
}
|
|
}
|
|
|
|
return element
|
|
}
|
|
|
|
// Find returns the first element matching the selector, or nil.
|
|
func (t *Tree) Find(selector string) *Element {
|
|
kind, value, ok := parseSelector(selector)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
for _, element := range t.Elements {
|
|
if match(element, kind, value) {
|
|
return element
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// FindAll returns every element matching the selector.
|
|
func (t *Tree) FindAll(selector string) []*Element {
|
|
kind, value, ok := parseSelector(selector)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
var matches []*Element
|
|
for _, element := range t.Elements {
|
|
if match(element, kind, value) {
|
|
matches = append(matches, element)
|
|
}
|
|
}
|
|
return matches
|
|
}
|
|
|
|
func parseSelector(selector string) (string, string, bool) {
|
|
index := strings.IndexByte(selector, ':')
|
|
if index <= 0 {
|
|
return "", "", false
|
|
}
|
|
return selector[:index], selector[index+1:], true
|
|
}
|
|
|
|
func match(element *Element, kind, value string) bool {
|
|
switch kind {
|
|
case "id":
|
|
if element.ResourceID == value {
|
|
return true
|
|
}
|
|
return strings.HasSuffix(element.ResourceID, ":id/"+value)
|
|
case "text":
|
|
return element.Text == value
|
|
case "desc":
|
|
// Exact match, or iOS merged label "desc, child text".
|
|
return element.Description == value || strings.HasPrefix(element.Description, value+", ")
|
|
case "descPrefix":
|
|
return strings.HasPrefix(element.Description, value)
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// boundsPattern matches "[l,t,r,b]" (4-value Android/Maestro format).
|
|
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) {
|
|
if m := boundsPattern.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
|
|
}
|
|
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)
|
|
}
|