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
26 changes: 21 additions & 5 deletions pkg/driver/wda/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,23 +498,39 @@ 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
// went stale mid-walk. It clears on the next attempt, so retry once
// 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
Expand Down
8 changes: 4 additions & 4 deletions pkg/driver/wda/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/driver/wda/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down
64 changes: 64 additions & 0 deletions pkg/driver/wda/lookup_source_test.go
Original file line number Diff line number Diff line change
@@ -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": `<AppiumAUT><XCUIElementTypeApplication type="XCUIElementTypeApplication" name="App" enabled="true" x="0" y="0" width="390" height="844">` +
`<XCUIElementTypeStaticText type="XCUIElementTypeStaticText" name="greeting" label="Hello" enabled="true" x="20" y="100" width="200" height="30"/>` +
`</XCUIElementTypeApplication></AppiumAUT>`})
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])
}
}
Loading