From 860e668d54f9ad0cc67ae3fb94ba6fef67d1dbfa Mon Sep 17 00:00:00 2001 From: lvladika Date: Sun, 8 Nov 2026 17:47:48 +0100 Subject: [PATCH] fix(wda): clear fields with typed backspaces on a simulator WebDriverAgent's first try at clearing a field is a hardware keyboard's Clear key (an IOHIDEvent). On a simulator iOS then takes a hardware keyboard as attached and keeps the software keyboard parked below the screen whenever a field has focus, so what an app shows with the keyboard (a betslip's stake chips) never appears. A simulator's session now sets useClearTextShortcut false: typed backspaces, as Maestro erases. A real iPhone keeps its keyboard up after the Clear key, and typed backspaces erase only what is left of the cursor: a tap in the middle of a one-digit field put the cursor before the digit about half the time, and nothing was erased. A real device's session sets it true. It is sent either way, since WebDriverAgent keeps settings process-wide. --- pkg/driver/wda/client.go | 22 +++++++++++++++++++++- pkg/driver/wda/client_test.go | 23 +++++++++++++++++++++++ pkg/driver/wda/driver.go | 3 +++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/pkg/driver/wda/client.go b/pkg/driver/wda/client.go index 78e931b9..a4e1d069 100644 --- a/pkg/driver/wda/client.go +++ b/pkg/driver/wda/client.go @@ -35,6 +35,11 @@ type Client struct { sourceOKAt time.Time sourceErr error sourceErrsAt time.Time + + // typedClear: clear a field with typed backspaces instead of WebDriverAgent's + // hardware-keyboard Clear key (useClearTextShortcut false). On for a + // simulator only; NewDriver sets it from the device. + typedClear bool } // SourceUnreadableSince returns the last page-source error when no read @@ -169,7 +174,22 @@ func (c *Client) CreateSession(bundleID string, alertAction string) error { // even though stock Maestro's XCTest traversal (no such cap) sees them // (#171). Applied here so every session — the primary one and the one // launchApp recreates — gets it. - _ = c.UpdateSettings(map[string]interface{}{"snapshotMaxDepth": wdaSnapshotMaxDepth()}) + // + // useClearTextShortcut: WebDriverAgent's first try at clearing a field is + // a hardware keyboard's Clear key (an IOHIDEvent). On a simulator iOS then + // takes a hardware keyboard as attached and keeps the software keyboard + // parked below the screen whenever a field has focus, so what an app shows + // with the keyboard (a betslip's stake chips) never appears; there the + // field is cleared with typed backspaces, as Maestro erases. A real + // iPhone keeps its keyboard up after the Clear key, and typed backspaces + // erase only what is left of the cursor: a tap in the middle of a + // one-digit field put the cursor before the digit about half the time, + // and nothing was erased. Sent either way, since WebDriverAgent keeps it + // process-wide. + _ = c.UpdateSettings(map[string]interface{}{ + "snapshotMaxDepth": wdaSnapshotMaxDepth(), + "useClearTextShortcut": !c.typedClear, + }) return nil } diff --git a/pkg/driver/wda/client_test.go b/pkg/driver/wda/client_test.go index b3d915ec..30f7b9e5 100644 --- a/pkg/driver/wda/client_test.go +++ b/pkg/driver/wda/client_test.go @@ -9,6 +9,8 @@ import ( "net/http/httptest" "strings" "testing" + + "github.com/devicelab-dev/maestro-runner/pkg/core" ) // mockWDAServer creates a mock WDA server for testing @@ -40,6 +42,7 @@ func TestNewClient(t *testing.T) { // TestCreateSession tests session creation func TestCreateSession(t *testing.T) { var sawSnapshotDepth bool + clearShortcut := map[string]interface{}{} server := mockWDAServer(func(w http.ResponseWriter, r *http.Request) { switch { case r.Method == "POST" && r.URL.Path == "/session": @@ -57,6 +60,10 @@ func TestCreateSession(t *testing.T) { if _, ok := body.Settings["snapshotMaxDepth"]; ok { sawSnapshotDepth = true } + // ...and says how a field is cleared. + if v, ok := body.Settings["useClearTextShortcut"]; ok { + clearShortcut["sent"] = v + } jsonResponse(w, map[string]interface{}{"value": map[string]interface{}{}}) default: t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path) @@ -80,6 +87,22 @@ func TestCreateSession(t *testing.T) { if !sawSnapshotDepth { t.Error("CreateSession should raise snapshotMaxDepth so deep RN trees are not clipped") } + // A real iPhone (the client's default) keeps WDA's hardware-key clear: typed + // backspaces erase only what is left of the cursor. + if clearShortcut["sent"] != true { + t.Errorf("CreateSession on a real device should send useClearTextShortcut true, sent %v", clearShortcut["sent"]) + } + + // A simulator's driver clears with typed backspaces: the hardware key parks + // the software keyboard there. + delete(clearShortcut, "sent") + NewDriver(client, &core.PlatformInfo{IsSimulator: true}, "SIM") + if err := client.CreateSession("com.example.app", ""); err != nil { + t.Fatalf("CreateSession failed: %v", err) + } + if clearShortcut["sent"] != false { + t.Errorf("CreateSession on a simulator should send useClearTextShortcut false, sent %v", clearShortcut["sent"]) + } } // TestCreateSessionAlternateFormat tests alternate session response format diff --git a/pkg/driver/wda/driver.go b/pkg/driver/wda/driver.go index d880fd24..7ded1bf0 100644 --- a/pkg/driver/wda/driver.go +++ b/pkg/driver/wda/driver.go @@ -91,6 +91,9 @@ const ( // NewDriver creates a new WDA driver. func NewDriver(client *Client, info *core.PlatformInfo, udid string) *Driver { + if client != nil { + client.typedClear = info != nil && info.IsSimulator + } return &Driver{ client: client, info: info,