Skip to content

Commit

Permalink
fix xpath selector changes when .child .xpath called, #close 942 (#947)
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue authored Mar 20, 2024
1 parent 9e88100 commit 4b849e5
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 145 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
name: Python application

on: [push]
on:
push:
branches:
- master
tags-ignore:
- '*'
pull_request:
branches:
- master

jobs:
build-and-publish:
Expand Down
43 changes: 19 additions & 24 deletions tests/real_device/test_xpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@

import threading
from functools import partial
from pprint import pprint

import pytest

import uiautomator2 as u2


def test_xpath_selector(sess: u2.Session):
sel1 = sess.xpath("/a")
print(str(sel1), type(str(sel1)))
assert str(sel1).endswith("=/a")
assert str(sel1.child("/b")).endswith("=/a/b")
assert str(sel1).endswith("=/a") # sel1 should not be changed
assert str(sel1.xpath("/b")).endswith("=/a|/b")
assert str(sel1.xpath(["/b", "/c"])).endswith("=/a|/b|/c")
assert sel1.position(0.1, 0.1) != sel1
assert sel1.fallback("click") != sel1
with pytest.raises(ValueError):
sel1.fallback("invalid-action")


def test_get_text(sess: u2.Session):
assert sess.xpath("App").get_text() == "App"

Expand All @@ -24,10 +36,11 @@ def test_swipe(sess: u2.Session):
d = sess
d.xpath("App").click()
d.xpath("Alarm").wait()
#assert not d.xpath("Voice Recognition").exists
# assert not d.xpath("Voice Recognition").exists
d.xpath("@android:id/list").get().swipe("up", 0.5)
assert d.xpath("Voice Recognition").wait()


def test_xpath_query(sess: u2.Session):
assert sess.xpath("Accessibility").wait()
assert sess.xpath("%ccessibility").wait()
Expand All @@ -46,40 +59,22 @@ def test_watcher(sess: u2.Session, request):
sess.xpath.watch_background(interval=1.0)

event = threading.Event()

def _set_event(e):
e.set()

sess.xpath.when("Action Bar").call(partial(_set_event, event))
assert event.wait(5.0), "xpath not trigger callback"


@pytest.mark.skip("Deprecated")
def test_watcher_from_yaml(sess: u2.Session, request):
yaml_content = """---
- when: App
then: click
- when: Action Bar
then: >
def callback(d):
print("D:", d)
d.xpath("Alarm").click()
def hello():
print("World")
"""
sess.xpath.apply_watch_from_yaml(yaml_content)
sess.xpath.watch_background(interval=1.0)

assert sess.xpath("Alarm Controller").wait(timeout=10)


def test_xpath_scroll_to(sess: u2.Session):
d = sess
d.xpath("Graphics").click()
d.xpath("@android:id/list").scroll_to("Pictures")
assert d.xpath("Pictures").exists


def test_xpath_parent(sess: u2.Session):
d = sess
info = d.xpath("App").parent("@android:id/list").info
assert info['resourceId'] == 'android:id/list'
assert info["resourceId"] == "android:id/list"
Loading

0 comments on commit 4b849e5

Please sign in to comment.