test_selector.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # coding: utf-8
  2. # author: codeskyblue
  3. import time
  4. import pytest
  5. import uiautomator2 as u2
  6. from uiautomator2 import Selector
  7. def test_selector_magic():
  8. s = Selector(text='123').child(text='456').sibling(text='789').clone()
  9. assert s['text'] == '123'
  10. del s['text']
  11. assert 'text' not in s
  12. s.update_instance(0)
  13. def test_exists(app: u2.Device):
  14. assert app(text='Addition').exists
  15. assert app(text='Addition').exists(timeout=.1)
  16. assert not app(text='should-not-exists').exists
  17. assert not app(text='should-not-exists').exists(timeout=.1)
  18. def test_selector_info(app: u2.Device):
  19. _info = app(text="Addition").info
  20. assert _info["text"] == "Addition"
  21. @pytest.mark.skip(reason="not stable")
  22. def test_child_by(app: u2.Device):
  23. app(text="Addition").click()
  24. app(text='Add').wait()
  25. time.sleep(.5)
  26. # childByText, childByInstance and childByDescription run query when called
  27. app(resourceId='android:id/content').child_by_text("Add")
  28. app(resourceId='android:id/content').child_by_instance(0)
  29. with pytest.raises(u2.UiObjectNotFoundError):
  30. app(resourceId='android:id/content').child_by_description("should-not-exists")
  31. # only run query after call UiObject method
  32. assert app(resourceId='android:id/content').child_selector(text="Add").exists
  33. def test_screenshot(app: u2.Device):
  34. lx, ly, rx, ry = app(text="Addition").bounds()
  35. image = app(text="Addition").screenshot()
  36. assert image.size == (rx - lx, ry - ly)
  37. def test_center(app: u2.Device):
  38. x, y = app(text="Addition").center()
  39. assert x > 0 and y > 0
  40. def test_click_exists(app: u2.Device):
  41. assert app(text="Addition").click_exists()
  42. app(text='Addition').wait_gone()
  43. assert not app(text="should-not-exists").click_exists()
  44. @pytest.mark.parametrize("direction", ["up", "down", "left", "right"])
  45. def test_swipe(app: u2.Device, direction: str):
  46. app(resourceId="android:id/content").swipe(direction)
  47. def test_pinch_gesture(app: u2.Device):
  48. app(text='Pinch').click()
  49. app(description='pinch image').wait()
  50. scale_text = app.xpath('Scale%').get_text()
  51. assert scale_text.endswith('1.00')
  52. app(description='pinch image').pinch_in(80)
  53. scale_text = app.xpath('Scale%').get_text()
  54. assert scale_text.endswith('0.50')
  55. app(description='pinch image').pinch_out()
  56. scale_text = app.xpath('Scale%').get_text()
  57. assert scale_text.endswith('3.00')
  58. app().gesture((0.1, 0.5), (0.9, 0.5), (0.5, 0.5), (0.5, 0.5), steps=20)
  59. scale_text = app.xpath('Scale%').get_text()
  60. assert scale_text.endswith('0.50')
  61. # TODO
  62. # long_click
  63. # drag_to
  64. # swipe
  65. # guesture