diff --git a/pkg/driver/wda/client.go b/pkg/driver/wda/client.go index 78e931b9..71c97ceb 100644 --- a/pkg/driver/wda/client.go +++ b/pkg/driver/wda/client.go @@ -498,15 +498,31 @@ func (c *Client) Screenshot() ([]byte, error) { return nil, fmt.Errorf("invalid screenshot response") } -// Source returns the UI hierarchy as XML. +// Source returns the UI hierarchy as XML, with every attribute WDA writes. func (c *Client) Source() (string, error) { - source, err := c.source() + source, err := c.source("/source") c.noteSource(err) return source, err } -func (c *Client) source() (string, error) { - resp, err := c.get(c.sessionPath("/source")) +// lookupSourceExcluded are the attributes a lookup's page source leaves out. +// WebDriverAgent computes both for every element of the tree, `visible` with an +// accessibility call each, and the page-source matcher reads neither: it +// decides by bounds. On a screen of a few thousand elements the source took +// 23 s with them and 2.5 s without (an iPhone 17, iOS 27). Appium's +// pageSourceExcludedAttributes setting exists for the same reason. +const lookupSourceExcluded = "visible,accessible" + +// LookupSource is Source for a lookup: the hierarchy without the attributes +// in lookupSourceExcluded. Every element of it parses as displayed. +func (c *Client) LookupSource() (string, error) { + source, err := c.source("/source?excluded_attributes=" + lookupSourceExcluded) + c.noteSource(err) + return source, err +} + +func (c *Client) source(path string) (string, error) { + resp, err := c.get(c.sessionPath(path)) if err != nil { // A snapshot taken while the accessibility tree is mutating can fail // with a transient kAXErrorInvalidUIElement (-25202): an element ref @@ -514,7 +530,7 @@ func (c *Client) source() (string, error) { // before surfacing it (mirrors Maestro's #3430 recovery). if isTransientAXError(err) { time.Sleep(150 * time.Millisecond) - resp, err = c.get(c.sessionPath("/source")) + resp, err = c.get(c.sessionPath(path)) } if err != nil { return "", err diff --git a/pkg/driver/wda/commands.go b/pkg/driver/wda/commands.go index 15344f4b..edaa858a 100644 --- a/pkg/driver/wda/commands.go +++ b/pkg/driver/wda/commands.go @@ -226,7 +226,7 @@ func (d *Driver) aimClearOfKeyboard(sel flow.Selector, info *core.ElementInfo, x // keyboard (a key, its return button), read from the page source; false when // the source cannot be read. func (d *Driver) onKeyboard(b core.Bounds) bool { - src, err := d.client.Source() + src, err := d.client.LookupSource() if err != nil { return false } @@ -491,7 +491,7 @@ func (d *Driver) assertVisibleCount(step *flow.AssertVisibleStep, want int) *cor // countVisibleMatchesOnce takes one page-source snapshot and counts the // selector's visible matches. func (d *Driver) countVisibleMatchesOnce(sel flow.Selector) (int, error) { - pageSource, err := d.client.Source() + pageSource, err := d.client.LookupSource() if err != nil { return 0, err } @@ -887,7 +887,7 @@ func (d *Driver) dismissKeyboard(kb *core.Bounds) bool { keyboardTop = kb.Y } sx, sy, ok := w/2, h/2, true - if src, err := d.client.Source(); err == nil { + if src, err := d.client.LookupSource(); err == nil { if elems, err := ParsePageSource(src); err == nil { elems = FilterOutOfBounds(elems, w, h) nodes := make([]core.ScreenNode, 0, len(elems)) @@ -1822,7 +1822,7 @@ func (d *Driver) copyTextFrom(step *flow.CopyTextFromStep) *core.CommandResult { // lookup returns an element id, and those are plain text and id selectors, so // there is no relative selector to resolve here. func (d *Driver) pageSourceText(sel flow.Selector, want core.Bounds) (string, error) { - pageSource, err := d.client.Source() + pageSource, err := d.client.LookupSource() if err != nil { return "", err } diff --git a/pkg/driver/wda/driver.go b/pkg/driver/wda/driver.go index d880fd24..5a605533 100644 --- a/pkg/driver/wda/driver.go +++ b/pkg/driver/wda/driver.go @@ -1273,7 +1273,7 @@ func (d *Driver) findElementRelativeWithContext(ctx context.Context, sel flow.Se // findElementRelativeOnce performs a single attempt to find element with relative selector. func (d *Driver) findElementRelativeOnce(sel flow.Selector) (*core.ElementInfo, error) { - pageSource, err := d.client.Source() + pageSource, err := d.client.LookupSource() if err != nil { return nil, fmt.Errorf("failed to get page source: %w", err) } @@ -1377,7 +1377,7 @@ func (d *Driver) resolveRelativeSelector(sel flow.Selector, allElements []*Parse // findElementByPageSourceOnce performs a single page source search. func (d *Driver) findElementByPageSourceOnce(sel flow.Selector) (*core.ElementInfo, error) { - pageSource, err := d.client.Source() + pageSource, err := d.client.LookupSource() if err != nil { return nil, err } diff --git a/pkg/driver/wda/lookup_source_test.go b/pkg/driver/wda/lookup_source_test.go new file mode 100644 index 00000000..d07337be --- /dev/null +++ b/pkg/driver/wda/lookup_source_test.go @@ -0,0 +1,64 @@ +package wda + +import ( + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + + "github.com/devicelab-dev/maestro-runner/pkg/flow" +) + +// A lookup that falls back to the page source asks WDA to leave out the +// attributes it computes per element and the matcher never reads; the +// hierarchy a report or a tool shows keeps every attribute. +func TestLookupSourceLeavesOutVisibleAndAccessible(t *testing.T) { + var mu sync.Mutex + var sources []string + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case strings.HasSuffix(r.URL.Path, "/source"): + mu.Lock() + sources = append(sources, r.URL.Query().Get("excluded_attributes")) + mu.Unlock() + // WDA leaves out what it was asked to: no visible attribute here. + jsonResponse(w, map[string]any{"value": `` + + `` + + ``}) + case strings.HasSuffix(r.URL.Path, "/elements"): + jsonResponse(w, map[string]any{"value": []any{}}) + case strings.HasSuffix(r.URL.Path, "/element"): + w.WriteHeader(http.StatusNotFound) + jsonResponse(w, map[string]any{"value": map[string]string{"error": "no such element", "message": "not found"}}) + default: + w.WriteHeader(http.StatusNotFound) + jsonResponse(w, map[string]any{"value": map[string]string{"error": "unknown command", "message": r.URL.Path}}) + } + })) + defer server.Close() + d := createTestDriver(server) + + info, err := d.findElementByPageSourceOnce(flow.Selector{Text: "Hello"}) + if err != nil { + t.Fatalf("lookup: %v", err) + } + if !info.Visible || info.MatchNote != "" { + t.Errorf("an element of a source without visible should count as displayed with no note, got visible=%v note=%q", info.Visible, info.MatchNote) + } + if _, err := d.Hierarchy(); err != nil { + t.Fatalf("hierarchy: %v", err) + } + + mu.Lock() + defer mu.Unlock() + if len(sources) != 2 { + t.Fatalf("want 2 source reads, got %d", len(sources)) + } + if sources[0] != "visible,accessible" { + t.Errorf("lookup's source: excluded_attributes = %q, want %q", sources[0], "visible,accessible") + } + if sources[1] != "" { + t.Errorf("hierarchy's source should keep every attribute, got excluded_attributes = %q", sources[1]) + } +}