From 7ab0f2e6a29d716ebdb96370dd5edf5108b853aa Mon Sep 17 00:00:00 2001 From: lvladika Date: Tue, 10 Nov 2026 08:26:58 +0100 Subject: [PATCH] perf(wda): send coordinate gestures with snapshotMaxDepth 1 A tap, double tap, long press, swipe, flick or drag at x,y is an XCUICoordinate on the app in WebDriverAgent, and XCTest snapshots the app down to snapshotMaxDepth before it touches, though the touch needs only the app's frame. On a heavy screen of a real iPhone that read takes seconds before every touch. Each gesture now goes out with the depth at 1 and the session's depth (wdaSnapshotMaxDepth) set again after it, so finds and page sources still see the whole tree. A refused depth sends the gesture as before; a refused restore is tried once more, then logged. --- pkg/driver/wda/client.go | 40 ++++++++++++---- pkg/driver/wda/client_test.go | 16 +++++++ pkg/driver/wda/gesture_test.go | 70 ++++++++++++++++++++++++++++ pkg/driver/wda/stale_element_test.go | 2 + 4 files changed, 118 insertions(+), 10 deletions(-) create mode 100644 pkg/driver/wda/gesture_test.go diff --git a/pkg/driver/wda/client.go b/pkg/driver/wda/client.go index ca9186bd..ee60f569 100644 --- a/pkg/driver/wda/client.go +++ b/pkg/driver/wda/client.go @@ -210,6 +210,31 @@ func wdaSnapshotMaxDepth() int { return defaultDepth } +// gesture posts a coordinate gesture (a tap, press or drag at x,y) with +// WebDriverAgent's snapshotMaxDepth at 1. WebDriverAgent makes the point an +// XCUICoordinate on the app, and XCTest snapshots the app down to +// snapshotMaxDepth before it touches, though the touch needs only the app's +// frame. On a heavy screen that snapshot takes seconds on a real iPhone, +// before every touch: from a long live list a /wda/tap took +// 3.5-4.1 s at depth 50 on an iPhone 13 (iOS 18) and 0.9 s at depth 1, and on +// iOS 17 each such tap froze the app for seconds. The depth goes back to +// wdaSnapshotMaxDepth() after the gesture, so finds and page sources still +// see the whole tree. If WebDriverAgent refuses the setting, the gesture goes +// as before. +func (c *Client) gesture(path string, body map[string]interface{}) error { + lowered := c.UpdateSettings(map[string]interface{}{"snapshotMaxDepth": 1}) == nil + _, err := c.post(c.sessionPath(path), body) + if lowered { + depth := map[string]interface{}{"snapshotMaxDepth": wdaSnapshotMaxDepth()} + if restoreErr := c.UpdateSettings(depth); restoreErr != nil { + if restoreErr = c.UpdateSettings(depth); restoreErr != nil { + logger.Warn("WDA: snapshotMaxDepth stayed 1 after a gesture, so finds see only the app: %v", restoreErr) + } + } + } + return err +} + // UpdateSettings updates WDA session settings. // Supports keys like shouldWaitForQuiescence, waitForIdleTimeout, etc. func (c *Client) UpdateSettings(settings map[string]interface{}) error { @@ -364,30 +389,27 @@ func (c *Client) ActivateApp(bundleID string) error { // Tap performs a tap at coordinates. func (c *Client) Tap(x, y float64) error { - _, err := c.post(c.sessionPath("/wda/tap"), map[string]interface{}{ + return c.gesture("/wda/tap", map[string]interface{}{ "x": x, "y": y, }) - return err } // DoubleTap performs a double tap at coordinates. func (c *Client) DoubleTap(x, y float64) error { - _, err := c.post(c.sessionPath("/wda/doubleTap"), map[string]interface{}{ + return c.gesture("/wda/doubleTap", map[string]interface{}{ "x": x, "y": y, }) - return err } // LongPress performs a long press at coordinates. func (c *Client) LongPress(x, y float64, durationSec float64) error { - _, err := c.post(c.sessionPath("/wda/touchAndHold"), map[string]interface{}{ + return c.gesture("/wda/touchAndHold", map[string]interface{}{ "x": x, "y": y, "duration": durationSec, }) - return err } // swipeMoveMs is how long the finger takes to travel from start to end. @@ -417,7 +439,7 @@ func (c *Client) pointerDrag(fromX, fromY, toX, toY float64, moveMs, restMs int) if restMs < 0 { restMs = 0 } - _, err := c.post(c.sessionPath("/actions"), map[string]interface{}{ + return c.gesture("/actions", map[string]interface{}{ "actions": []interface{}{ map[string]interface{}{ "type": "pointer", @@ -433,7 +455,6 @@ func (c *Client) pointerDrag(fromX, fromY, toX, toY float64, moveMs, restMs int) }, }, }) - return err } // DragFromTo presses at the start point, holds, then drags to the end point. @@ -441,14 +462,13 @@ func (c *Client) pointerDrag(fromX, fromY, toX, toY float64, moveMs, restMs int) // pressDurationSec is the hold before the move — the lift gesture reorder UIs // wait for. XCUITest paces the move itself; the drag speed is not ours to set. func (c *Client) DragFromTo(fromX, fromY, toX, toY, pressDurationSec float64) error { - _, err := c.post(c.sessionPath("/wda/dragfromtoforduration"), map[string]interface{}{ + return c.gesture("/wda/dragfromtoforduration", map[string]interface{}{ "fromX": fromX, "fromY": fromY, "toX": toX, "toY": toY, "duration": pressDurationSec, }) - return err } // Input diff --git a/pkg/driver/wda/client_test.go b/pkg/driver/wda/client_test.go index 30f7b9e5..52b31559 100644 --- a/pkg/driver/wda/client_test.go +++ b/pkg/driver/wda/client_test.go @@ -356,6 +356,10 @@ func TestActivateApp(t *testing.T) { // TestTap tests tap action func TestTap(t *testing.T) { server := mockWDAServer(func(w http.ResponseWriter, r *http.Request) { + if strings.HasSuffix(r.URL.Path, "/appium/settings") { // the depth around the gesture (TestGesturesRunAtDepthOne) + jsonResponse(w, map[string]interface{}{"status": 0}) + return + } if !strings.Contains(r.URL.Path, "/wda/tap") { t.Errorf("Expected /wda/tap, got %s", r.URL.Path) } @@ -378,6 +382,10 @@ func TestTap(t *testing.T) { // TestDoubleTap tests double tap action func TestDoubleTap(t *testing.T) { server := mockWDAServer(func(w http.ResponseWriter, r *http.Request) { + if strings.HasSuffix(r.URL.Path, "/appium/settings") { // the depth around the gesture (TestGesturesRunAtDepthOne) + jsonResponse(w, map[string]interface{}{"status": 0}) + return + } if !strings.Contains(r.URL.Path, "/wda/doubleTap") { t.Errorf("Expected /wda/doubleTap, got %s", r.URL.Path) } @@ -400,6 +408,10 @@ func TestDoubleTap(t *testing.T) { // TestLongPress tests long press action func TestLongPress(t *testing.T) { server := mockWDAServer(func(w http.ResponseWriter, r *http.Request) { + if strings.HasSuffix(r.URL.Path, "/appium/settings") { // the depth around the gesture (TestGesturesRunAtDepthOne) + jsonResponse(w, map[string]interface{}{"status": 0}) + return + } if !strings.Contains(r.URL.Path, "/wda/touchAndHold") { t.Errorf("Expected /wda/touchAndHold, got %s", r.URL.Path) } @@ -422,6 +434,10 @@ func TestLongPress(t *testing.T) { // TestSwipe tests swipe action func TestSwipe(t *testing.T) { server := mockWDAServer(func(w http.ResponseWriter, r *http.Request) { + if strings.HasSuffix(r.URL.Path, "/appium/settings") { // the depth around the gesture (TestGesturesRunAtDepthOne) + jsonResponse(w, map[string]interface{}{"status": 0}) + return + } if !strings.Contains(r.URL.Path, "/actions") { t.Errorf("Expected /actions, got %s", r.URL.Path) } diff --git a/pkg/driver/wda/gesture_test.go b/pkg/driver/wda/gesture_test.go new file mode 100644 index 00000000..b89d2884 --- /dev/null +++ b/pkg/driver/wda/gesture_test.go @@ -0,0 +1,70 @@ +package wda + +import ( + "encoding/json" + "net/http" + "strings" + "testing" +) + +// Every coordinate gesture goes out with snapshotMaxDepth at 1 and the +// session's depth set again after it. A refused depth leaves the gesture as it +// was, and a refused restore is tried once more. +func TestGesturesRunAtDepthOne(t *testing.T) { + gestures := map[string]func(c *Client) error{ + "/wda/tap": func(c *Client) error { return c.Tap(1, 2) }, + "/wda/doubleTap": func(c *Client) error { return c.DoubleTap(1, 2) }, + "/wda/touchAndHold": func(c *Client) error { return c.LongPress(1, 2, 1) }, + "/actions": func(c *Client) error { return c.Swipe(1, 2, 3, 4, 0) }, + "/wda/dragfromtoforduration": func(c *Client) error { return c.DragFromTo(1, 2, 3, 4, 1) }, + } + cases := []struct { + name string + refuse func(depth string, tries int) bool // which settings calls WebDriverAgent refuses + want string + }{ + {"accepted", func(string, int) bool { return false }, "depth=1 GESTURE depth=62"}, + {"depth refused", func(d string, _ int) bool { return d == "1" }, "depth=1! GESTURE"}, + {"restore refused once", func(d string, tries int) bool { return d == "62" && tries == 1 }, "depth=1 GESTURE depth=62! depth=62"}, + } + for path, gesture := range gestures { + for _, tc := range cases { + t.Run(tc.name+" "+path, func(t *testing.T) { + var got []string + restores := 0 + server := mockWDAServer(func(w http.ResponseWriter, r *http.Request) { + if !strings.HasSuffix(r.URL.Path, "/appium/settings") { + got = append(got, strings.TrimPrefix(r.URL.Path, "/session/s")) + jsonResponse(w, map[string]interface{}{"value": nil}) + return + } + var body struct { + Settings map[string]json.Number `json:"settings"` + } + _ = json.NewDecoder(r.Body).Decode(&body) + depth := body.Settings["snapshotMaxDepth"].String() + if depth == "62" { + restores++ + } + if tc.refuse(depth, restores) { + got = append(got, "depth="+depth+"!") + w.WriteHeader(http.StatusInternalServerError) + jsonResponse(w, map[string]interface{}{"value": map[string]string{"error": "unknown error", "message": "no"}}) + return + } + got = append(got, "depth="+depth) + jsonResponse(w, map[string]interface{}{"value": nil}) + }) + defer server.Close() + client := &Client{baseURL: server.URL, httpClient: http.DefaultClient, sessionID: "s"} + if err := gesture(client); err != nil { + t.Fatal(err) + } + want := strings.ReplaceAll(tc.want, "GESTURE", path) + if strings.Join(got, " ") != want { + t.Errorf("requests %q, want %q", strings.Join(got, " "), want) + } + }) + } + } +} diff --git a/pkg/driver/wda/stale_element_test.go b/pkg/driver/wda/stale_element_test.go index 1695ac49..310c31c2 100644 --- a/pkg/driver/wda/stale_element_test.go +++ b/pkg/driver/wda/stale_element_test.go @@ -44,6 +44,8 @@ func TestTapOnStaleElementRecovery(t *testing.T) { } taps <- [2]float64{point.X, point.Y} jsonResponse(w, map[string]any{"value": nil}) + case strings.HasSuffix(r.URL.Path, "/appium/settings"): // the tap's depth + jsonResponse(w, map[string]any{"value": nil}) case strings.HasSuffix(r.URL.Path, "/window/size"): jsonResponse(w, map[string]any{"value": map[string]int{"width": 390, "height": 844}}) default: