From a82000e3046aeda7242ae1c3f39dbe48afaf9a9b Mon Sep 17 00:00:00 2001 From: lvladika Date: Mon, 16 Nov 2026 23:36:18 +0100 Subject: [PATCH 1/9] fix: scrollUntilVisible with a timeout of its own looks until it passes, as in Maestro A step that sets `timeout:` now keeps looking for its element after the screen stops moving, swiping every round, and fails only when that timeout passes. Maestro's scrollUntilVisible swipes until its timeout whatever the screen does (Orchestra.scrollUntilVisible). A list that fills late reads to ScrollProgress exactly like the end of the content (an empty page while its rows load; on the drivers that compare the page source, a spinner's node too, while wda's settled screenshot sees a turning spinner move), so the step gave up after two or three scrolls where Maestro waited and found the row. A step without a timeout keeps the early stop, so a target that is not in the list still does not cost the whole default timeout. The end-of-content answers for an element already found (on screen but not yet centred, or partly past its container's edge) are unchanged, and so is devicelab iOS's answer to a permission prompt over a list that looks stuck. A step whose timeout passes now fails in Maestro's words, "No visible element found: ", naming the timeout. Its cause says "not found" in every driver but appium's partly visible case (an element found but never sufficiently visible), so the report files it as element_not_found (devicelab iOS's "not visible" was filed as an assertion) and that appium case as timeout. One that runs out of maxScrolls keeps its message. The no-progress tests of uiautomator2, devicelab, appium, wda and devicelab_ios_legacy gave their step a 60 s (legacy 20 s) timeout; they now give none, and still check the early stop. --- pkg/core/scrollprogress.go | 32 ++++++ pkg/core/scrollprogress_test.go | 29 ++++- pkg/driver/appium/commands.go | 20 ++-- pkg/driver/appium/driver_test.go | 94 +++++++++++++++- pkg/driver/devicelab/commands.go | 9 +- pkg/driver/devicelab/commands_test.go | 71 +++++++++++- pkg/driver/devicelab_ios/actions.go | 16 ++- pkg/driver/devicelab_ios/actions_test.go | 65 +++++++++++ pkg/driver/devicelab_ios_legacy/commands.go | 10 +- .../devicelab_ios_legacy/settle_test.go | 58 +++++++++- pkg/driver/uiautomator2/commands.go | 9 +- pkg/driver/uiautomator2/commands_test.go | 72 ++++++++++++- pkg/driver/wda/commands.go | 9 +- pkg/driver/wda/commands_test.go | 102 +++++++++++++++++- 14 files changed, 566 insertions(+), 30 deletions(-) diff --git a/pkg/core/scrollprogress.go b/pkg/core/scrollprogress.go index a6599ce6..817b889f 100644 --- a/pkg/core/scrollprogress.go +++ b/pkg/core/scrollprogress.go @@ -3,6 +3,9 @@ package core import ( "fmt" "hash/fnv" + "time" + + "github.com/devicelab-dev/maestro-runner/pkg/flow" ) // scrollStuckWindow is how many consecutive captures the stuck check keeps. @@ -72,3 +75,32 @@ func ScrollSignature(capture string) string { _, _ = h.Write([]byte(capture)) return fmt.Sprintf("%016x", h.Sum64()) } + +// ScrollLooksUntilTimeout reports whether a scrollUntilVisible step that has +// not found its element keeps looking after the surface stops moving: it does +// when the flow gave the step its own `timeout:`. That timeout is the step's +// budget, as in Maestro, which swipes until it passes whatever the screen +// does. A list that fills late (empty, or still loading its rows, when the +// step starts) reads to ScrollProgress exactly like a list at its end, and +// stopping there fails a step whose author gave it time to wait. +// +// A step without a timeout keeps the early stop, so a target that is not in +// the list does not cost the whole default timeout. The end-of-content +// answers for an element already found (on screen but not yet centred, or +// partly past its container's edge) do not depend on this: they still end +// the step as soon as the surface stops. +func ScrollLooksUntilTimeout(step *flow.ScrollUntilVisibleStep) bool { + return step.TimeoutMs > 0 +} + +// ScrollTimeoutMessage is the failure message of a scrollUntilVisible step +// whose timeout passed before its element was visible enough, led by +// Maestro's words for it ("No visible element found: ..."). selector is the +// driver's description of the element; an empty one is left out. +func ScrollTimeoutMessage(selector, direction string, timeout time.Duration) string { + found := "No visible element found" + if selector != "" { + found += ": " + selector + } + return fmt.Sprintf("%s (scrolling %s until the %dms timeout)", found, direction, timeout.Milliseconds()) +} diff --git a/pkg/core/scrollprogress_test.go b/pkg/core/scrollprogress_test.go index 8b762cd2..83a6c5b8 100644 --- a/pkg/core/scrollprogress_test.go +++ b/pkg/core/scrollprogress_test.go @@ -1,6 +1,12 @@ package core -import "testing" +import ( + "strings" + "testing" + "time" + + "github.com/devicelab-dev/maestro-runner/pkg/flow" +) func TestScrollProgressObserve(t *testing.T) { cases := []struct { @@ -49,3 +55,24 @@ func TestScrollSignature(t *testing.T) { t.Errorf("signature length = %d, want 16 hex chars", len(a)) } } + +func TestScrollLooksUntilTimeout(t *testing.T) { + if ScrollLooksUntilTimeout(&flow.ScrollUntilVisibleStep{}) { + t.Error("a step without a timeout of its own keeps the early stop") + } + if !ScrollLooksUntilTimeout(&flow.ScrollUntilVisibleStep{BaseStep: flow.BaseStep{TimeoutMs: 30000}}) { + t.Error("a step with its own timeout looks until it passes") + } +} + +func TestScrollTimeoutMessage(t *testing.T) { + got := ScrollTimeoutMessage(`id="row"`, "down", 30*time.Second) + if !strings.HasPrefix(got, `No visible element found: id="row"`) || !strings.Contains(got, "30000ms") { + t.Errorf("message = %q", got) + } + // A selector the driver cannot describe (only a relative or a state + // filter) leaves no dangling colon. + if got := ScrollTimeoutMessage("", "down", time.Second); got != "No visible element found (scrolling down until the 1000ms timeout)" { + t.Errorf("message without a description = %q", got) + } +} diff --git a/pkg/driver/appium/commands.go b/pkg/driver/appium/commands.go index eddaaf76..26b4874a 100644 --- a/pkg/driver/appium/commands.go +++ b/pkg/driver/appium/commands.go @@ -476,11 +476,15 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com if hanging != nil { return successResult("Element visible (end of content, partly past its scroll container)", hanging) } - reason := fmt.Sprintf("scrolling %s made no progress after %d scrolls (end of content?)", direction, i) - if partiallyVisible { - return errorResult(fmt.Errorf("element found but never sufficiently visible after scrolling"), reason) + // A step with its own timeout looks until it passes, as in Maestro: + // a list still filling reads as stuck too. + if !core.ScrollLooksUntilTimeout(step) { + reason := fmt.Sprintf("scrolling %s made no progress after %d scrolls (end of content?)", direction, i) + if partiallyVisible { + return errorResult(fmt.Errorf("element found but never sufficiently visible after scrolling"), reason) + } + return errorResult(fmt.Errorf("element not found after scrolling"), reason) } - return errorResult(fmt.Errorf("element not found after scrolling"), reason) } // Scroll. A failed scroll ends the step with its reason; ignoring it @@ -497,10 +501,14 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com if hanging != nil { return successResult("Element visible (scrolls ran out, partly past its scroll container)", hanging) } + reason := "" + if !time.Now().Before(deadline) { + reason = core.ScrollTimeoutMessage(step.Element.Describe(), direction, timeout) + } if partiallyVisible { - return errorResult(fmt.Errorf("element found but never sufficiently visible after scrolling"), "") + return errorResult(fmt.Errorf("element found but never sufficiently visible after scrolling"), reason) } - return errorResult(fmt.Errorf("element not found after scrolling"), "") + return errorResult(fmt.Errorf("element not found after scrolling"), reason) } // scrollSurfaceSignature reduces the current page source to a key for diff --git a/pkg/driver/appium/driver_test.go b/pkg/driver/appium/driver_test.go index 6330bc05..3a9e49d4 100644 --- a/pkg/driver/appium/driver_test.go +++ b/pkg/driver/appium/driver_test.go @@ -8,6 +8,7 @@ import ( "net/http" "net/http/httptest" "strings" + "sync/atomic" "testing" "time" @@ -2961,8 +2962,9 @@ func TestAccessibilityLabelOf(t *testing.T) { } func TestAppiumScrollUntilVisibleStopsWhenScreenStopsMoving(t *testing.T) { - // The same source on every read: a list at its end. Two scrolls that - // change nothing are proof enough — the loop must not spend the other 18. + // The same source on every read: a list at its end. For a step without a + // timeout of its own, two scrolls that change nothing are proof enough — + // the loop must not spend the other 18. scrollCount := 0 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") @@ -2994,7 +2996,6 @@ func TestAppiumScrollUntilVisibleStopsWhenScreenStopsMoving(t *testing.T) { result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ Element: flow.Selector{Text: "NonExistent"}, Direction: "down", - BaseStep: flow.BaseStep{TimeoutMs: 60000}, }) if result.Success { @@ -3007,3 +3008,90 @@ func TestAppiumScrollUntilVisibleStopsWhenScreenStopsMoving(t *testing.T) { t.Errorf("message should name the reason, got %q", result.Message) } } + +// fillsLateServer is an Appium server whose page source stays the same, a +// list still loading, until the shownAfter'th scroll, and then has the target +// row on screen. It counts the scrolls (W3C actions). +func fillsLateServer(scrolls *atomic.Int32, shownAfter int32) *httptest.Server { + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + path := r.URL.Path + switch { + case strings.HasSuffix(path, "/source"): + row := `` + if scrolls.Load() >= shownAfter { + row = `` + } + writeJSON(w, map[string]interface{}{ + "value": ` + + + ` + row + ` + +`, + }) + case strings.Contains(path, "/actions") && r.Method == "POST": + scrolls.Add(1) + writeJSON(w, map[string]interface{}{"value": nil}) + case strings.Contains(path, "/window/rect"): + writeJSON(w, map[string]interface{}{ + "value": map[string]interface{}{"width": 1080.0, "height": 2340.0, "x": 0.0, "y": 0.0}, + }) + default: + writeJSON(w, map[string]interface{}{"value": nil}) + } + })) +} + +func TestAppiumScrollUntilVisibleWithTimeoutWaitsForListThatFillsLate(t *testing.T) { + // The screen does not change for four scrolls (the list is still + // loading), then the target is there. A step with its own timeout looks + // until that timeout, as Maestro does: a still screen is not proof that + // the content has ended. + var scrolls atomic.Int32 + server := fillsLateServer(&scrolls, 4) + defer server.Close() + driver := createTestAppiumDriver(server) + + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "Target"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 20000}, + }) + + if !result.Success { + t.Fatalf("expected the target once the list filled, got %q", result.Message) + } + if got := scrolls.Load(); got < 4 { + t.Errorf("expected at least 4 scrolls before the target showed, got %d", got) + } +} + +func TestAppiumScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { + // A screen that never changes and a step with its own timeout: the step + // keeps scrolling until the timeout, then fails in Maestro's words. + var scrolls atomic.Int32 + server := fillsLateServer(&scrolls, 1<<30) + defer server.Close() + driver := createTestAppiumDriver(server) + + start := time.Now() + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "NonExistent"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 5000}, + }) + + if result.Success { + t.Fatal("expected failure when the element never shows") + } + if elapsed := time.Since(start); elapsed < 5*time.Second { + t.Errorf("gave up after %v, before the 5s timeout", elapsed) + } + if got := scrolls.Load(); got <= 2 { + t.Errorf("expected more scrolls than the no-progress stop's 2, got %d", got) + } + if !strings.Contains(result.Message, "No visible element found") { + t.Errorf("message should be Maestro's, got %q", result.Message) + } +} diff --git a/pkg/driver/devicelab/commands.go b/pkg/driver/devicelab/commands.go index c197391a..fced0161 100644 --- a/pkg/driver/devicelab/commands.go +++ b/pkg/driver/devicelab/commands.go @@ -1721,7 +1721,11 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com if hanging != nil { return successResult(fmt.Sprintf("Element visible after %d scrolls (end of content, partly past its scroll container)", i), hanging) } - return errorResult(fmt.Errorf("element not found"), fmt.Sprintf("Element not found: scrolling %s made no progress after %d scrolls (end of content?)", direction, i)) + // A step with its own timeout looks until it passes, as in Maestro: + // a list still filling reads as stuck too. + if !core.ScrollLooksUntilTimeout(step) { + return errorResult(fmt.Errorf("element not found"), fmt.Sprintf("Element not found: scrolling %s made no progress after %d scrolls (end of content?)", direction, i)) + } } scrollErr := error(nil) @@ -1743,6 +1747,9 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com if hanging != nil { return successResult("Element visible (scrolls ran out, partly past its scroll container)", hanging) } + if !time.Now().Before(deadline) { + return errorResult(fmt.Errorf("element not found"), core.ScrollTimeoutMessage(step.Element.Describe(), direction, timeout)) + } return errorResult(fmt.Errorf("element not found"), fmt.Sprintf("Element not found after %d scrolls", maxScrolls)) } diff --git a/pkg/driver/devicelab/commands_test.go b/pkg/driver/devicelab/commands_test.go index 2aa56612..c0b02e12 100644 --- a/pkg/driver/devicelab/commands_test.go +++ b/pkg/driver/devicelab/commands_test.go @@ -327,8 +327,9 @@ func movingList(captures *int) string { } func TestScrollUntilVisibleStopsWhenScreenStopsMoving(t *testing.T) { - // The same capture on every read: a list at its end. Two scrolls that - // change nothing are proof enough — the loop must not spend the other 18. + // The same capture on every read: a list at its end. For a step without a + // timeout of its own, two scrolls that change nothing are proof enough — + // the loop must not spend the other 18. client := &mockDeviceLabClient{ sourceFunc: func() (string, error) { return ` @@ -344,7 +345,6 @@ func TestScrollUntilVisibleStopsWhenScreenStopsMoving(t *testing.T) { result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ Element: flow.Selector{Text: "NonExistent"}, Direction: "down", - BaseStep: flow.BaseStep{TimeoutMs: 60000}, }) if result.Success { @@ -357,3 +357,68 @@ func TestScrollUntilVisibleStopsWhenScreenStopsMoving(t *testing.T) { t.Errorf("message should name the reason, got %q", result.Message) } } + +// fillsLateSource is a page source that stays the same, a list still loading, +// until the shownAfter'th scroll, and then has the target row on screen. +func fillsLateSource(scrolls, shownAfter int) string { + row := `` + if scrolls >= shownAfter { + row = `` + } + return ` + + + ` + row + ` + +` +} + +func TestScrollUntilVisibleWithTimeoutWaitsForListThatFillsLate(t *testing.T) { + // The screen does not change for four scrolls (the list is still + // loading), then the target is there. A step with its own timeout looks + // until that timeout, as Maestro does: a still screen is not proof that + // the content has ended. + client := &mockDeviceLabClient{} + client.sourceFunc = func() (string, error) { return fillsLateSource(client.scrollCalls, 4), nil } + driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) + + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "Target"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 20000}, + }) + + if !result.Success { + t.Fatalf("expected the target once the list filled, got %q", result.Message) + } + if client.scrollCalls < 4 { + t.Errorf("expected at least 4 scrolls before the target showed, got %d", client.scrollCalls) + } +} + +func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { + // A screen that never changes and a step with its own timeout: the step + // keeps scrolling until the timeout, then fails in Maestro's words. + client := &mockDeviceLabClient{sourceFunc: func() (string, error) { return fillsLateSource(0, 1), nil }} + driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) + + start := time.Now() + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "NonExistent"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 5000}, + }) + + if result.Success { + t.Fatal("expected failure when the element never shows") + } + if elapsed := time.Since(start); elapsed < 5*time.Second { + t.Errorf("gave up after %v, before the 5s timeout", elapsed) + } + if client.scrollCalls <= 2 { + t.Errorf("expected more scrolls than the no-progress stop's 2, got %d", client.scrollCalls) + } + if !strings.Contains(result.Message, "No visible element found") { + t.Errorf("message should be Maestro's, got %q", result.Message) + } +} diff --git a/pkg/driver/devicelab_ios/actions.go b/pkg/driver/devicelab_ios/actions.go index 3b946d8f..cf4e8d24 100644 --- a/pkg/driver/devicelab_ios/actions.go +++ b/pkg/driver/devicelab_ios/actions.go @@ -354,12 +354,22 @@ func (d *Driver) scrollUntilVisible(s *flow.ScrollUntilVisibleStep) *core.Comman if shown != nil { return core.SuccessResult("element visible at the end of the content", toElementInfo(shown)) } - err := fmt.Errorf("%s not visible: scrolling %s made no progress after %d scrolls (end of content?)", - describe(s.Element), direction, i+1) - return core.ErrorResult(err, err.Error()) + // A step with its own timeout looks until it passes, as in + // Maestro: a list still filling reads as stuck too. + if !core.ScrollLooksUntilTimeout(s) { + err := fmt.Errorf("%s not visible: scrolling %s made no progress after %d scrolls (end of content?)", + describe(s.Element), direction, i+1) + return core.ErrorResult(err, err.Error()) + } } } } + if time.Now().After(deadline) { + // A "not found" cause, as the other drivers give, files the failure + // as element_not_found, which is what Maestro's ElementNotFound is. + err := fmt.Errorf("%s not found after scrolling %s", describe(s.Element), direction) + return core.ErrorResult(err, core.ScrollTimeoutMessage(describe(s.Element), direction, timeout)) + } err := fmt.Errorf("%s not visible after scrolling %s", describe(s.Element), direction) return core.ErrorResult(err, err.Error()) } diff --git a/pkg/driver/devicelab_ios/actions_test.go b/pkg/driver/devicelab_ios/actions_test.go index d4554270..b86262c2 100644 --- a/pkg/driver/devicelab_ios/actions_test.go +++ b/pkg/driver/devicelab_ios/actions_test.go @@ -10,6 +10,7 @@ import ( "strings" "sync/atomic" "testing" + "time" "github.com/devicelab-dev/maestro-runner/pkg/flow" ) @@ -226,6 +227,70 @@ func TestScrollUntilVisibleStopsWithoutProgress(t *testing.T) { } } +func TestScrollUntilVisibleWithTimeoutWaitsForListThatFillsLate(t *testing.T) { + // The screen does not change for four scrolls (the list is still + // loading), then the target is there. A step with its own timeout looks + // until that timeout, as Maestro does: a still screen is not proof that + // the content has ended. + var scrolls atomic.Int32 + d, _, _ := newTestDriver(t, func(cmd string, _ Args) (*Response, error) { + filled := scrolls.Load() >= 4 + switch cmd { + case "act": + scrolls.Add(1) + case "find": + if filled { + return tree(node(1, "Cell", "Target", 0, 300, 400, 50)), nil + } + return tree(), nil + case "settle": + if filled { + return ok(&Payload{ScreenHash: "list"}), nil + } + return ok(&Payload{ScreenHash: "loading"}), nil + } + return ok(nil), nil + }) + res := d.Execute(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "Target"}, + BaseStep: flow.BaseStep{TimeoutMs: 20000}, + }) + if !res.Success || scrolls.Load() < 4 { + t.Fatalf("res=%+v scrolls=%d, want the target after at least 4 scrolls", res, scrolls.Load()) + } +} + +func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { + // A screen that never changes and a step with its own timeout: the step + // keeps scrolling until the timeout, then fails in Maestro's words. + d, fa, _ := newTestDriver(t, func(cmd string, _ Args) (*Response, error) { + switch cmd { + case "settle": + return ok(&Payload{ScreenHash: "same"}), nil + case "find": + return tree(), nil + } + return ok(nil), nil + }) + start := time.Now() + res := d.Execute(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "Missing"}, + BaseStep: flow.BaseStep{TimeoutMs: 1000}, + }) + if res.Success || !strings.Contains(res.Message, "No visible element found") { + t.Fatalf("res = %+v, want Maestro's message", res) + } + if res.Error == nil || !strings.Contains(res.Error.Error(), "not found") { + t.Errorf("cause = %v, want a not-found cause, as the other drivers give", res.Error) + } + if elapsed := time.Since(start); elapsed < time.Second { + t.Errorf("gave up after %v, before the 1s timeout", elapsed) + } + if n := len(fa.sent("act")); n <= 3 { + t.Errorf("scrolls = %d, want more than the no-progress stop's 3", n) + } +} + func TestScrollUntilVisibleLimits(t *testing.T) { d, fa, _ := newTestDriver(t, screenOf()) res := d.Execute(&flow.ScrollUntilVisibleStep{Element: flow.Selector{Text: "X"}, MaxScrolls: 2}) diff --git a/pkg/driver/devicelab_ios_legacy/commands.go b/pkg/driver/devicelab_ios_legacy/commands.go index a79cc9c5..31d6f366 100644 --- a/pkg/driver/devicelab_ios_legacy/commands.go +++ b/pkg/driver/devicelab_ios_legacy/commands.go @@ -844,7 +844,9 @@ func (d *Driver) handleScrollUntilVisible(s *flow.ScrollUntilVisibleStep) *core. if node := d.scrollTargetIn(nodes, s.Element, s.VisibilityPercentage); node != nil { return core.SuccessResult("element found after scrolling", toElementInfo(node)) } - if sig, ok := scrollSurfaceSignature(nodes); ok && progress.Observe(sig) { + // A step with its own timeout looks until it passes, as in + // Maestro: a list still filling reads as stuck too. + if sig, ok := scrollSurfaceSignature(nodes); ok && progress.Observe(sig) && !core.ScrollLooksUntilTimeout(s) { return core.ErrorResult( fmt.Errorf("element not found after %d scrolls", i), fmt.Sprintf("scroll target not found: scrolling %s made no progress (end of content?)", direction), @@ -860,6 +862,12 @@ func (d *Driver) handleScrollUntilVisible(s *flow.ScrollUntilVisibleStep) *core. // screen each time and its rows are not where they will stop. d.settleScreen(settleCapMs(deadline)) } + if !time.Now().Before(deadline) { + return core.ErrorResult( + fmt.Errorf("element not found after scrolling"), + core.ScrollTimeoutMessage(describeSelector(s.Element), direction, timeout), + ) + } return core.ErrorResult( fmt.Errorf("element not found after scrolling"), "scroll target not found", diff --git a/pkg/driver/devicelab_ios_legacy/settle_test.go b/pkg/driver/devicelab_ios_legacy/settle_test.go index 4a567d73..85730d20 100644 --- a/pkg/driver/devicelab_ios_legacy/settle_test.go +++ b/pkg/driver/devicelab_ios_legacy/settle_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "net/http" "net/http/httptest" + "strings" "sync" "testing" "time" @@ -124,7 +125,8 @@ func TestScrollUntilVisibleSettlesAfterEachSwipe(t *testing.T) { } } -// A screen that stops moving ends the loop early with a clear reason. +// A screen that stops moving ends the loop early with a clear reason, for a +// step without a timeout of its own. func TestScrollUntilVisibleStopsWhenNothingMoves(t *testing.T) { info := &core.PlatformInfo{ScreenWidth: 400, ScreenHeight: 800} r := &settleRunner{settled: true, nodesFn: func(int) []SnapshotNode { @@ -132,8 +134,7 @@ func TestScrollUntilVisibleStopsWhenNothingMoves(t *testing.T) { }} d := r.driver(t, info) res := d.handleScrollUntilVisible(&flow.ScrollUntilVisibleStep{ - Element: flow.Selector{ID: "missing"}, - BaseStep: flow.BaseStep{TimeoutMs: 20000}, + Element: flow.Selector{ID: "missing"}, }) if res.Success { t.Fatal("found an element that is not there") @@ -143,6 +144,57 @@ func TestScrollUntilVisibleStopsWhenNothingMoves(t *testing.T) { } } +// A step with its own timeout looks until that timeout, as Maestro does: a +// screen that does not change for four scrolls (the list is still loading) +// and then shows the target is found. +func TestScrollUntilVisibleWithTimeoutWaitsForListThatFillsLate(t *testing.T) { + info := &core.PlatformInfo{ScreenWidth: 400, ScreenHeight: 800} + r := &settleRunner{settled: true, nodesFn: func(drags int) []SnapshotNode { + if drags < 4 { + return []SnapshotNode{{Type: "ActivityIndicator", Label: "loading", Rect: SnapshotRect{X: 180, Y: 380, Width: 40, Height: 40}}} + } + return []SnapshotNode{{Type: "Cell", Identifier: "target", Rect: SnapshotRect{X: 0, Y: 300, Width: 100, Height: 40}}} + }} + d := r.driver(t, info) + res := d.handleScrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{ID: "target"}, + BaseStep: flow.BaseStep{TimeoutMs: 20000}, + }) + if !res.Success { + t.Fatalf("expected the target once the list filled, got %q", res.Message) + } + if r.drags < 4 { + t.Errorf("drags = %d, want at least 4 before the target showed", r.drags) + } +} + +// A screen that never changes and a step with its own timeout: the step keeps +// scrolling until the timeout, then fails in Maestro's words. +func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { + info := &core.PlatformInfo{ScreenWidth: 400, ScreenHeight: 800} + r := &settleRunner{settled: true, nodesFn: func(int) []SnapshotNode { + return []SnapshotNode{{Type: "Other", Label: "end of list", Rect: SnapshotRect{Width: 400, Height: 40}}} + }} + d := r.driver(t, info) + start := time.Now() + res := d.handleScrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{ID: "missing"}, + BaseStep: flow.BaseStep{TimeoutMs: 1000}, + }) + if res.Success { + t.Fatal("found an element that is not there") + } + if elapsed := time.Since(start); elapsed < time.Second { + t.Errorf("gave up after %v, before the 1s timeout", elapsed) + } + if r.drags <= 4 { + t.Errorf("drags = %d, want more than the no-progress stop's", r.drags) + } + if !strings.Contains(res.Message, "No visible element found") { + t.Errorf("message should be Maestro's, got %q", res.Message) + } +} + // An index selector picks the Nth match and still needs it on screen. func TestScrollTargetInHonoursIndex(t *testing.T) { d := &Driver{info: &core.PlatformInfo{ScreenWidth: 400, ScreenHeight: 800}} diff --git a/pkg/driver/uiautomator2/commands.go b/pkg/driver/uiautomator2/commands.go index aefcc48b..00cf4cd5 100644 --- a/pkg/driver/uiautomator2/commands.go +++ b/pkg/driver/uiautomator2/commands.go @@ -765,7 +765,11 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com if hanging != nil { return successResult(fmt.Sprintf("Element visible after %d scrolls (end of content, partly past its scroll container)", i), hanging) } - return errorResult(fmt.Errorf("element not found"), fmt.Sprintf("Element not found: scrolling %s made no progress after %d scrolls (end of content?)", direction, i)) + // A step with its own timeout looks until it passes, as in Maestro: + // a list still filling reads as stuck too. + if !core.ScrollLooksUntilTimeout(step) { + return errorResult(fmt.Errorf("element not found"), fmt.Sprintf("Element not found: scrolling %s made no progress after %d scrolls (end of content?)", direction, i)) + } } scrollErr := error(nil) @@ -787,6 +791,9 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com if hanging != nil { return successResult("Element visible (scrolls ran out, partly past its scroll container)", hanging) } + if !time.Now().Before(deadline) { + return errorResult(fmt.Errorf("element not found"), core.ScrollTimeoutMessage(step.Element.Describe(), direction, timeout)) + } return errorResult(fmt.Errorf("element not found"), fmt.Sprintf("Element not found after %d scrolls", maxScrolls)) } diff --git a/pkg/driver/uiautomator2/commands_test.go b/pkg/driver/uiautomator2/commands_test.go index 522cbf29..3d893b91 100644 --- a/pkg/driver/uiautomator2/commands_test.go +++ b/pkg/driver/uiautomator2/commands_test.go @@ -11,6 +11,7 @@ import ( "strings" "sync/atomic" "testing" + "time" "github.com/devicelab-dev/maestro-runner/pkg/core" "github.com/devicelab-dev/maestro-runner/pkg/flow" @@ -4491,8 +4492,9 @@ func movingList(captures *int) string { } func TestScrollUntilVisibleStopsWhenScreenStopsMoving(t *testing.T) { - // The same capture on every read: a list at its end. Two scrolls that - // change nothing are proof enough — the loop must not spend the other 18. + // The same capture on every read: a list at its end. For a step without a + // timeout of its own, two scrolls that change nothing are proof enough — + // the loop must not spend the other 18. client := &MockUIA2Client{ sourceFunc: func() (string, error) { return ` @@ -4508,7 +4510,6 @@ func TestScrollUntilVisibleStopsWhenScreenStopsMoving(t *testing.T) { result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ Element: flow.Selector{Text: "NonExistent"}, Direction: "down", - BaseStep: flow.BaseStep{TimeoutMs: 60000}, }) if result.Success { @@ -4521,3 +4522,68 @@ func TestScrollUntilVisibleStopsWhenScreenStopsMoving(t *testing.T) { t.Errorf("message should name the reason, got %q", result.Message) } } + +// fillsLateSource is a page source that stays the same, a list still loading, +// until the shownAfter'th scroll, and then has the target row on screen. +func fillsLateSource(scrolls, shownAfter int) string { + row := `` + if scrolls >= shownAfter { + row = `` + } + return ` + + + ` + row + ` + +` +} + +func TestScrollUntilVisibleWithTimeoutWaitsForListThatFillsLate(t *testing.T) { + // The screen does not change for four scrolls (the list is still + // loading), then the target is there. A step with its own timeout looks + // until that timeout, as Maestro does: a still screen is not proof that + // the content has ended. + client := &MockUIA2Client{} + client.sourceFunc = func() (string, error) { return fillsLateSource(len(client.scrollCalls), 4), nil } + driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) + + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "Target"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 20000}, + }) + + if !result.Success { + t.Fatalf("expected the target once the list filled, got %q", result.Message) + } + if got := len(client.scrollCalls); got < 4 { + t.Errorf("expected at least 4 scrolls before the target showed, got %d", got) + } +} + +func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { + // A screen that never changes and a step with its own timeout: the step + // keeps scrolling until the timeout, then fails in Maestro's words. + client := &MockUIA2Client{sourceFunc: func() (string, error) { return fillsLateSource(0, 1), nil }} + driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) + + start := time.Now() + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "NonExistent"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 5000}, + }) + + if result.Success { + t.Fatal("expected failure when the element never shows") + } + if elapsed := time.Since(start); elapsed < 5*time.Second { + t.Errorf("gave up after %v, before the 5s timeout", elapsed) + } + if got := len(client.scrollCalls); got <= 2 { + t.Errorf("expected more scrolls than the no-progress stop's 2, got %d", got) + } + if !strings.Contains(result.Message, "No visible element found") { + t.Errorf("message should be Maestro's, got %q", result.Message) + } +} diff --git a/pkg/driver/wda/commands.go b/pkg/driver/wda/commands.go index 9dfc8bb3..ee8e9202 100644 --- a/pkg/driver/wda/commands.go +++ b/pkg/driver/wda/commands.go @@ -1132,13 +1132,20 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com if shown != nil { return successResult("Element visible after scrolling (end of content before centring)", shown) } - return errorResult(fmt.Errorf("element not found after scrolling"), fmt.Sprintf("Element not found: %s — scrolling %s made no progress after %d scrolls (end of content?)", selectorDesc(step.Element), direction, i+1)) + // A step with its own timeout looks until it passes, as in Maestro: + // a list still filling reads as stuck too. + if !core.ScrollLooksUntilTimeout(step) { + return errorResult(fmt.Errorf("element not found after scrolling"), fmt.Sprintf("Element not found: %s — scrolling %s made no progress after %d scrolls (end of content?)", selectorDesc(step.Element), direction, i+1)) + } } } if shown != nil { return successResult("Element visible after scrolling (scrolls ran out before centring)", shown) } + if !time.Now().Before(deadline) { + return errorResult(fmt.Errorf("element not found after scrolling"), core.ScrollTimeoutMessage(selectorDesc(step.Element), direction, timeout)) + } return errorResult(fmt.Errorf("element not found after scrolling"), fmt.Sprintf("Element not found: %s", selectorDesc(step.Element))) } diff --git a/pkg/driver/wda/commands_test.go b/pkg/driver/wda/commands_test.go index 61906e15..31004b0b 100644 --- a/pkg/driver/wda/commands_test.go +++ b/pkg/driver/wda/commands_test.go @@ -9,7 +9,9 @@ import ( "net/http/httptest" "os/exec" "strings" + "sync/atomic" "testing" + "time" "github.com/devicelab-dev/maestro-runner/pkg/core" "github.com/devicelab-dev/maestro-runner/pkg/flow" @@ -5226,9 +5228,9 @@ func TestSwipeFromElementPointStartsAtPoint(t *testing.T) { } func TestScrollUntilVisibleStopsWhenScreenStopsMoving(t *testing.T) { - // The same picture after every scroll: a list at its end. Three settled - // screenshots that match are proof enough — the loop must not spend the - // other 17 scrolls. + // The same picture after every scroll: a list at its end. For a step + // without a timeout of its own, three settled screenshots that match are + // proof enough — the loop must not spend the rest of the default timeout. scrollCount := 0 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") @@ -5261,7 +5263,6 @@ func TestScrollUntilVisibleStopsWhenScreenStopsMoving(t *testing.T) { result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ Element: flow.Selector{Text: "NonExistent"}, Direction: "down", - BaseStep: flow.BaseStep{TimeoutMs: 60000}, }) if result.Success { @@ -5275,6 +5276,99 @@ func TestScrollUntilVisibleStopsWhenScreenStopsMoving(t *testing.T) { } } +// fillsLateWDAServer is a WebDriverAgent whose screen stays the same, a list +// still loading, until the shownAfter'th scroll, and then shows the target +// row. It counts the scrolls (W3C actions). +func fillsLateWDAServer(scrolls *atomic.Int32, shownAfter int32) *httptest.Server { + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + path := r.URL.Path + filled := scrolls.Load() >= shownAfter + switch { + case strings.Contains(path, "/actions"): + scrolls.Add(1) + jsonResponse(w, map[string]interface{}{"status": 0}) + case strings.HasSuffix(path, "/screenshot"): + picture := "loading" + if filled { + picture = "list" + } + jsonResponse(w, map[string]interface{}{"value": base64.StdEncoding.EncodeToString([]byte(picture))}) + case strings.HasSuffix(path, "/source"): + row := `` + if filled { + row = `` + } + jsonResponse(w, map[string]interface{}{ + "value": ` + + ` + row + ` + +`, + }) + case strings.Contains(path, "/window/size"): + jsonResponse(w, map[string]interface{}{ + "value": map[string]interface{}{"width": 390.0, "height": 844.0}, + }) + default: + jsonResponse(w, map[string]interface{}{"status": 0}) + } + })) +} + +func TestScrollUntilVisibleWithTimeoutWaitsForListThatFillsLate(t *testing.T) { + // The screen does not change for four scrolls (the list is still + // loading), then the target is there. A step with its own timeout looks + // until that timeout, as Maestro does: a still screen is not proof that + // the content has ended. + var scrolls atomic.Int32 + server := fillsLateWDAServer(&scrolls, 4) + defer server.Close() + driver := createTestDriver(server) + + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "Target"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 20000}, + }) + + if !result.Success { + t.Fatalf("expected the target once the list filled, got %q", result.Message) + } + if got := scrolls.Load(); got < 4 { + t.Errorf("expected at least 4 scrolls before the target showed, got %d", got) + } +} + +func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { + // A screen that never changes and a step with its own timeout: the step + // keeps scrolling until the timeout, then fails in Maestro's words. + var scrolls atomic.Int32 + server := fillsLateWDAServer(&scrolls, 1<<30) + defer server.Close() + driver := createTestDriver(server) + + start := time.Now() + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "NonExistent"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 5000}, + }) + + if result.Success { + t.Fatal("expected failure when the element never shows") + } + if elapsed := time.Since(start); elapsed < 5*time.Second { + t.Errorf("gave up after %v, before the 5s timeout", elapsed) + } + if got := scrolls.Load(); got <= 3 { + t.Errorf("expected more scrolls than the no-progress stop's 3, got %d", got) + } + if !strings.Contains(result.Message, "No visible element found") { + t.Errorf("message should be Maestro's, got %q", result.Message) + } +} + // ============================================================================= // autoClickAlertSelector: permission prompts only, not the app's own alerts // ============================================================================= From b4167195d37fa259f7ee3d31a882b1a76fa93d16 Mon Sep 17 00:00:00 2001 From: lvladika Date: Mon, 16 Nov 2026 23:39:20 +0100 Subject: [PATCH 2/9] fix: scrollUntilVisible with a timeout of its own is not cut at the default 20 scrolls On uiautomator2, devicelab and appium a step that sets `timeout:` and no `maxScrolls` is now bounded by its timeout alone, as the iOS drivers have done since 6ef5fc7 and c9fc359 ("the timeout is the budget; a scroll count caps it only when the flow sets one") and as Maestro does. The default 20 scrolls could end such a step before its timeout, before a row that came late could show. A step without a timeout keeps the cap of 20, and a flow's `maxScrolls` still caps a step with one. TestScrollUntilVisibleDefaultMaxScrolls gave its step a 60 s timeout; it now gives none (its 20 mock rounds take about 26 s, inside the default 30 s) and still checks the cap. --- pkg/driver/appium/commands.go | 6 +++ pkg/driver/appium/driver_test.go | 53 ++++++++++++++++++++++++ pkg/driver/devicelab/commands.go | 6 +++ pkg/driver/devicelab/commands_test.go | 44 +++++++++++++++++++- pkg/driver/uiautomator2/commands.go | 6 +++ pkg/driver/uiautomator2/commands_test.go | 44 +++++++++++++++++++- 6 files changed, 155 insertions(+), 4 deletions(-) diff --git a/pkg/driver/appium/commands.go b/pkg/driver/appium/commands.go index 26b4874a..f44ebcb1 100644 --- a/pkg/driver/appium/commands.go +++ b/pkg/driver/appium/commands.go @@ -3,6 +3,7 @@ package appium import ( "context" "fmt" + "math" "sort" "strconv" "strings" @@ -389,6 +390,11 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com maxScrolls := 20 if step.MaxScrolls > 0 { maxScrolls = step.MaxScrolls + } else if core.ScrollLooksUntilTimeout(step) { + // The step's own timeout is its budget, as in Maestro and on the iOS + // drivers: the default count does not cut it short. At about a + // second a scroll, 20 scrolls ended a 30s step at about 20s. + maxScrolls = math.MaxInt } partiallyVisible := false diff --git a/pkg/driver/appium/driver_test.go b/pkg/driver/appium/driver_test.go index 3a9e49d4..dcdd5ff6 100644 --- a/pkg/driver/appium/driver_test.go +++ b/pkg/driver/appium/driver_test.go @@ -3095,3 +3095,56 @@ func TestAppiumScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) t.Errorf("message should be Maestro's, got %q", result.Message) } } + +func TestAppiumScrollUntilVisibleWithTimeoutIsNotCutAtDefaultMaxScrolls(t *testing.T) { + // The target comes on screen after the 21st scroll; until then the list + // moves and the target sits partly below the screen's bottom edge. A step + // with its own timeout scrolls until that timeout, as Maestro does; the + // default of 20 scrolls only caps a step without one. + var scrolls atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + path := r.URL.Path + switch { + case strings.HasSuffix(path, "/source"): + n := scrolls.Load() + top := 2290 + if n >= 21 { + top = 1000 + } + writeJSON(w, map[string]interface{}{ + "value": fmt.Sprintf(` + + + + + +`, n, top, top+100), + }) + case strings.Contains(path, "/actions") && r.Method == "POST": + scrolls.Add(1) + writeJSON(w, map[string]interface{}{"value": nil}) + case strings.Contains(path, "/window/rect"): + writeJSON(w, map[string]interface{}{ + "value": map[string]interface{}{"width": 1080.0, "height": 2340.0, "x": 0.0, "y": 0.0}, + }) + default: + writeJSON(w, map[string]interface{}{"value": nil}) + } + })) + defer server.Close() + driver := createTestAppiumDriver(server) + + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "Target"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 60000}, + }) + + if !result.Success { + t.Fatalf("expected the target after 21 scrolls, got %q", result.Message) + } + if got := scrolls.Load(); got != 21 { + t.Errorf("expected 21 scrolls, got %d", got) + } +} diff --git a/pkg/driver/devicelab/commands.go b/pkg/driver/devicelab/commands.go index fced0161..b6d8ad33 100644 --- a/pkg/driver/devicelab/commands.go +++ b/pkg/driver/devicelab/commands.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "math" "os" "path/filepath" "regexp" @@ -1606,6 +1607,11 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com maxScrolls := 20 if step.MaxScrolls > 0 { maxScrolls = step.MaxScrolls + } else if core.ScrollLooksUntilTimeout(step) { + // The step's own timeout is its budget, as in Maestro and on the iOS + // drivers: the default count does not cut it short. At about a + // second a scroll, 20 scrolls ended a 30s step at about 20s. + maxScrolls = math.MaxInt } timeout := 30 * time.Second if step.TimeoutMs > 0 { diff --git a/pkg/driver/devicelab/commands_test.go b/pkg/driver/devicelab/commands_test.go index c0b02e12..33d1afb4 100644 --- a/pkg/driver/devicelab/commands_test.go +++ b/pkg/driver/devicelab/commands_test.go @@ -190,8 +190,8 @@ func TestScrollUntilVisibleDefaultMaxScrolls(t *testing.T) { step := &flow.ScrollUntilVisibleStep{ Element: flow.Selector{Text: "NonExistent"}, Direction: "down", - BaseStep: flow.BaseStep{TimeoutMs: 60000}, // long timeout - // MaxScrolls not set — defaults to 20 + // No timeout of its own (the default 30s outlasts 20 scrolls here) + // and MaxScrolls not set — defaults to 20 } result := driver.scrollUntilVisible(step) @@ -204,6 +204,46 @@ func TestScrollUntilVisibleDefaultMaxScrolls(t *testing.T) { } } +// growingList is a list that moves on every capture, with the target row +// partly below the screen's bottom edge until the shownAfter'th scroll, and +// fully on screen from then on. A partly visible match is found at once, so +// each round costs only the scroll's settle. +func growingList(scrolls, shownAfter int) string { + top := 2350 + if scrolls >= shownAfter { + top = 1000 + } + return fmt.Sprintf(` + + + + + +`, scrolls, top, top+100) +} + +func TestScrollUntilVisibleWithTimeoutIsNotCutAtDefaultMaxScrolls(t *testing.T) { + // The target comes on screen after the 21st scroll. A step with its own + // timeout scrolls until that timeout, as Maestro does; the default of 20 + // scrolls only caps a step without one. + client := &mockDeviceLabClient{} + client.sourceFunc = func() (string, error) { return growingList(client.scrollCalls, 21), nil } + driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) + + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "Target"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 60000}, + }) + + if !result.Success { + t.Fatalf("expected the target after 21 scrolls, got %q", result.Message) + } + if got := client.scrollCalls; got != 21 { + t.Errorf("expected 21 scrolls, got %d", got) + } +} + // TestScrollStopCriterion covers scrollUntilVisible's stop check, which now // requires the flow's visibility percentage (default: fully inside the // viewport) rather than any 1px overlap — a match half-hidden at the screen diff --git a/pkg/driver/uiautomator2/commands.go b/pkg/driver/uiautomator2/commands.go index 00cf4cd5..e93fcb98 100644 --- a/pkg/driver/uiautomator2/commands.go +++ b/pkg/driver/uiautomator2/commands.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "math" "os" "path/filepath" "strconv" @@ -657,6 +658,11 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com maxScrolls := 20 if step.MaxScrolls > 0 { maxScrolls = step.MaxScrolls + } else if core.ScrollLooksUntilTimeout(step) { + // The step's own timeout is its budget, as in Maestro and on the iOS + // drivers: the default count does not cut it short. At about a + // second a scroll, 20 scrolls ended a 30s step at about 20s. + maxScrolls = math.MaxInt } timeout := 30 * time.Second if step.TimeoutMs > 0 { diff --git a/pkg/driver/uiautomator2/commands_test.go b/pkg/driver/uiautomator2/commands_test.go index 3d893b91..5bd4e75a 100644 --- a/pkg/driver/uiautomator2/commands_test.go +++ b/pkg/driver/uiautomator2/commands_test.go @@ -4147,8 +4147,8 @@ func TestScrollUntilVisibleDefaultMaxScrolls(t *testing.T) { step := &flow.ScrollUntilVisibleStep{ Element: flow.Selector{Text: "NonExistent"}, Direction: "down", - BaseStep: flow.BaseStep{TimeoutMs: 60000}, // long timeout so maxScrolls is the limit - // MaxScrolls not set — defaults to 20 + // No timeout of its own (the default 30s outlasts 20 scrolls here) + // and MaxScrolls not set — defaults to 20 } result := driver.scrollUntilVisible(step) @@ -4162,6 +4162,46 @@ func TestScrollUntilVisibleDefaultMaxScrolls(t *testing.T) { } } +// growingList is a list that moves on every capture, with the target row +// partly below the screen's bottom edge until the shownAfter'th scroll, and +// fully on screen from then on. A partly visible match is found at once, so +// each round costs only the scroll's settle. +func growingList(scrolls, shownAfter int) string { + top := 2350 + if scrolls >= shownAfter { + top = 1000 + } + return fmt.Sprintf(` + + + + + +`, scrolls, top, top+100) +} + +func TestScrollUntilVisibleWithTimeoutIsNotCutAtDefaultMaxScrolls(t *testing.T) { + // The target comes on screen after the 21st scroll. A step with its own + // timeout scrolls until that timeout, as Maestro does; the default of 20 + // scrolls only caps a step without one. + client := &MockUIA2Client{} + client.sourceFunc = func() (string, error) { return growingList(len(client.scrollCalls), 21), nil } + driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) + + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "Target"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 60000}, + }) + + if !result.Success { + t.Fatalf("expected the target after 21 scrolls, got %q", result.Message) + } + if got := len(client.scrollCalls); got != 21 { + t.Errorf("expected 21 scrolls, got %d", got) + } +} + // ============================================================================ // Compile-time interface assertion // ============================================================================ From 6ad34f9e6b6ba7801593ed0cdb813265767cff0c Mon Sep 17 00:00:00 2001 From: lvladika Date: Tue, 17 Nov 2026 19:24:44 +0100 Subject: [PATCH 3/9] fix: scrollUntilVisible without a timeout of its own keeps its message at the deadline The after-loop branch that fails in Maestro's words when the deadline has passed was not gated: a step with no timeout of its own that ran into the default 30 s (a list that keeps moving, a maxScrolls it never reaches) got "No visible element found: ... (scrolling down until the 30000ms timeout)" where it had its driver's own message ("Element not found after 100 scrolls" on uiautomator2 and devicelab, "... not visible after scrolling down" on devicelab iOS, whose cause also moved it from an assertion to element_not_found, and appium's partly visible case from unknown to timeout). That branch now takes the same predicate as the early stop (core.ScrollLooksUntilTimeout), so a step without a timeout of its own keeps its message and its cause exactly when its loop ends at the deadline or at maxScrolls, in all six drivers. Each driver has a test of such a step at the deadline; it passes on main, fails before this change and passes after it. --- pkg/driver/appium/commands.go | 4 +- pkg/driver/appium/driver_test.go | 58 ++++++++++++++++++ pkg/driver/devicelab/commands.go | 4 +- pkg/driver/devicelab/commands_test.go | 26 ++++++++ pkg/driver/devicelab_ios/actions.go | 8 ++- pkg/driver/devicelab_ios/actions_test.go | 32 ++++++++++ pkg/driver/devicelab_ios_legacy/commands.go | 4 +- .../devicelab_ios_legacy/settle_test.go | 23 +++++++ pkg/driver/uiautomator2/commands.go | 4 +- pkg/driver/uiautomator2/commands_test.go | 26 ++++++++ pkg/driver/wda/commands.go | 4 +- pkg/driver/wda/commands_test.go | 60 +++++++++++++++++++ 12 files changed, 245 insertions(+), 8 deletions(-) diff --git a/pkg/driver/appium/commands.go b/pkg/driver/appium/commands.go index f44ebcb1..61482e58 100644 --- a/pkg/driver/appium/commands.go +++ b/pkg/driver/appium/commands.go @@ -507,8 +507,10 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com if hanging != nil { return successResult("Element visible (scrolls ran out, partly past its scroll container)", hanging) } + // A step with its own timeout that ran out of it fails in Maestro's + // words; one without keeps its message, however its loop ended. reason := "" - if !time.Now().Before(deadline) { + if core.ScrollLooksUntilTimeout(step) && !time.Now().Before(deadline) { reason = core.ScrollTimeoutMessage(step.Element.Describe(), direction, timeout) } if partiallyVisible { diff --git a/pkg/driver/appium/driver_test.go b/pkg/driver/appium/driver_test.go index dcdd5ff6..90d5d608 100644 --- a/pkg/driver/appium/driver_test.go +++ b/pkg/driver/appium/driver_test.go @@ -3096,6 +3096,64 @@ func TestAppiumScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) } } +// movingServer is an Appium server whose page source moves on every scroll, +// a list that keeps going without the target. It counts the scrolls (W3C +// actions). +func movingServer(scrolls *atomic.Int32) *httptest.Server { + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + path := r.URL.Path + switch { + case strings.HasSuffix(path, "/source"): + y := 100 + scrolls.Load() + writeJSON(w, map[string]interface{}{ + "value": fmt.Sprintf(` + + + + +`, y, y+50), + }) + case strings.Contains(path, "/actions") && r.Method == "POST": + scrolls.Add(1) + writeJSON(w, map[string]interface{}{"value": nil}) + case strings.Contains(path, "/window/rect"): + writeJSON(w, map[string]interface{}{ + "value": map[string]interface{}{"width": 1080.0, "height": 2340.0, "x": 0.0, "y": 0.0}, + }) + default: + writeJSON(w, map[string]interface{}{"value": nil}) + } + })) +} + +func TestAppiumScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing.T) { + // A step without a timeout of its own that runs into the default 30s (a + // list that keeps moving, a maxScrolls it never reaches) keeps the message + // it had: Maestro's words are for a step that set its timeout. + var scrolls atomic.Int32 + server := movingServer(&scrolls) + defer server.Close() + driver := createTestAppiumDriver(server) + + start := time.Now() + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "NonExistent"}, + Direction: "down", + MaxScrolls: 1000, + }) + + if result.Success { + t.Fatal("expected failure when the element never shows") + } + if elapsed := time.Since(start); elapsed < 30*time.Second { + t.Fatalf("ended after %v, before the default 30s timeout", elapsed) + } + if result.Message != "element not found after scrolling" || result.Error == nil || result.Error.Error() != "element not found after scrolling" { + t.Errorf("a step without a timeout of its own should keep its message, got %q (%v)", result.Message, result.Error) + } +} + func TestAppiumScrollUntilVisibleWithTimeoutIsNotCutAtDefaultMaxScrolls(t *testing.T) { // The target comes on screen after the 21st scroll; until then the list // moves and the target sits partly below the screen's bottom edge. A step diff --git a/pkg/driver/devicelab/commands.go b/pkg/driver/devicelab/commands.go index b6d8ad33..f0d5feca 100644 --- a/pkg/driver/devicelab/commands.go +++ b/pkg/driver/devicelab/commands.go @@ -1753,7 +1753,9 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com if hanging != nil { return successResult("Element visible (scrolls ran out, partly past its scroll container)", hanging) } - if !time.Now().Before(deadline) { + // A step with its own timeout that ran out of it fails in Maestro's + // words; one without keeps its message, however its loop ended. + if core.ScrollLooksUntilTimeout(step) && !time.Now().Before(deadline) { return errorResult(fmt.Errorf("element not found"), core.ScrollTimeoutMessage(step.Element.Describe(), direction, timeout)) } return errorResult(fmt.Errorf("element not found"), fmt.Sprintf("Element not found after %d scrolls", maxScrolls)) diff --git a/pkg/driver/devicelab/commands_test.go b/pkg/driver/devicelab/commands_test.go index 33d1afb4..96b07eb5 100644 --- a/pkg/driver/devicelab/commands_test.go +++ b/pkg/driver/devicelab/commands_test.go @@ -462,3 +462,29 @@ func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { t.Errorf("message should be Maestro's, got %q", result.Message) } } + +func TestScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing.T) { + // A step without a timeout of its own that runs into the default 30s (a + // list that keeps moving, a maxScrolls it never reaches) keeps the message + // it had: Maestro's words are for a step that set its timeout. + captures := 0 + client := &mockDeviceLabClient{sourceFunc: func() (string, error) { return movingList(&captures), nil }} + driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) + + start := time.Now() + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "NonExistent"}, + Direction: "down", + MaxScrolls: 1000, + }) + + if result.Success { + t.Fatal("expected failure when the element never shows") + } + if elapsed := time.Since(start); elapsed < 30*time.Second { + t.Fatalf("ended after %v, before the default 30s timeout", elapsed) + } + if result.Message != "Element not found after 1000 scrolls" || result.Error == nil || result.Error.Error() != "element not found" { + t.Errorf("a step without a timeout of its own should keep its message, got %q (%v)", result.Message, result.Error) + } +} diff --git a/pkg/driver/devicelab_ios/actions.go b/pkg/driver/devicelab_ios/actions.go index cf4e8d24..9971139a 100644 --- a/pkg/driver/devicelab_ios/actions.go +++ b/pkg/driver/devicelab_ios/actions.go @@ -364,9 +364,11 @@ func (d *Driver) scrollUntilVisible(s *flow.ScrollUntilVisibleStep) *core.Comman } } } - if time.Now().After(deadline) { - // A "not found" cause, as the other drivers give, files the failure - // as element_not_found, which is what Maestro's ElementNotFound is. + if core.ScrollLooksUntilTimeout(s) && time.Now().After(deadline) { + // A step with its own timeout that ran out of it fails in Maestro's + // words, with a "not found" cause, as the other drivers give: the + // report files it as element_not_found, which is what Maestro's + // ElementNotFound is. One without keeps its message. err := fmt.Errorf("%s not found after scrolling %s", describe(s.Element), direction) return core.ErrorResult(err, core.ScrollTimeoutMessage(describe(s.Element), direction, timeout)) } diff --git a/pkg/driver/devicelab_ios/actions_test.go b/pkg/driver/devicelab_ios/actions_test.go index b86262c2..ff669275 100644 --- a/pkg/driver/devicelab_ios/actions_test.go +++ b/pkg/driver/devicelab_ios/actions_test.go @@ -3,6 +3,7 @@ package devicelab_ios import ( "bytes" "encoding/base64" + "fmt" "image" "image/color" "image/png" @@ -291,6 +292,37 @@ func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { } } +func TestScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing.T) { + // A step without a timeout of its own that runs into the default 30s (a + // screen that moves after every scroll) keeps the message and the cause + // it had: Maestro's words are for a step that set its timeout. + var scrolls atomic.Int32 + d, _, _ := newTestDriver(t, func(cmd string, _ Args) (*Response, error) { + switch cmd { + case "act": + scrolls.Add(1) + case "find": + return tree(), nil + case "settle": + return ok(&Payload{ScreenHash: fmt.Sprintf("list after %d scrolls", scrolls.Load())}), nil + } + return ok(nil), nil + }) + sel := flow.Selector{Text: "Missing"} + start := time.Now() + res := d.Execute(&flow.ScrollUntilVisibleStep{Element: sel}) + if res.Success { + t.Fatal("found an element that is not there") + } + if elapsed := time.Since(start); elapsed < 30*time.Second { + t.Fatalf("ended after %v, before the default 30s timeout", elapsed) + } + want := fmt.Sprintf("%s not visible after scrolling down", describe(sel)) + if res.Message != want || res.Error == nil || res.Error.Error() != want { + t.Errorf("a step without a timeout of its own should keep %q, got %q (%v)", want, res.Message, res.Error) + } +} + func TestScrollUntilVisibleLimits(t *testing.T) { d, fa, _ := newTestDriver(t, screenOf()) res := d.Execute(&flow.ScrollUntilVisibleStep{Element: flow.Selector{Text: "X"}, MaxScrolls: 2}) diff --git a/pkg/driver/devicelab_ios_legacy/commands.go b/pkg/driver/devicelab_ios_legacy/commands.go index 31d6f366..4df04b80 100644 --- a/pkg/driver/devicelab_ios_legacy/commands.go +++ b/pkg/driver/devicelab_ios_legacy/commands.go @@ -862,7 +862,9 @@ func (d *Driver) handleScrollUntilVisible(s *flow.ScrollUntilVisibleStep) *core. // screen each time and its rows are not where they will stop. d.settleScreen(settleCapMs(deadline)) } - if !time.Now().Before(deadline) { + // A step with its own timeout that ran out of it fails in Maestro's + // words; one without keeps its message, however its loop ended. + if core.ScrollLooksUntilTimeout(s) && !time.Now().Before(deadline) { return core.ErrorResult( fmt.Errorf("element not found after scrolling"), core.ScrollTimeoutMessage(describeSelector(s.Element), direction, timeout), diff --git a/pkg/driver/devicelab_ios_legacy/settle_test.go b/pkg/driver/devicelab_ios_legacy/settle_test.go index 85730d20..44399142 100644 --- a/pkg/driver/devicelab_ios_legacy/settle_test.go +++ b/pkg/driver/devicelab_ios_legacy/settle_test.go @@ -2,6 +2,7 @@ package devicelab_ios_legacy import ( "encoding/json" + "fmt" "net/http" "net/http/httptest" "strings" @@ -195,6 +196,28 @@ func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { } } +// A step without a timeout of its own that runs into the default 30s (a screen +// that moves after every swipe) keeps the message it had: Maestro's words are +// for a step that set its timeout. +func TestScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing.T) { + info := &core.PlatformInfo{ScreenWidth: 400, ScreenHeight: 800} + r := &settleRunner{settled: true, nodesFn: func(drags int) []SnapshotNode { + return []SnapshotNode{{Type: "Other", Label: fmt.Sprintf("row %d", drags), Rect: SnapshotRect{Width: 400, Height: 40}}} + }} + d := r.driver(t, info) + start := time.Now() + res := d.handleScrollUntilVisible(&flow.ScrollUntilVisibleStep{Element: flow.Selector{ID: "missing"}}) + if res.Success { + t.Fatal("found an element that is not there") + } + if elapsed := time.Since(start); elapsed < 30*time.Second { + t.Fatalf("ended after %v, before the default 30s timeout", elapsed) + } + if res.Message != "scroll target not found" || res.Error == nil || res.Error.Error() != "element not found after scrolling" { + t.Errorf("a step without a timeout of its own should keep its message, got %q (%v)", res.Message, res.Error) + } +} + // An index selector picks the Nth match and still needs it on screen. func TestScrollTargetInHonoursIndex(t *testing.T) { d := &Driver{info: &core.PlatformInfo{ScreenWidth: 400, ScreenHeight: 800}} diff --git a/pkg/driver/uiautomator2/commands.go b/pkg/driver/uiautomator2/commands.go index e93fcb98..0585d174 100644 --- a/pkg/driver/uiautomator2/commands.go +++ b/pkg/driver/uiautomator2/commands.go @@ -797,7 +797,9 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com if hanging != nil { return successResult("Element visible (scrolls ran out, partly past its scroll container)", hanging) } - if !time.Now().Before(deadline) { + // A step with its own timeout that ran out of it fails in Maestro's + // words; one without keeps its message, however its loop ended. + if core.ScrollLooksUntilTimeout(step) && !time.Now().Before(deadline) { return errorResult(fmt.Errorf("element not found"), core.ScrollTimeoutMessage(step.Element.Describe(), direction, timeout)) } return errorResult(fmt.Errorf("element not found"), fmt.Sprintf("Element not found after %d scrolls", maxScrolls)) diff --git a/pkg/driver/uiautomator2/commands_test.go b/pkg/driver/uiautomator2/commands_test.go index 5bd4e75a..c89d10a9 100644 --- a/pkg/driver/uiautomator2/commands_test.go +++ b/pkg/driver/uiautomator2/commands_test.go @@ -4627,3 +4627,29 @@ func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { t.Errorf("message should be Maestro's, got %q", result.Message) } } + +func TestScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing.T) { + // A step without a timeout of its own that runs into the default 30s (a + // list that keeps moving, a maxScrolls it never reaches) keeps the message + // it had: Maestro's words are for a step that set its timeout. + captures := 0 + client := &MockUIA2Client{sourceFunc: func() (string, error) { return movingList(&captures), nil }} + driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) + + start := time.Now() + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "NonExistent"}, + Direction: "down", + MaxScrolls: 1000, + }) + + if result.Success { + t.Fatal("expected failure when the element never shows") + } + if elapsed := time.Since(start); elapsed < 30*time.Second { + t.Fatalf("ended after %v, before the default 30s timeout", elapsed) + } + if result.Message != "Element not found after 1000 scrolls" || result.Error == nil || result.Error.Error() != "element not found" { + t.Errorf("a step without a timeout of its own should keep its message, got %q (%v)", result.Message, result.Error) + } +} diff --git a/pkg/driver/wda/commands.go b/pkg/driver/wda/commands.go index ee8e9202..bdd3986f 100644 --- a/pkg/driver/wda/commands.go +++ b/pkg/driver/wda/commands.go @@ -1143,7 +1143,9 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com if shown != nil { return successResult("Element visible after scrolling (scrolls ran out before centring)", shown) } - if !time.Now().Before(deadline) { + // A step with its own timeout that ran out of it fails in Maestro's + // words; one without keeps its message, however its loop ended. + if core.ScrollLooksUntilTimeout(step) && !time.Now().Before(deadline) { return errorResult(fmt.Errorf("element not found after scrolling"), core.ScrollTimeoutMessage(selectorDesc(step.Element), direction, timeout)) } return errorResult(fmt.Errorf("element not found after scrolling"), fmt.Sprintf("Element not found: %s", selectorDesc(step.Element))) diff --git a/pkg/driver/wda/commands_test.go b/pkg/driver/wda/commands_test.go index 31004b0b..389f0b10 100644 --- a/pkg/driver/wda/commands_test.go +++ b/pkg/driver/wda/commands_test.go @@ -5369,6 +5369,66 @@ func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { } } +// movingWDAServer is a WebDriverAgent whose screen moves on every scroll, +// a list that keeps going without the target. It counts the scrolls (W3C +// actions). +func movingWDAServer(scrolls *atomic.Int32) *httptest.Server { + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + path := r.URL.Path + switch { + case strings.Contains(path, "/actions"): + scrolls.Add(1) + jsonResponse(w, map[string]interface{}{"status": 0}) + case strings.HasSuffix(path, "/screenshot"): + picture := fmt.Sprintf("list after %d scrolls", scrolls.Load()) + jsonResponse(w, map[string]interface{}{"value": base64.StdEncoding.EncodeToString([]byte(picture))}) + case strings.HasSuffix(path, "/source"): + jsonResponse(w, map[string]interface{}{ + "value": fmt.Sprintf(` + + + +`, 100+scrolls.Load()), + }) + case strings.Contains(path, "/window/size"): + jsonResponse(w, map[string]interface{}{ + "value": map[string]interface{}{"width": 390.0, "height": 844.0}, + }) + default: + jsonResponse(w, map[string]interface{}{"status": 0}) + } + })) +} + +func TestScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing.T) { + // A step without a timeout of its own that runs into the default 30s (a + // list that keeps moving) keeps the message it had: Maestro's words are + // for a step that set its timeout. + var scrolls atomic.Int32 + server := movingWDAServer(&scrolls) + defer server.Close() + driver := createTestDriver(server) + + step := &flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "NonExistent"}, + Direction: "down", + } + start := time.Now() + result := driver.scrollUntilVisible(step) + + if result.Success { + t.Fatal("expected failure when the element never shows") + } + if elapsed := time.Since(start); elapsed < 30*time.Second { + t.Fatalf("ended after %v, before the default 30s timeout", elapsed) + } + want := fmt.Sprintf("Element not found: %s", selectorDesc(step.Element)) + if result.Message != want || result.Error == nil || result.Error.Error() != "element not found after scrolling" { + t.Errorf("a step without a timeout of its own should keep its message %q, got %q (%v)", want, result.Message, result.Error) + } +} + // ============================================================================= // autoClickAlertSelector: permission prompts only, not the app's own alerts // ============================================================================= From af2185850a2a759ca2afb47e7aeef65546239154 Mon Sep 17 00:00:00 2001 From: lvladika Date: Tue, 17 Nov 2026 19:26:38 +0100 Subject: [PATCH 4/9] fix: scrollUntilVisible stops when the run is cancelled A step with a timeout of its own looks until that timeout on a still screen, so a run cancelled meanwhile (a runFlow's timeout, a caller's cancel) made the uiautomator2, devicelab and wda drivers swipe on until the step's timeout: 17 swipes in 5 s for `timeout: 5000`. Their loops now check the run's context at the start of each round, as the appium driver's does, and end with the cancel as the cause. A cancel that comes during a round still lets that round's swipe go out; the step ends before the next one. The devicelab iOS drivers already stop there: devicelab iOS checks the context and ends with "not visible after scrolling", and the legacy driver's requests carry it, so its next one fails with the aborted request as the cause. This check is the one exit not gated on the step's own timeout: a step without one stops at the next round too, where it went on to the no-progress check, its cap or the default 30 s. --- pkg/driver/devicelab/commands.go | 5 ++++ pkg/driver/devicelab/commands_test.go | 28 +++++++++++++++++++++ pkg/driver/uiautomator2/commands.go | 5 ++++ pkg/driver/uiautomator2/commands_test.go | 28 +++++++++++++++++++++ pkg/driver/wda/commands.go | 5 ++++ pkg/driver/wda/commands_test.go | 32 ++++++++++++++++++++++++ 6 files changed, 103 insertions(+) diff --git a/pkg/driver/devicelab/commands.go b/pkg/driver/devicelab/commands.go index f0d5feca..21f0cf08 100644 --- a/pkg/driver/devicelab/commands.go +++ b/pkg/driver/devicelab/commands.go @@ -1669,6 +1669,11 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com for i := 0; i < maxScrolls && time.Now().Before(deadline); i++ { wasHanging := hanging hanging = nil + // A cancelled run stops here: with a timeout of its own the step + // would otherwise swipe on a still screen until that timeout. + if err := d.parentContext().Err(); err != nil { + return errorResult(fmt.Errorf("scroll cancelled: %w", err), "") + } _, info, err := d.findElement(step.Element, true, 1000) if err == nil && info != nil { if decided, done := centering.Check(step.CenterElement, info.Bounds, direction, width, height); decided { diff --git a/pkg/driver/devicelab/commands_test.go b/pkg/driver/devicelab/commands_test.go index 96b07eb5..d7445915 100644 --- a/pkg/driver/devicelab/commands_test.go +++ b/pkg/driver/devicelab/commands_test.go @@ -488,3 +488,31 @@ func TestScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing t.Errorf("a step without a timeout of its own should keep its message, got %q (%v)", result.Message, result.Error) } } + +func TestScrollUntilVisibleStopsWhenRunIsCancelled(t *testing.T) { + // The run is cancelled (a runFlow's timeout, a caller's cancel) while a + // step with its own timeout looks on a still screen: it stops at once + // instead of swiping on until that timeout. + client := &mockDeviceLabClient{sourceFunc: func() (string, error) { return fillsLateSource(0, 1), nil }} + driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) + ctx, cancel := context.WithCancel(context.Background()) + cancel() + driver.SetContext(ctx) + + start := time.Now() + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "NonExistent"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 5000}, + }) + + if result.Success || !errors.Is(result.Error, context.Canceled) { + t.Fatalf("expected the cancel as the cause, got %q (%v)", result.Message, result.Error) + } + if got := client.scrollCalls; got != 0 { + t.Errorf("expected no scroll after the cancel, got %d", got) + } + if elapsed := time.Since(start); elapsed > time.Second { + t.Errorf("stopped after %v; want at once", elapsed) + } +} diff --git a/pkg/driver/uiautomator2/commands.go b/pkg/driver/uiautomator2/commands.go index 0585d174..b4444299 100644 --- a/pkg/driver/uiautomator2/commands.go +++ b/pkg/driver/uiautomator2/commands.go @@ -715,6 +715,11 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com for i := 0; i < maxScrolls && time.Now().Before(deadline); i++ { wasHanging := hanging hanging = nil + // A cancelled run stops here: with a timeout of its own the step + // would otherwise swipe on a still screen until that timeout. + if err := d.parentContext().Err(); err != nil { + return errorResult(fmt.Errorf("scroll cancelled: %w", err), "") + } // Try to find element (short timeout - includes page source fallback) _, info, err := d.findElement(step.Element, true, 1000) if err == nil && info != nil { diff --git a/pkg/driver/uiautomator2/commands_test.go b/pkg/driver/uiautomator2/commands_test.go index c89d10a9..ef5121c7 100644 --- a/pkg/driver/uiautomator2/commands_test.go +++ b/pkg/driver/uiautomator2/commands_test.go @@ -4653,3 +4653,31 @@ func TestScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing t.Errorf("a step without a timeout of its own should keep its message, got %q (%v)", result.Message, result.Error) } } + +func TestScrollUntilVisibleStopsWhenRunIsCancelled(t *testing.T) { + // The run is cancelled (a runFlow's timeout, a caller's cancel) while a + // step with its own timeout looks on a still screen: it stops at once + // instead of swiping on until that timeout. + client := &MockUIA2Client{sourceFunc: func() (string, error) { return fillsLateSource(0, 1), nil }} + driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) + ctx, cancel := context.WithCancel(context.Background()) + cancel() + driver.SetContext(ctx) + + start := time.Now() + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "NonExistent"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 5000}, + }) + + if result.Success || !errors.Is(result.Error, context.Canceled) { + t.Fatalf("expected the cancel as the cause, got %q (%v)", result.Message, result.Error) + } + if got := len(client.scrollCalls); got != 0 { + t.Errorf("expected no scroll after the cancel, got %d", got) + } + if elapsed := time.Since(start); elapsed > time.Second { + t.Errorf("stopped after %v; want at once", elapsed) + } +} diff --git a/pkg/driver/wda/commands.go b/pkg/driver/wda/commands.go index bdd3986f..cea31c59 100644 --- a/pkg/driver/wda/commands.go +++ b/pkg/driver/wda/commands.go @@ -1083,6 +1083,11 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com var shown *core.ElementInfo for i := 0; i < maxScrolls && time.Now().Before(deadline); i++ { + // A cancelled run stops here: with a timeout of its own the step + // would otherwise swipe on a still screen until that timeout. + if err := d.parentContext().Err(); err != nil { + return errorResult(fmt.Errorf("scroll cancelled: %w", err), "") + } info, err := d.findElement(step.Element, true, 1000) if err == nil && info != nil { // Found in the tree is not enough: an element half-hidden behind diff --git a/pkg/driver/wda/commands_test.go b/pkg/driver/wda/commands_test.go index 389f0b10..2fa7f20f 100644 --- a/pkg/driver/wda/commands_test.go +++ b/pkg/driver/wda/commands_test.go @@ -1,8 +1,10 @@ package wda import ( + "context" "encoding/base64" "encoding/json" + "errors" "fmt" "io" "net/http" @@ -5429,6 +5431,36 @@ func TestScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing } } +func TestScrollUntilVisibleStopsWhenRunIsCancelled(t *testing.T) { + // The run is cancelled (a runFlow's timeout, a caller's cancel) while a + // step with its own timeout looks on a still screen: it stops at once + // instead of swiping on until that timeout. + var scrolls atomic.Int32 + server := fillsLateWDAServer(&scrolls, 1<<30) + defer server.Close() + driver := createTestDriver(server) + ctx, cancel := context.WithCancel(context.Background()) + cancel() + driver.SetContext(ctx) + + start := time.Now() + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "NonExistent"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 5000}, + }) + + if result.Success || !errors.Is(result.Error, context.Canceled) { + t.Fatalf("expected the cancel as the cause, got %q (%v)", result.Message, result.Error) + } + if got := scrolls.Load(); got != 0 { + t.Errorf("expected no scroll after the cancel, got %d", got) + } + if elapsed := time.Since(start); elapsed > time.Second { + t.Errorf("stopped after %v; want at once", elapsed) + } +} + // ============================================================================= // autoClickAlertSelector: permission prompts only, not the app's own alerts // ============================================================================= From c053ce4989fd2c8c7d2091676b295acc2890fc1e Mon Sep 17 00:00:00 2001 From: lvladika Date: Tue, 17 Nov 2026 19:29:33 +0100 Subject: [PATCH 5/9] test: scrollUntilVisible with a timeout still takes an uncentred element at the end of the content A step with its own timeout keeps looking after the screen stops moving only while its element was never found: an element on screen that centerElement cannot bring to the middle (the last row of a list) is still taken there. A test of that in uiautomator2, devicelab, wda and devicelab iOS, each asserting the end-of-content exit and its scroll count; they fail when the timeout's gate is put before that answer. The growingList comment said a partly visible match ends the find at once; in these mocks every round spends the find's 1s budget and the scroll's settle, about 1.35s. --- pkg/driver/devicelab/commands_test.go | 36 ++++++++++++++++- pkg/driver/devicelab_ios/actions_test.go | 27 +++++++++++++ pkg/driver/uiautomator2/commands_test.go | 36 ++++++++++++++++- pkg/driver/wda/commands_test.go | 49 ++++++++++++++++++++++++ 4 files changed, 144 insertions(+), 4 deletions(-) diff --git a/pkg/driver/devicelab/commands_test.go b/pkg/driver/devicelab/commands_test.go index d7445915..131b5a01 100644 --- a/pkg/driver/devicelab/commands_test.go +++ b/pkg/driver/devicelab/commands_test.go @@ -206,8 +206,10 @@ func TestScrollUntilVisibleDefaultMaxScrolls(t *testing.T) { // growingList is a list that moves on every capture, with the target row // partly below the screen's bottom edge until the shownAfter'th scroll, and -// fully on screen from then on. A partly visible match is found at once, so -// each round costs only the scroll's settle. +// fully on screen from then on. Each round spends the find's 1s budget (the +// mock answers no locator, so the find reads the page source at the end of +// it, whether the match is partly visible or not) and the scroll's 300ms +// settle: about 1.35s a round, 28s for 21 scrolls. func growingList(scrolls, shownAfter int) string { top := 2350 if scrolls >= shownAfter { @@ -516,3 +518,33 @@ func TestScrollUntilVisibleStopsWhenRunIsCancelled(t *testing.T) { t.Errorf("stopped after %v; want at once", elapsed) } } + +func TestScrollUntilVisibleWithTimeoutAcceptsUncentredElementAtEndOfContent(t *testing.T) { + // centerElement on the last row of a list: it is on screen but can never + // reach the middle, and the screen stops moving. A step with its own + // timeout still takes it at the end of the content, as one without does: + // only the not-found failure waits for the timeout. + client := &mockDeviceLabClient{sourceFunc: func() (string, error) { + return ` + + + + +`, nil + }} + driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) + + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "Target"}, + Direction: "down", + CenterElement: true, + BaseStep: flow.BaseStep{TimeoutMs: 20000}, + }) + + if !result.Success || !strings.Contains(result.Message, "end of content before centring") { + t.Fatalf("expected the element at the end of the content, got %q (%v)", result.Message, result.Error) + } + if got := client.scrollCalls; got != 2 { + t.Errorf("expected the 2 scrolls that show the end of the content, got %d", got) + } +} diff --git a/pkg/driver/devicelab_ios/actions_test.go b/pkg/driver/devicelab_ios/actions_test.go index ff669275..1f81493f 100644 --- a/pkg/driver/devicelab_ios/actions_test.go +++ b/pkg/driver/devicelab_ios/actions_test.go @@ -817,6 +817,33 @@ func TestScrollUntilVisibleCenterStopsAtEndOfContent(t *testing.T) { } } +func TestScrollUntilVisibleWithTimeoutCenterStopsAtEndOfContent(t *testing.T) { + // The last item of a list cannot reach the middle and the screen stops + // moving: a step with its own timeout still takes the visible element at + // the end of the content, as one without does; only the not-found + // failure waits for the timeout. + d, fa, _ := newTestDriver(t, func(cmd string, _ Args) (*Response, error) { + switch cmd { + case "find": + return tree(node(1, "StaticText", "Last", 84, 770, 82, 20)), nil + case "settle": + return ok(&Payload{ScreenHash: "same"}), nil + } + return ok(nil), nil + }) + res := d.Execute(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "Last"}, + CenterElement: true, + BaseStep: flow.BaseStep{TimeoutMs: 20000}, + }) + if !res.Success || res.Message != "element visible at the end of the content" { + t.Fatalf("res = %+v, want the element at the end of the content", res) + } + if n := len(fa.sent("act")); n != 3 { + t.Errorf("scrolls = %d, want the 3 that show the end of the content", n) + } +} + func TestScrollUsesMaestroDefaultDuration(t *testing.T) { d, fa, _ := newTestDriver(t, screenOf()) if res := d.Execute(&flow.ScrollStep{Direction: "down"}); !res.Success { diff --git a/pkg/driver/uiautomator2/commands_test.go b/pkg/driver/uiautomator2/commands_test.go index ef5121c7..f96535b1 100644 --- a/pkg/driver/uiautomator2/commands_test.go +++ b/pkg/driver/uiautomator2/commands_test.go @@ -4164,8 +4164,10 @@ func TestScrollUntilVisibleDefaultMaxScrolls(t *testing.T) { // growingList is a list that moves on every capture, with the target row // partly below the screen's bottom edge until the shownAfter'th scroll, and -// fully on screen from then on. A partly visible match is found at once, so -// each round costs only the scroll's settle. +// fully on screen from then on. Each round spends the find's 1s budget (the +// mock answers no locator, so the find reads the page source at the end of +// it, whether the match is partly visible or not) and the scroll's 300ms +// settle: about 1.35s a round, 28s for 21 scrolls. func growingList(scrolls, shownAfter int) string { top := 2350 if scrolls >= shownAfter { @@ -4681,3 +4683,33 @@ func TestScrollUntilVisibleStopsWhenRunIsCancelled(t *testing.T) { t.Errorf("stopped after %v; want at once", elapsed) } } + +func TestScrollUntilVisibleWithTimeoutAcceptsUncentredElementAtEndOfContent(t *testing.T) { + // centerElement on the last row of a list: it is on screen but can never + // reach the middle, and the screen stops moving. A step with its own + // timeout still takes it at the end of the content, as one without does: + // only the not-found failure waits for the timeout. + client := &MockUIA2Client{sourceFunc: func() (string, error) { + return ` + + + + +`, nil + }} + driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) + + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "Target"}, + Direction: "down", + CenterElement: true, + BaseStep: flow.BaseStep{TimeoutMs: 20000}, + }) + + if !result.Success || !strings.Contains(result.Message, "end of content before centring") { + t.Fatalf("expected the element at the end of the content, got %q (%v)", result.Message, result.Error) + } + if got := len(client.scrollCalls); got != 2 { + t.Errorf("expected the 2 scrolls that show the end of the content, got %d", got) + } +} diff --git a/pkg/driver/wda/commands_test.go b/pkg/driver/wda/commands_test.go index 2fa7f20f..b0f5184d 100644 --- a/pkg/driver/wda/commands_test.go +++ b/pkg/driver/wda/commands_test.go @@ -5461,6 +5461,55 @@ func TestScrollUntilVisibleStopsWhenRunIsCancelled(t *testing.T) { } } +func TestScrollUntilVisibleWithTimeoutAcceptsUncentredElementAtEndOfContent(t *testing.T) { + // centerElement on the last row of a list: it is on screen but can never + // reach the middle, and the screen stops moving. A step with its own + // timeout still takes it at the end of the content, as one without does: + // only the not-found failure waits for the timeout. + var scrolls atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + path := r.URL.Path + switch { + case strings.Contains(path, "/actions"): + scrolls.Add(1) + jsonResponse(w, map[string]interface{}{"status": 0}) + case strings.HasSuffix(path, "/screenshot"): + jsonResponse(w, map[string]interface{}{"value": base64.StdEncoding.EncodeToString([]byte("end of the list"))}) + case strings.HasSuffix(path, "/source"): + jsonResponse(w, map[string]interface{}{ + "value": ` + + + +`, + }) + case strings.Contains(path, "/window/size"): + jsonResponse(w, map[string]interface{}{ + "value": map[string]interface{}{"width": 390.0, "height": 844.0}, + }) + default: + jsonResponse(w, map[string]interface{}{"status": 0}) + } + })) + defer server.Close() + driver := createTestDriver(server) + + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "Target"}, + Direction: "down", + CenterElement: true, + BaseStep: flow.BaseStep{TimeoutMs: 20000}, + }) + + if !result.Success || !strings.Contains(result.Message, "end of content before centring") { + t.Fatalf("expected the element at the end of the content, got %q (%v)", result.Message, result.Error) + } + if got := scrolls.Load(); got != 3 { + t.Errorf("expected the 3 scrolls that show the end of the content, got %d", got) + } +} + // ============================================================================= // autoClickAlertSelector: permission prompts only, not the app's own alerts // ============================================================================= From fb652e13bcec6bb27f2f8a002908dbc658466413 Mon Sep 17 00:00:00 2001 From: lvladika Date: Tue, 17 Nov 2026 19:54:09 +0100 Subject: [PATCH 6/9] test: scrollUntilVisible with a timeout still takes a row partly past its list's edge at the end of the content The end-of-content answer for a row hanging past its scroll container's leading edge is reached when the find did not see the row while the list still coasted and the captures after it show the list at rest, the row hanging. A test of it with a timeout set in uiautomator2 and devicelab: the mock answers each round's first read (the find's) with the row not in yet for two rounds, and every other read with the list at rest. It passes on main and fails when the timeout's gate is put before that answer (the step then ends a scroll later, on another exit). --- pkg/driver/devicelab/scroll_container_test.go | 35 +++++++++++++++++++ .../uiautomator2/scroll_container_test.go | 35 +++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/pkg/driver/devicelab/scroll_container_test.go b/pkg/driver/devicelab/scroll_container_test.go index 87197898..f9107b18 100644 --- a/pkg/driver/devicelab/scroll_container_test.go +++ b/pkg/driver/devicelab/scroll_container_test.go @@ -2,6 +2,7 @@ package devicelab import ( "fmt" + "strings" "testing" "github.com/devicelab-dev/maestro-runner/pkg/core" @@ -73,6 +74,40 @@ func TestScrollUntilVisibleAcceptsHangingRowAtListEnd(t *testing.T) { } } +// A step with its own timeout still takes a row partly past its list's edge +// at the end of the content: only the not-found failure waits for the +// timeout. The find's look of each round comes first; while the list still +// coasted (the first two rounds) it did not see the row, and the captures +// after it show the list at rest, the row hanging, three times the same. +func TestScrollUntilVisibleWithTimeoutAcceptsHangingRowAtEndOfContent(t *testing.T) { + client := &mockDeviceLabClient{} + round, reads := -1, 0 + client.sourceFunc = func() (string, error) { + if n := client.scrollCalls; n != round { + round, reads = n, 0 + } + reads++ + if reads == 1 && round < 2 { + return sheetRow(2420, 2500), nil // the find's look: the row not in yet + } + return sheetRow(2295, 2400), nil // at rest: the row hangs past the list's edge + } + driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) + + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "Target"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 20000}, + }) + + if !result.Success || !strings.Contains(result.Message, "end of content, partly past its scroll container") { + t.Fatalf("expected the row at the end of the content, got %q (%v)", result.Message, result.Error) + } + if got := client.scrollCalls; got != 2 { + t.Errorf("expected the 2 scrolls that show the end of the content, got %d", got) + } +} + // Only the leading edge counts: scrolling up can't bring in a row that hangs // past the list's bottom, so it is accepted at once, as before. func TestScrollUntilVisibleUpIgnoresOverhangAtTheBottom(t *testing.T) { diff --git a/pkg/driver/uiautomator2/scroll_container_test.go b/pkg/driver/uiautomator2/scroll_container_test.go index a764db03..c4a5c9ff 100644 --- a/pkg/driver/uiautomator2/scroll_container_test.go +++ b/pkg/driver/uiautomator2/scroll_container_test.go @@ -2,6 +2,7 @@ package uiautomator2 import ( "fmt" + "strings" "testing" "github.com/devicelab-dev/maestro-runner/pkg/core" @@ -73,6 +74,40 @@ func TestScrollUntilVisibleAcceptsHangingRowAtListEnd(t *testing.T) { } } +// A step with its own timeout still takes a row partly past its list's edge +// at the end of the content: only the not-found failure waits for the +// timeout. The find's look of each round comes first; while the list still +// coasted (the first two rounds) it did not see the row, and the captures +// after it show the list at rest, the row hanging, three times the same. +func TestScrollUntilVisibleWithTimeoutAcceptsHangingRowAtEndOfContent(t *testing.T) { + client := &MockUIA2Client{} + round, reads := -1, 0 + client.sourceFunc = func() (string, error) { + if n := len(client.scrollCalls); n != round { + round, reads = n, 0 + } + reads++ + if reads == 1 && round < 2 { + return sheetRow(2420, 2500), nil // the find's look: the row not in yet + } + return sheetRow(2295, 2400), nil // at rest: the row hangs past the list's edge + } + driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) + + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "Target"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 20000}, + }) + + if !result.Success || !strings.Contains(result.Message, "end of content, partly past its scroll container") { + t.Fatalf("expected the row at the end of the content, got %q (%v)", result.Message, result.Error) + } + if got := len(client.scrollCalls); got != 2 { + t.Errorf("expected the 2 scrolls that show the end of the content, got %d", got) + } +} + // Only the leading edge counts: scrolling up can't bring in a row that hangs // past the list's bottom, so it is accepted at once, as before. func TestScrollUntilVisibleUpIgnoresOverhangAtTheBottom(t *testing.T) { From 691fd012640c86dc60ca4106a18c525a0ed5f8ec Mon Sep 17 00:00:00 2001 From: lvladika Date: Tue, 17 Nov 2026 19:54:23 +0100 Subject: [PATCH 7/9] docs: the scroll cap's comment drops a figure nobody measured The comment beside the lifted 20-scroll cap said 20 scrolls ended a 30s step at about 20s. No run measured that: a report's "after 20 scrolls" prints maxScrolls whatever ended the loop, and a round's time varies with the device and the screen. The comment now says only that the default count could end a step before its own timeout, in uiautomator2, devicelab and appium. No code change. --- pkg/driver/appium/commands.go | 4 ++-- pkg/driver/devicelab/commands.go | 4 ++-- pkg/driver/uiautomator2/commands.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/driver/appium/commands.go b/pkg/driver/appium/commands.go index 61482e58..34cb9f71 100644 --- a/pkg/driver/appium/commands.go +++ b/pkg/driver/appium/commands.go @@ -392,8 +392,8 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com maxScrolls = step.MaxScrolls } else if core.ScrollLooksUntilTimeout(step) { // The step's own timeout is its budget, as in Maestro and on the iOS - // drivers: the default count does not cut it short. At about a - // second a scroll, 20 scrolls ended a 30s step at about 20s. + // drivers: the default count, which could end the step before that + // timeout, does not apply. maxScrolls = math.MaxInt } diff --git a/pkg/driver/devicelab/commands.go b/pkg/driver/devicelab/commands.go index 21f0cf08..07e00e31 100644 --- a/pkg/driver/devicelab/commands.go +++ b/pkg/driver/devicelab/commands.go @@ -1609,8 +1609,8 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com maxScrolls = step.MaxScrolls } else if core.ScrollLooksUntilTimeout(step) { // The step's own timeout is its budget, as in Maestro and on the iOS - // drivers: the default count does not cut it short. At about a - // second a scroll, 20 scrolls ended a 30s step at about 20s. + // drivers: the default count, which could end the step before that + // timeout, does not apply. maxScrolls = math.MaxInt } timeout := 30 * time.Second diff --git a/pkg/driver/uiautomator2/commands.go b/pkg/driver/uiautomator2/commands.go index b4444299..dfe6bd15 100644 --- a/pkg/driver/uiautomator2/commands.go +++ b/pkg/driver/uiautomator2/commands.go @@ -660,8 +660,8 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com maxScrolls = step.MaxScrolls } else if core.ScrollLooksUntilTimeout(step) { // The step's own timeout is its budget, as in Maestro and on the iOS - // drivers: the default count does not cut it short. At about a - // second a scroll, 20 scrolls ended a 30s step at about 20s. + // drivers: the default count, which could end the step before that + // timeout, does not apply. maxScrolls = math.MaxInt } timeout := 30 * time.Second From ff6f14896556a2a9bbda478eeef8321140ece6af Mon Sep 17 00:00:00 2001 From: lvladika Date: Wed, 18 Nov 2026 14:16:40 +0100 Subject: [PATCH 8/9] test: scrollUntilVisible's tests stop waiting out its defaults The tests added for a step's own timeout spent about 5 minutes of real time across the driver packages: the six tests of a step without a timeout that runs into the default waited its whole 30s, and the test that the 20-scroll cap does not cut a step with a timeout of its own ran 21 rounds of about 1.3s each on uiautomator2 and devicelab. Both defaults are now package variables in pkg/core, ScrollUntilVisibleDefaultTimeout (30s) and ScrollUntilVisibleDefaultMaxScrolls (20, the cap of uiautomator2, devicelab and appium), which the six drivers read where they had the literals; their values are unchanged. The deadline tests set the default timeout to 1s, the cap tests the cap to 2 with the target after the third scroll, each restored by t.Cleanup (no test in these packages runs in parallel). The still-screen tests with a timeout of their own go from 5s to 3s on uiautomator2, devicelab and appium and to 3.5s on wda: a round that finds nothing costs about 1.3s there (1s on wda), and the test needs one round past the no-progress stop to tell the two apart. The iOS drivers' were already at 1s. --- pkg/core/scrollprogress.go | 10 +++++ pkg/driver/appium/commands.go | 4 +- pkg/driver/appium/driver_test.go | 40 +++++++++++------- pkg/driver/devicelab/commands.go | 4 +- pkg/driver/devicelab/commands_test.go | 41 ++++++++++++------- pkg/driver/devicelab_ios/actions.go | 2 +- pkg/driver/devicelab_ios/actions_test.go | 11 +++-- pkg/driver/devicelab_ios_legacy/commands.go | 2 +- .../devicelab_ios_legacy/settle_test.go | 10 +++-- pkg/driver/uiautomator2/commands.go | 4 +- pkg/driver/uiautomator2/commands_test.go | 41 ++++++++++++------- pkg/driver/wda/commands.go | 2 +- pkg/driver/wda/commands_test.go | 19 ++++++--- 13 files changed, 127 insertions(+), 63 deletions(-) diff --git a/pkg/core/scrollprogress.go b/pkg/core/scrollprogress.go index 817b889f..28c5b1f7 100644 --- a/pkg/core/scrollprogress.go +++ b/pkg/core/scrollprogress.go @@ -76,6 +76,16 @@ func ScrollSignature(capture string) string { return fmt.Sprintf("%016x", h.Sum64()) } +// ScrollUntilVisibleDefaultTimeout is a scrollUntilVisible step's timeout +// when the flow gives it none. It is a variable so that tests can shorten it. +var ScrollUntilVisibleDefaultTimeout = 30 * time.Second + +// ScrollUntilVisibleDefaultMaxScrolls is the scroll count that caps a +// scrollUntilVisible step with neither `maxScrolls:` nor a timeout of its own, +// on the drivers that cap one (uiautomator2, devicelab, appium). It is a +// variable so that tests can shorten it. +var ScrollUntilVisibleDefaultMaxScrolls = 20 + // ScrollLooksUntilTimeout reports whether a scrollUntilVisible step that has // not found its element keeps looking after the surface stops moving: it does // when the flow gave the step its own `timeout:`. That timeout is the step's diff --git a/pkg/driver/appium/commands.go b/pkg/driver/appium/commands.go index 34cb9f71..2ccac45e 100644 --- a/pkg/driver/appium/commands.go +++ b/pkg/driver/appium/commands.go @@ -383,11 +383,11 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com timeout := time.Duration(step.TimeoutMs) * time.Millisecond if timeout <= 0 { - timeout = 30 * time.Second + timeout = core.ScrollUntilVisibleDefaultTimeout } deadline := time.Now().Add(timeout) - maxScrolls := 20 + maxScrolls := core.ScrollUntilVisibleDefaultMaxScrolls if step.MaxScrolls > 0 { maxScrolls = step.MaxScrolls } else if core.ScrollLooksUntilTimeout(step) { diff --git a/pkg/driver/appium/driver_test.go b/pkg/driver/appium/driver_test.go index 90d5d608..fec65357 100644 --- a/pkg/driver/appium/driver_test.go +++ b/pkg/driver/appium/driver_test.go @@ -3070,6 +3070,8 @@ func TestAppiumScrollUntilVisibleWithTimeoutWaitsForListThatFillsLate(t *testing func TestAppiumScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { // A screen that never changes and a step with its own timeout: the step // keeps scrolling until the timeout, then fails in Maestro's words. + // A round that finds nothing costs about 1.3s here, and the no-progress + // stop comes in the third: 3s is the least that tells the two apart. var scrolls atomic.Int32 server := fillsLateServer(&scrolls, 1<<30) defer server.Close() @@ -3079,14 +3081,14 @@ func TestAppiumScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ Element: flow.Selector{Text: "NonExistent"}, Direction: "down", - BaseStep: flow.BaseStep{TimeoutMs: 5000}, + BaseStep: flow.BaseStep{TimeoutMs: 3000}, }) if result.Success { t.Fatal("expected failure when the element never shows") } - if elapsed := time.Since(start); elapsed < 5*time.Second { - t.Errorf("gave up after %v, before the 5s timeout", elapsed) + if elapsed := time.Since(start); elapsed < 3*time.Second { + t.Errorf("gave up after %v, before the 3s timeout", elapsed) } if got := scrolls.Load(); got <= 2 { t.Errorf("expected more scrolls than the no-progress stop's 2, got %d", got) @@ -3128,9 +3130,13 @@ func movingServer(scrolls *atomic.Int32) *httptest.Server { } func TestAppiumScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing.T) { - // A step without a timeout of its own that runs into the default 30s (a + // A step without a timeout of its own that runs into the default timeout (a // list that keeps moving, a maxScrolls it never reaches) keeps the message // it had: Maestro's words are for a step that set its timeout. + // The default is 30s; a second is enough to run into it. + defaultTimeout := core.ScrollUntilVisibleDefaultTimeout + core.ScrollUntilVisibleDefaultTimeout = time.Second + t.Cleanup(func() { core.ScrollUntilVisibleDefaultTimeout = defaultTimeout }) var scrolls atomic.Int32 server := movingServer(&scrolls) defer server.Close() @@ -3146,8 +3152,8 @@ func TestAppiumScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *t if result.Success { t.Fatal("expected failure when the element never shows") } - if elapsed := time.Since(start); elapsed < 30*time.Second { - t.Fatalf("ended after %v, before the default 30s timeout", elapsed) + if elapsed := time.Since(start); elapsed < core.ScrollUntilVisibleDefaultTimeout { + t.Fatalf("ended after %v, before the default timeout", elapsed) } if result.Message != "element not found after scrolling" || result.Error == nil || result.Error.Error() != "element not found after scrolling" { t.Errorf("a step without a timeout of its own should keep its message, got %q (%v)", result.Message, result.Error) @@ -3155,10 +3161,16 @@ func TestAppiumScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *t } func TestAppiumScrollUntilVisibleWithTimeoutIsNotCutAtDefaultMaxScrolls(t *testing.T) { - // The target comes on screen after the 21st scroll; until then the list - // moves and the target sits partly below the screen's bottom edge. A step - // with its own timeout scrolls until that timeout, as Maestro does; the - // default of 20 scrolls only caps a step without one. + // The target comes on screen after the scroll past the default cap; until + // then the list moves and the target sits partly below the screen's bottom + // edge. A step with its own timeout scrolls until that timeout, as Maestro + // does; the default cap only applies to a step without one. + + // The default cap is 20 scrolls; 2 is enough to show that it does not + // apply, and it saves the 20 rounds the real cap would cost. + defaultMaxScrolls := core.ScrollUntilVisibleDefaultMaxScrolls + core.ScrollUntilVisibleDefaultMaxScrolls = 2 + t.Cleanup(func() { core.ScrollUntilVisibleDefaultMaxScrolls = defaultMaxScrolls }) var scrolls atomic.Int32 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") @@ -3167,7 +3179,7 @@ func TestAppiumScrollUntilVisibleWithTimeoutIsNotCutAtDefaultMaxScrolls(t *testi case strings.HasSuffix(path, "/source"): n := scrolls.Load() top := 2290 - if n >= 21 { + if n >= 3 { top = 1000 } writeJSON(w, map[string]interface{}{ @@ -3200,9 +3212,9 @@ func TestAppiumScrollUntilVisibleWithTimeoutIsNotCutAtDefaultMaxScrolls(t *testi }) if !result.Success { - t.Fatalf("expected the target after 21 scrolls, got %q", result.Message) + t.Fatalf("expected the target after 3 scrolls, got %q", result.Message) } - if got := scrolls.Load(); got != 21 { - t.Errorf("expected 21 scrolls, got %d", got) + if got := scrolls.Load(); got != 3 { + t.Errorf("expected 3 scrolls, got %d", got) } } diff --git a/pkg/driver/devicelab/commands.go b/pkg/driver/devicelab/commands.go index 07e00e31..72a215b0 100644 --- a/pkg/driver/devicelab/commands.go +++ b/pkg/driver/devicelab/commands.go @@ -1604,7 +1604,7 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com direction = "down" } - maxScrolls := 20 + maxScrolls := core.ScrollUntilVisibleDefaultMaxScrolls if step.MaxScrolls > 0 { maxScrolls = step.MaxScrolls } else if core.ScrollLooksUntilTimeout(step) { @@ -1613,7 +1613,7 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com // timeout, does not apply. maxScrolls = math.MaxInt } - timeout := 30 * time.Second + timeout := core.ScrollUntilVisibleDefaultTimeout if step.TimeoutMs > 0 { timeout = time.Duration(step.TimeoutMs) * time.Millisecond } diff --git a/pkg/driver/devicelab/commands_test.go b/pkg/driver/devicelab/commands_test.go index 131b5a01..95f64298 100644 --- a/pkg/driver/devicelab/commands_test.go +++ b/pkg/driver/devicelab/commands_test.go @@ -209,7 +209,7 @@ func TestScrollUntilVisibleDefaultMaxScrolls(t *testing.T) { // fully on screen from then on. Each round spends the find's 1s budget (the // mock answers no locator, so the find reads the page source at the end of // it, whether the match is partly visible or not) and the scroll's 300ms -// settle: about 1.35s a round, 28s for 21 scrolls. +// settle: about 1.35s a round. func growingList(scrolls, shownAfter int) string { top := 2350 if scrolls >= shownAfter { @@ -225,11 +225,17 @@ func growingList(scrolls, shownAfter int) string { } func TestScrollUntilVisibleWithTimeoutIsNotCutAtDefaultMaxScrolls(t *testing.T) { - // The target comes on screen after the 21st scroll. A step with its own - // timeout scrolls until that timeout, as Maestro does; the default of 20 - // scrolls only caps a step without one. + // The target comes on screen after the scroll past the default cap. A + // step with its own timeout scrolls until that timeout, as Maestro does; + // the default cap only applies to a step without one. + + // The default cap is 20 scrolls; 2 is enough to show that it does not + // apply, and it saves the 20 rounds the real cap would cost. + defaultMaxScrolls := core.ScrollUntilVisibleDefaultMaxScrolls + core.ScrollUntilVisibleDefaultMaxScrolls = 2 + t.Cleanup(func() { core.ScrollUntilVisibleDefaultMaxScrolls = defaultMaxScrolls }) client := &mockDeviceLabClient{} - client.sourceFunc = func() (string, error) { return growingList(client.scrollCalls, 21), nil } + client.sourceFunc = func() (string, error) { return growingList(client.scrollCalls, 3), nil } driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ @@ -239,10 +245,10 @@ func TestScrollUntilVisibleWithTimeoutIsNotCutAtDefaultMaxScrolls(t *testing.T) }) if !result.Success { - t.Fatalf("expected the target after 21 scrolls, got %q", result.Message) + t.Fatalf("expected the target after 3 scrolls, got %q", result.Message) } - if got := client.scrollCalls; got != 21 { - t.Errorf("expected 21 scrolls, got %d", got) + if got := client.scrollCalls; got != 3 { + t.Errorf("expected 3 scrolls, got %d", got) } } @@ -441,6 +447,9 @@ func TestScrollUntilVisibleWithTimeoutWaitsForListThatFillsLate(t *testing.T) { func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { // A screen that never changes and a step with its own timeout: the step // keeps scrolling until the timeout, then fails in Maestro's words. + // A round that finds nothing costs about 1.3s here (the find's 1s budget + // and the 300ms settle), and the no-progress stop comes in the third: 3s + // is the least that tells the two apart. client := &mockDeviceLabClient{sourceFunc: func() (string, error) { return fillsLateSource(0, 1), nil }} driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) @@ -448,14 +457,14 @@ func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ Element: flow.Selector{Text: "NonExistent"}, Direction: "down", - BaseStep: flow.BaseStep{TimeoutMs: 5000}, + BaseStep: flow.BaseStep{TimeoutMs: 3000}, }) if result.Success { t.Fatal("expected failure when the element never shows") } - if elapsed := time.Since(start); elapsed < 5*time.Second { - t.Errorf("gave up after %v, before the 5s timeout", elapsed) + if elapsed := time.Since(start); elapsed < 3*time.Second { + t.Errorf("gave up after %v, before the 3s timeout", elapsed) } if client.scrollCalls <= 2 { t.Errorf("expected more scrolls than the no-progress stop's 2, got %d", client.scrollCalls) @@ -466,9 +475,13 @@ func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { } func TestScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing.T) { - // A step without a timeout of its own that runs into the default 30s (a + // A step without a timeout of its own that runs into the default timeout (a // list that keeps moving, a maxScrolls it never reaches) keeps the message // it had: Maestro's words are for a step that set its timeout. + // The default is 30s; a second is enough to run into it. + defaultTimeout := core.ScrollUntilVisibleDefaultTimeout + core.ScrollUntilVisibleDefaultTimeout = time.Second + t.Cleanup(func() { core.ScrollUntilVisibleDefaultTimeout = defaultTimeout }) captures := 0 client := &mockDeviceLabClient{sourceFunc: func() (string, error) { return movingList(&captures), nil }} driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) @@ -483,8 +496,8 @@ func TestScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing if result.Success { t.Fatal("expected failure when the element never shows") } - if elapsed := time.Since(start); elapsed < 30*time.Second { - t.Fatalf("ended after %v, before the default 30s timeout", elapsed) + if elapsed := time.Since(start); elapsed < core.ScrollUntilVisibleDefaultTimeout { + t.Fatalf("ended after %v, before the default timeout", elapsed) } if result.Message != "Element not found after 1000 scrolls" || result.Error == nil || result.Error.Error() != "element not found" { t.Errorf("a step without a timeout of its own should keep its message, got %q (%v)", result.Message, result.Error) diff --git a/pkg/driver/devicelab_ios/actions.go b/pkg/driver/devicelab_ios/actions.go index 9971139a..c30f17bd 100644 --- a/pkg/driver/devicelab_ios/actions.go +++ b/pkg/driver/devicelab_ios/actions.go @@ -296,7 +296,7 @@ func (d *Driver) scrollUntilVisible(s *flow.ScrollUntilVisibleStep) *core.Comman return core.ErrorResult(err, err.Error()+" — it currently works on the uiautomator2 driver") } var progress core.ScrollProgress - timeout := 30 * time.Second + timeout := core.ScrollUntilVisibleDefaultTimeout if s.TimeoutMs > 0 { timeout = time.Duration(s.TimeoutMs) * time.Millisecond } diff --git a/pkg/driver/devicelab_ios/actions_test.go b/pkg/driver/devicelab_ios/actions_test.go index 1f81493f..d5d446bc 100644 --- a/pkg/driver/devicelab_ios/actions_test.go +++ b/pkg/driver/devicelab_ios/actions_test.go @@ -13,6 +13,7 @@ import ( "testing" "time" + "github.com/devicelab-dev/maestro-runner/pkg/core" "github.com/devicelab-dev/maestro-runner/pkg/flow" ) @@ -293,9 +294,13 @@ func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { } func TestScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing.T) { - // A step without a timeout of its own that runs into the default 30s (a + // A step without a timeout of its own that runs into the default timeout (a // screen that moves after every scroll) keeps the message and the cause // it had: Maestro's words are for a step that set its timeout. + // The default is 30s; a second is enough to run into it. + defaultTimeout := core.ScrollUntilVisibleDefaultTimeout + core.ScrollUntilVisibleDefaultTimeout = time.Second + t.Cleanup(func() { core.ScrollUntilVisibleDefaultTimeout = defaultTimeout }) var scrolls atomic.Int32 d, _, _ := newTestDriver(t, func(cmd string, _ Args) (*Response, error) { switch cmd { @@ -314,8 +319,8 @@ func TestScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing if res.Success { t.Fatal("found an element that is not there") } - if elapsed := time.Since(start); elapsed < 30*time.Second { - t.Fatalf("ended after %v, before the default 30s timeout", elapsed) + if elapsed := time.Since(start); elapsed < core.ScrollUntilVisibleDefaultTimeout { + t.Fatalf("ended after %v, before the default timeout", elapsed) } want := fmt.Sprintf("%s not visible after scrolling down", describe(sel)) if res.Message != want || res.Error == nil || res.Error.Error() != want { diff --git a/pkg/driver/devicelab_ios_legacy/commands.go b/pkg/driver/devicelab_ios_legacy/commands.go index 4df04b80..32482773 100644 --- a/pkg/driver/devicelab_ios_legacy/commands.go +++ b/pkg/driver/devicelab_ios_legacy/commands.go @@ -823,7 +823,7 @@ func (d *Driver) handleScrollUntilVisible(s *flow.ScrollUntilVisibleStep) *core. if maxScrolls <= 0 { maxScrolls = math.MaxInt } - timeout := 30 * time.Second + timeout := core.ScrollUntilVisibleDefaultTimeout if s.TimeoutMs > 0 { timeout = time.Duration(s.TimeoutMs) * time.Millisecond } diff --git a/pkg/driver/devicelab_ios_legacy/settle_test.go b/pkg/driver/devicelab_ios_legacy/settle_test.go index 44399142..68fb2a16 100644 --- a/pkg/driver/devicelab_ios_legacy/settle_test.go +++ b/pkg/driver/devicelab_ios_legacy/settle_test.go @@ -196,10 +196,14 @@ func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { } } -// A step without a timeout of its own that runs into the default 30s (a screen +// A step without a timeout of its own that runs into the default timeout (a screen // that moves after every swipe) keeps the message it had: Maestro's words are // for a step that set its timeout. func TestScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing.T) { + // The default is 30s; a second is enough to run into it. + defaultTimeout := core.ScrollUntilVisibleDefaultTimeout + core.ScrollUntilVisibleDefaultTimeout = time.Second + t.Cleanup(func() { core.ScrollUntilVisibleDefaultTimeout = defaultTimeout }) info := &core.PlatformInfo{ScreenWidth: 400, ScreenHeight: 800} r := &settleRunner{settled: true, nodesFn: func(drags int) []SnapshotNode { return []SnapshotNode{{Type: "Other", Label: fmt.Sprintf("row %d", drags), Rect: SnapshotRect{Width: 400, Height: 40}}} @@ -210,8 +214,8 @@ func TestScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing if res.Success { t.Fatal("found an element that is not there") } - if elapsed := time.Since(start); elapsed < 30*time.Second { - t.Fatalf("ended after %v, before the default 30s timeout", elapsed) + if elapsed := time.Since(start); elapsed < core.ScrollUntilVisibleDefaultTimeout { + t.Fatalf("ended after %v, before the default timeout", elapsed) } if res.Message != "scroll target not found" || res.Error == nil || res.Error.Error() != "element not found after scrolling" { t.Errorf("a step without a timeout of its own should keep its message, got %q (%v)", res.Message, res.Error) diff --git a/pkg/driver/uiautomator2/commands.go b/pkg/driver/uiautomator2/commands.go index dfe6bd15..c285935d 100644 --- a/pkg/driver/uiautomator2/commands.go +++ b/pkg/driver/uiautomator2/commands.go @@ -655,7 +655,7 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com direction = "down" } - maxScrolls := 20 + maxScrolls := core.ScrollUntilVisibleDefaultMaxScrolls if step.MaxScrolls > 0 { maxScrolls = step.MaxScrolls } else if core.ScrollLooksUntilTimeout(step) { @@ -664,7 +664,7 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com // timeout, does not apply. maxScrolls = math.MaxInt } - timeout := 30 * time.Second + timeout := core.ScrollUntilVisibleDefaultTimeout if step.TimeoutMs > 0 { timeout = time.Duration(step.TimeoutMs) * time.Millisecond } diff --git a/pkg/driver/uiautomator2/commands_test.go b/pkg/driver/uiautomator2/commands_test.go index f96535b1..8fb2befb 100644 --- a/pkg/driver/uiautomator2/commands_test.go +++ b/pkg/driver/uiautomator2/commands_test.go @@ -4167,7 +4167,7 @@ func TestScrollUntilVisibleDefaultMaxScrolls(t *testing.T) { // fully on screen from then on. Each round spends the find's 1s budget (the // mock answers no locator, so the find reads the page source at the end of // it, whether the match is partly visible or not) and the scroll's 300ms -// settle: about 1.35s a round, 28s for 21 scrolls. +// settle: about 1.35s a round. func growingList(scrolls, shownAfter int) string { top := 2350 if scrolls >= shownAfter { @@ -4183,11 +4183,17 @@ func growingList(scrolls, shownAfter int) string { } func TestScrollUntilVisibleWithTimeoutIsNotCutAtDefaultMaxScrolls(t *testing.T) { - // The target comes on screen after the 21st scroll. A step with its own - // timeout scrolls until that timeout, as Maestro does; the default of 20 - // scrolls only caps a step without one. + // The target comes on screen after the scroll past the default cap. A + // step with its own timeout scrolls until that timeout, as Maestro does; + // the default cap only applies to a step without one. + + // The default cap is 20 scrolls; 2 is enough to show that it does not + // apply, and it saves the 20 rounds the real cap would cost. + defaultMaxScrolls := core.ScrollUntilVisibleDefaultMaxScrolls + core.ScrollUntilVisibleDefaultMaxScrolls = 2 + t.Cleanup(func() { core.ScrollUntilVisibleDefaultMaxScrolls = defaultMaxScrolls }) client := &MockUIA2Client{} - client.sourceFunc = func() (string, error) { return growingList(len(client.scrollCalls), 21), nil } + client.sourceFunc = func() (string, error) { return growingList(len(client.scrollCalls), 3), nil } driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ @@ -4197,10 +4203,10 @@ func TestScrollUntilVisibleWithTimeoutIsNotCutAtDefaultMaxScrolls(t *testing.T) }) if !result.Success { - t.Fatalf("expected the target after 21 scrolls, got %q", result.Message) + t.Fatalf("expected the target after 3 scrolls, got %q", result.Message) } - if got := len(client.scrollCalls); got != 21 { - t.Errorf("expected 21 scrolls, got %d", got) + if got := len(client.scrollCalls); got != 3 { + t.Errorf("expected 3 scrolls, got %d", got) } } @@ -4606,6 +4612,9 @@ func TestScrollUntilVisibleWithTimeoutWaitsForListThatFillsLate(t *testing.T) { func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { // A screen that never changes and a step with its own timeout: the step // keeps scrolling until the timeout, then fails in Maestro's words. + // A round that finds nothing costs about 1.3s here (the find's 1s budget + // and the 300ms settle), and the no-progress stop comes in the third: 3s + // is the least that tells the two apart. client := &MockUIA2Client{sourceFunc: func() (string, error) { return fillsLateSource(0, 1), nil }} driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) @@ -4613,14 +4622,14 @@ func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ Element: flow.Selector{Text: "NonExistent"}, Direction: "down", - BaseStep: flow.BaseStep{TimeoutMs: 5000}, + BaseStep: flow.BaseStep{TimeoutMs: 3000}, }) if result.Success { t.Fatal("expected failure when the element never shows") } - if elapsed := time.Since(start); elapsed < 5*time.Second { - t.Errorf("gave up after %v, before the 5s timeout", elapsed) + if elapsed := time.Since(start); elapsed < 3*time.Second { + t.Errorf("gave up after %v, before the 3s timeout", elapsed) } if got := len(client.scrollCalls); got <= 2 { t.Errorf("expected more scrolls than the no-progress stop's 2, got %d", got) @@ -4631,9 +4640,13 @@ func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { } func TestScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing.T) { - // A step without a timeout of its own that runs into the default 30s (a + // A step without a timeout of its own that runs into the default timeout (a // list that keeps moving, a maxScrolls it never reaches) keeps the message // it had: Maestro's words are for a step that set its timeout. + // The default is 30s; a second is enough to run into it. + defaultTimeout := core.ScrollUntilVisibleDefaultTimeout + core.ScrollUntilVisibleDefaultTimeout = time.Second + t.Cleanup(func() { core.ScrollUntilVisibleDefaultTimeout = defaultTimeout }) captures := 0 client := &MockUIA2Client{sourceFunc: func() (string, error) { return movingList(&captures), nil }} driver := New(client, &core.PlatformInfo{ScreenWidth: 1080, ScreenHeight: 2400}, nil) @@ -4648,8 +4661,8 @@ func TestScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing if result.Success { t.Fatal("expected failure when the element never shows") } - if elapsed := time.Since(start); elapsed < 30*time.Second { - t.Fatalf("ended after %v, before the default 30s timeout", elapsed) + if elapsed := time.Since(start); elapsed < core.ScrollUntilVisibleDefaultTimeout { + t.Fatalf("ended after %v, before the default timeout", elapsed) } if result.Message != "Element not found after 1000 scrolls" || result.Error == nil || result.Error.Error() != "element not found" { t.Errorf("a step without a timeout of its own should keep its message, got %q (%v)", result.Message, result.Error) diff --git a/pkg/driver/wda/commands.go b/pkg/driver/wda/commands.go index cea31c59..d4190d2a 100644 --- a/pkg/driver/wda/commands.go +++ b/pkg/driver/wda/commands.go @@ -1067,7 +1067,7 @@ func (d *Driver) scrollUntilVisible(step *flow.ScrollUntilVisibleStep) *core.Com if step.MaxScrolls > 0 { maxScrolls = step.MaxScrolls } - timeout := 30 * time.Second + timeout := core.ScrollUntilVisibleDefaultTimeout if step.TimeoutMs > 0 { timeout = time.Duration(step.TimeoutMs) * time.Millisecond } diff --git a/pkg/driver/wda/commands_test.go b/pkg/driver/wda/commands_test.go index b0f5184d..6513adb4 100644 --- a/pkg/driver/wda/commands_test.go +++ b/pkg/driver/wda/commands_test.go @@ -5345,6 +5345,9 @@ func TestScrollUntilVisibleWithTimeoutWaitsForListThatFillsLate(t *testing.T) { func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { // A screen that never changes and a step with its own timeout: the step // keeps scrolling until the timeout, then fails in Maestro's words. + // A round that finds nothing costs about 1s here, and the no-progress + // stop comes in the fourth: 3.5s is about the least that tells the two + // apart. var scrolls atomic.Int32 server := fillsLateWDAServer(&scrolls, 1<<30) defer server.Close() @@ -5354,14 +5357,14 @@ func TestScrollUntilVisibleWithTimeoutRunsToItOnStillScreen(t *testing.T) { result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ Element: flow.Selector{Text: "NonExistent"}, Direction: "down", - BaseStep: flow.BaseStep{TimeoutMs: 5000}, + BaseStep: flow.BaseStep{TimeoutMs: 3500}, }) if result.Success { t.Fatal("expected failure when the element never shows") } - if elapsed := time.Since(start); elapsed < 5*time.Second { - t.Errorf("gave up after %v, before the 5s timeout", elapsed) + if elapsed := time.Since(start); elapsed < 3500*time.Millisecond { + t.Errorf("gave up after %v, before the 3.5s timeout", elapsed) } if got := scrolls.Load(); got <= 3 { t.Errorf("expected more scrolls than the no-progress stop's 3, got %d", got) @@ -5404,9 +5407,13 @@ func movingWDAServer(scrolls *atomic.Int32) *httptest.Server { } func TestScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing.T) { - // A step without a timeout of its own that runs into the default 30s (a + // A step without a timeout of its own that runs into the default timeout (a // list that keeps moving) keeps the message it had: Maestro's words are // for a step that set its timeout. + // The default is 30s; a second is enough to run into it. + defaultTimeout := core.ScrollUntilVisibleDefaultTimeout + core.ScrollUntilVisibleDefaultTimeout = time.Second + t.Cleanup(func() { core.ScrollUntilVisibleDefaultTimeout = defaultTimeout }) var scrolls atomic.Int32 server := movingWDAServer(&scrolls) defer server.Close() @@ -5422,8 +5429,8 @@ func TestScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing if result.Success { t.Fatal("expected failure when the element never shows") } - if elapsed := time.Since(start); elapsed < 30*time.Second { - t.Fatalf("ended after %v, before the default 30s timeout", elapsed) + if elapsed := time.Since(start); elapsed < core.ScrollUntilVisibleDefaultTimeout { + t.Fatalf("ended after %v, before the default timeout", elapsed) } want := fmt.Sprintf("Element not found: %s", selectorDesc(step.Element)) if result.Message != want || result.Error == nil || result.Error.Error() != "element not found after scrolling" { From f7ec738cab55808ed8817843c9828088a5aee43b Mon Sep 17 00:00:00 2001 From: lvladika Date: Wed, 18 Nov 2026 14:19:46 +0100 Subject: [PATCH 9/9] test: the default-cap test keeps the cap, not the deadline, its limit TestScrollUntilVisibleDefaultMaxScrolls (uiautomator2, devicelab) runs its 20 scrolls with no timeout of its own, so the default 30s timeout was about 4s from ending it first (20 rounds of about 1.3s each). It now sets the default timeout to a minute for itself, so a slower round cannot turn a check of the cap into a check of the deadline. It still expects 20 scrolls. --- pkg/driver/devicelab/commands_test.go | 8 ++++++-- pkg/driver/uiautomator2/commands_test.go | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pkg/driver/devicelab/commands_test.go b/pkg/driver/devicelab/commands_test.go index 95f64298..149f86c7 100644 --- a/pkg/driver/devicelab/commands_test.go +++ b/pkg/driver/devicelab/commands_test.go @@ -178,6 +178,11 @@ func TestScrollUntilVisibleRespectsTimeout(t *testing.T) { } func TestScrollUntilVisibleDefaultMaxScrolls(t *testing.T) { + // 20 rounds of about 1.3s here end within 4s of the default 30s timeout: + // a minute keeps the cap, not the deadline, the limit. + defaultTimeout := core.ScrollUntilVisibleDefaultTimeout + core.ScrollUntilVisibleDefaultTimeout = time.Minute + t.Cleanup(func() { core.ScrollUntilVisibleDefaultTimeout = defaultTimeout }) captures := 0 client := &mockDeviceLabClient{ sourceFunc: func() (string, error) { @@ -190,8 +195,7 @@ func TestScrollUntilVisibleDefaultMaxScrolls(t *testing.T) { step := &flow.ScrollUntilVisibleStep{ Element: flow.Selector{Text: "NonExistent"}, Direction: "down", - // No timeout of its own (the default 30s outlasts 20 scrolls here) - // and MaxScrolls not set — defaults to 20 + // No timeout of its own and MaxScrolls not set — defaults to 20 } result := driver.scrollUntilVisible(step) diff --git a/pkg/driver/uiautomator2/commands_test.go b/pkg/driver/uiautomator2/commands_test.go index 8fb2befb..49f2080b 100644 --- a/pkg/driver/uiautomator2/commands_test.go +++ b/pkg/driver/uiautomator2/commands_test.go @@ -4135,6 +4135,11 @@ func TestScrollUntilVisibleRespectsTimeout(t *testing.T) { } func TestScrollUntilVisibleDefaultMaxScrolls(t *testing.T) { + // 20 rounds of about 1.3s here end within 4s of the default 30s timeout: + // a minute keeps the cap, not the deadline, the limit. + defaultTimeout := core.ScrollUntilVisibleDefaultTimeout + core.ScrollUntilVisibleDefaultTimeout = time.Minute + t.Cleanup(func() { core.ScrollUntilVisibleDefaultTimeout = defaultTimeout }) captures := 0 client := &MockUIA2Client{ sourceFunc: func() (string, error) { @@ -4147,8 +4152,7 @@ func TestScrollUntilVisibleDefaultMaxScrolls(t *testing.T) { step := &flow.ScrollUntilVisibleStep{ Element: flow.Selector{Text: "NonExistent"}, Direction: "down", - // No timeout of its own (the default 30s outlasts 20 scrolls here) - // and MaxScrolls not set — defaults to 20 + // No timeout of its own and MaxScrolls not set — defaults to 20 } result := driver.scrollUntilVisible(step)