Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions pkg/core/scrollprogress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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())
}
29 changes: 28 additions & 1 deletion pkg/core/scrollprogress_test.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)
}
}
32 changes: 24 additions & 8 deletions pkg/driver/appium/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package appium
import (
"context"
"fmt"
"math"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading
Loading