test_device.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # coding: utf-8
  2. # author: codeskyblue
  3. from pathlib import Path
  4. import random
  5. import pytest
  6. import uiautomator2 as u2
  7. from PIL import Image
  8. def test_info(d: u2.Device):
  9. d.info
  10. d.device_info
  11. d.wlan_ip
  12. assert isinstance(d.serial, str)
  13. w, h = d.window_size()
  14. assert w > 0 and h > 0
  15. def test_dump_hierarchy(d: u2.Device):
  16. assert d.dump_hierarchy().startswith("<?xml")
  17. assert d.dump_hierarchy(compressed=True, pretty=True).startswith("<?xml")
  18. def test_screenshot(d: u2.Device, tmp_path: Path):
  19. im = d.screenshot()
  20. assert isinstance(im, Image.Image)
  21. d.screenshot(tmp_path / "screenshot.png")
  22. assert (tmp_path / "screenshot.png").exists()
  23. def test_settings(d: u2.Device):
  24. d.implicitly_wait(10)
  25. def test_click(app: u2.Device):
  26. app.click(1, 1)
  27. app.long_click(1, 1)
  28. app.click(0.5, 0.5)
  29. app.double_click(1, 1)
  30. def test_swipe_drag(app: u2.Device):
  31. app.swipe(1, 1, 2, 2, steps=20)
  32. app.swipe(1, 1, 2, 2, duration=.1)
  33. app.swipe(1, 1, 2, 2)
  34. with pytest.warns(UserWarning):
  35. app.swipe(1, 1, 2, 2, 0.1, 20)
  36. app.swipe_points([(1, 1), (2, 2)], duration=0.1)
  37. app.drag(1, 1, 2, 2, duration=0.1)
  38. @pytest.mark.parametrize("direction", ["up", "down", "left", "right"])
  39. def test_swipe_ext(d: u2.Device, direction: str):
  40. d.swipe_ext(direction)
  41. def test_swipe_ext_inside_box(app: u2.Device):
  42. bounds = app.xpath('@android:id/content').get().bounds
  43. app.swipe_ext("up", box=bounds)
  44. def test_press(d: u2.Device):
  45. d.press("volume_down")
  46. # press home keycode
  47. d.press(3)
  48. d.long_press("volume_down")
  49. # long volume_down keycode
  50. d.long_press(25)
  51. d.keyevent("volume_down")
  52. def test_screen(d: u2.Device):
  53. # d.screen_off()
  54. d.screen_on()
  55. def test_orientation(d: u2.Device):
  56. with pytest.raises(ValueError):
  57. d.orientation = 'unknown'
  58. d.orientation = 'n'
  59. assert d.orientation == 'natural'
  60. d.freeze_rotation(True)
  61. d.freeze_rotation(False)
  62. def test_traversed_text(d: u2.Device):
  63. d.last_traversed_text
  64. d.clear_traversed_text()
  65. def test_open(d: u2.Device):
  66. d.open_notification()
  67. d.open_quick_settings()
  68. d.open_url("https://www.baidu.com")
  69. def test_toast(app: u2.Device):
  70. app.clear_toast()
  71. assert app.last_toast is None
  72. app(text='Toast').click()
  73. app(text='Show Toast').click()
  74. app.sleep(.2)
  75. assert app.last_toast == "Button Clicked!"
  76. app.clear_toast()
  77. assert app.last_toast is None
  78. def test_clipboard(d: u2.Device):
  79. d.set_input_ime()
  80. text = str(random.randint(0, 1000))
  81. d.clipboard = f'n{text}'
  82. assert d.clipboard == f'n{text}'
  83. def test_push_pull(d: u2.Device, tmp_path: Path):
  84. src_file = tmp_path / "test_push.txt"
  85. src_file.write_text("12345")
  86. d.push(src_file, "/data/local/tmp/test_push.txt")
  87. dst_file = tmp_path / "test_pull.txt"
  88. d.pull("/data/local/tmp/test_push.txt", dst_file)
  89. assert dst_file.read_text() == "12345"