diff --git a/pkg/core/scrollprogress.go b/pkg/core/scrollprogress.go index a6599ce6..28c5b1f7 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,42 @@ func ScrollSignature(capture string) string { _, _ = h.Write([]byte(capture)) 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 +// 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..2ccac45e 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" @@ -382,13 +383,18 @@ 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) { + // The step's own timeout is its budget, as in Maestro and on the iOS + // drivers: the default count, which could end the step before that + // timeout, does not apply. + maxScrolls = math.MaxInt } partiallyVisible := false @@ -476,11 +482,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 +507,16 @@ 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 core.ScrollLooksUntilTimeout(step) && !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..fec65357 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,213 @@ 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. + // 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() + driver := createTestAppiumDriver(server) + + start := time.Now() + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "NonExistent"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 3000}, + }) + + if result.Success { + t.Fatal("expected failure when the element never shows") + } + 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) + } + if !strings.Contains(result.Message, "No visible element found") { + t.Errorf("message should be Maestro's, got %q", result.Message) + } +} + +// 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 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() + 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 < 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) + } +} + +func TestAppiumScrollUntilVisibleWithTimeoutIsNotCutAtDefaultMaxScrolls(t *testing.T) { + // 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") + path := r.URL.Path + switch { + case strings.HasSuffix(path, "/source"): + n := scrolls.Load() + top := 2290 + if n >= 3 { + 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 3 scrolls, got %q", result.Message) + } + 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 c197391a..72a215b0 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" @@ -1603,11 +1604,16 @@ 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) { + // The step's own timeout is its budget, as in Maestro and on the iOS + // drivers: the default count, which could end the step before that + // 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 } @@ -1663,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 { @@ -1721,7 +1732,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 +1758,11 @@ 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. + 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 2aa56612..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", - BaseStep: flow.BaseStep{TimeoutMs: 60000}, // long timeout - // MaxScrolls not set — defaults to 20 + // No timeout of its own and MaxScrolls not set — defaults to 20 } result := driver.scrollUntilVisible(step) @@ -204,6 +208,54 @@ 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. 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. +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 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, 3), 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 3 scrolls, got %q", result.Message) + } + if got := client.scrollCalls; got != 3 { + t.Errorf("expected 3 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 @@ -327,8 +379,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 +397,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 +409,159 @@ 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. + // 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) + + start := time.Now() + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "NonExistent"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 3000}, + }) + + if result.Success { + t.Fatal("expected failure when the element never shows") + } + 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) + } + if !strings.Contains(result.Message, "No visible element found") { + 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 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) + + 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 < 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) + } +} + +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) + } +} + +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/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/devicelab_ios/actions.go b/pkg/driver/devicelab_ios/actions.go index 3b946d8f..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 } @@ -354,12 +354,24 @@ 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 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)) + } 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..d5d446bc 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" @@ -10,7 +11,9 @@ import ( "strings" "sync/atomic" "testing" + "time" + "github.com/devicelab-dev/maestro-runner/pkg/core" "github.com/devicelab-dev/maestro-runner/pkg/flow" ) @@ -226,6 +229,105 @@ 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 TestScrollUntilVisibleWithoutTimeoutKeepsItsMessageAtTheDeadline(t *testing.T) { + // 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 { + 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 < 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 { + 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}) @@ -720,6 +822,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/devicelab_ios_legacy/commands.go b/pkg/driver/devicelab_ios_legacy/commands.go index a79cc9c5..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 } @@ -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,14 @@ func (d *Driver) handleScrollUntilVisible(s *flow.ScrollUntilVisibleStep) *core. // screen each time and its rows are not where they will stop. d.settleScreen(settleCapMs(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), + ) + } 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..68fb2a16 100644 --- a/pkg/driver/devicelab_ios_legacy/settle_test.go +++ b/pkg/driver/devicelab_ios_legacy/settle_test.go @@ -2,8 +2,10 @@ package devicelab_ios_legacy import ( "encoding/json" + "fmt" "net/http" "net/http/httptest" + "strings" "sync" "testing" "time" @@ -124,7 +126,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 +135,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 +145,83 @@ 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) + } +} + +// 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}}} + }} + 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 < 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) + } +} + // 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..c285935d 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" @@ -654,11 +655,16 @@ 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) { + // The step's own timeout is its budget, as in Maestro and on the iOS + // drivers: the default count, which could end the step before that + // 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 } @@ -709,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 { @@ -765,7 +776,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 +802,11 @@ 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. + 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 522cbf29..49f2080b 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" @@ -4134,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) { @@ -4146,8 +4152,7 @@ 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 and MaxScrolls not set — defaults to 20 } result := driver.scrollUntilVisible(step) @@ -4161,6 +4166,54 @@ 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. 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. +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 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), 3), 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 3 scrolls, got %q", result.Message) + } + if got := len(client.scrollCalls); got != 3 { + t.Errorf("expected 3 scrolls, got %d", got) + } +} + // ============================================================================ // Compile-time interface assertion // ============================================================================ @@ -4491,8 +4544,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 +4562,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 +4574,159 @@ 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. + // 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) + + start := time.Now() + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "NonExistent"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 3000}, + }) + + if result.Success { + t.Fatal("expected failure when the element never shows") + } + 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) + } + if !strings.Contains(result.Message, "No visible element found") { + 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 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) + + 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 < 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) + } +} + +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) + } +} + +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/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) { diff --git a/pkg/driver/wda/commands.go b/pkg/driver/wda/commands.go index 9dfc8bb3..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 } @@ -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 @@ -1132,13 +1137,22 @@ 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) } + // 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 61906e15..6513adb4 100644 --- a/pkg/driver/wda/commands_test.go +++ b/pkg/driver/wda/commands_test.go @@ -1,15 +1,19 @@ package wda import ( + "context" "encoding/base64" "encoding/json" + "errors" "fmt" "io" "net/http" "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 +5230,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 +5265,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 +5278,245 @@ 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. + // 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() + driver := createTestDriver(server) + + start := time.Now() + result := driver.scrollUntilVisible(&flow.ScrollUntilVisibleStep{ + Element: flow.Selector{Text: "NonExistent"}, + Direction: "down", + BaseStep: flow.BaseStep{TimeoutMs: 3500}, + }) + + if result.Success { + t.Fatal("expected failure when the element never shows") + } + 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) + } + if !strings.Contains(result.Message, "No visible element found") { + t.Errorf("message should be Maestro's, got %q", result.Message) + } +} + +// 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 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() + 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 < 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" { + t.Errorf("a step without a timeout of its own should keep its message %q, got %q (%v)", want, 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. + 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) + } +} + +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 // =============================================================================