test_xpath.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # coding: utf-8
  2. #
  3. import threading
  4. from functools import partial
  5. import pytest
  6. import uiautomator2 as u2
  7. def test_get_text(dev: u2.Device):
  8. assert dev.xpath("App").get_text() == "App"
  9. def test_click(dev: u2.Device):
  10. dev.xpath("App").click()
  11. assert dev.xpath("Alarm").wait()
  12. assert dev.xpath("Alarm").exists
  13. def test_swipe(dev: u2.Device):
  14. d = dev
  15. d.xpath("App").click()
  16. d.xpath("Alarm").wait()
  17. # assert not d.xpath("Voice Recognition").exists
  18. d.xpath("@android:id/list").get().swipe("up", 0.5)
  19. assert d.xpath("Voice Recognition").wait()
  20. def test_xpath_query(dev: u2.Device):
  21. assert dev.xpath("Accessibility").wait()
  22. assert dev.xpath("%ccessibility").wait()
  23. assert dev.xpath("Accessibilit%").wait()
  24. def test_element_all(dev: u2.Device):
  25. app = dev.xpath('//*[@text="App"]')
  26. assert app.wait()
  27. assert len(app.all()) == 1
  28. assert app.exists
  29. def test_watcher(dev: u2.Device, request):
  30. dev.watcher.when("App").click()
  31. dev.watcher.start(interval=1.0)
  32. event = threading.Event()
  33. def _set_event(e):
  34. e.set()
  35. dev.watcher.when("Action Bar").call(partial(_set_event, event))
  36. assert event.wait(5.0), "xpath not trigger callback"
  37. def test_xpath_scroll_to(dev: u2.Device):
  38. d = dev
  39. d.xpath("Graphics").click()
  40. d.xpath("@android:id/list").scroll_to("Pictures")
  41. assert d.xpath("Pictures").exists
  42. def test_xpath_parent(dev: u2.Device):
  43. d = dev
  44. info = d.xpath("App").parent("@android:id/list").info
  45. assert info["resourceId"] == "android:id/list"