test_simple.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # coding: utf-8
  2. #
  3. # Test apk Download from
  4. # https://github.com/appium/java-client/raw/master/src/test/java/io/appium/java_client/ApiDemos-debug.apk
  5. import time
  6. import unittest
  7. import pytest
  8. import uiautomator2 as u2
  9. @pytest.mark.skip("not working")
  10. def test_toast_get_message(dev: u2.Device):
  11. d = dev
  12. assert d.toast.get_message(0) is None
  13. assert d.toast.get_message(0, default="d") == "d"
  14. d(text="App").click()
  15. d(text="Notification").click()
  16. d(text="NotifyWithText").click()
  17. try:
  18. d(text="Show Short Notification").click()
  19. except u2.UiObjectNotFoundError:
  20. d(text="SHOW SHORT NOTIFICATION").click()
  21. #self.assertEqual(d.toast.get_message(2, 5, ""), "Short notification")
  22. assert "Short notification" in d.toast.get_message(2, 5, "")
  23. time.sleep(.5)
  24. assert d.toast.get_message(0, 0.4)
  25. def test_scroll(dev: u2.Device):
  26. d = dev
  27. d(text="App").click()
  28. if not d(scrollable=True).exists:
  29. pytest.skip("screen to large, no need to scroll")
  30. d(scrollable=True).scroll.to(text="Voice Recognition")
  31. @pytest.mark.skip("Need upgrade")
  32. def test_watchers(self):
  33. """
  34. App -> Notification -> Status Bar
  35. """
  36. d = self.sess
  37. d.watcher.remove()
  38. d.watcher.stop()
  39. d(text="App").click()
  40. d.xpath("Notification").wait()
  41. d.watcher("N").when('Notification').click()
  42. d.watcher.run()
  43. self.assertTrue(d(text="Status Bar").wait(timeout=3))
  44. d.press("back")
  45. d.press("back")
  46. # Should auto click Notification when show up
  47. self.assertFalse(d.watcher.running())
  48. d.watcher.start()
  49. self.assertTrue(d.watcher.running())
  50. d(text="App").click()
  51. self.assertTrue(d(text="Status Bar").exists(timeout=5))
  52. d.watcher.remove("N")
  53. d.press("back")
  54. d.press("back")
  55. d(text="App").click()
  56. self.assertFalse(d(text="Status Bar").wait(timeout=5))
  57. @pytest.mark.skip("TODO:: not fixed")
  58. def test_count(self):
  59. d = self.sess
  60. count = d(resourceId="android:id/list").child(
  61. className="android.widget.TextView").count
  62. self.assertEqual(count, 11)
  63. self.assertEqual(
  64. d(resourceId="android:id/list").info['childCount'], 11)
  65. count = d(resourceId="android:id/list").child(
  66. className="android.widget.TextView", instance=0).count
  67. self.assertEqual(count, 1)
  68. def test_get_text(dev):
  69. d = dev
  70. text = d(resourceId="android:id/list").child(
  71. className="android.widget.TextView", text="App").get_text()
  72. assert text == "App"
  73. def test_xpath(dev):
  74. d = dev
  75. d.xpath("//*[@text='Media']").wait()
  76. assert len(d.xpath("//*[@text='Media']").all()) == 1
  77. assert len(d.xpath("//*[@text='MediaNotExists']").all()) == 0
  78. d.xpath("//*[@text='Media']").click()
  79. assert d.xpath('//*[contains(@text, "VideoView")]').wait(5)
  80. @pytest.mark.skip("Need fix")
  81. def test_implicitly_wait(d):
  82. d.implicitly_wait(2)
  83. assert d.implicitly_wait() == 2
  84. start = time.time()
  85. with self.assertRaises(u2.UiObjectNotFoundError):
  86. d(text="Sensors").get_text()
  87. time_used = time.time() - start
  88. assert time_used >= 2
  89. # maybe longer then 2, waitForExists -> getText
  90. # getText may take 1~2s
  91. assert time_used < 2 + 3
  92. @pytest.mark.skip("TODO:: not fixed")
  93. def test_select_iter(d):
  94. d(text='OS').click()
  95. texts = d(resourceId='android:id/list').child(
  96. className='android.widget.TextView')
  97. assert texts.count == 4
  98. words = []
  99. for item in texts:
  100. words.append(item.get_text())
  101. assert words == ['Morse Code', 'Rotation Vector', 'Sensors', 'SMS Messaging']
  102. @pytest.mark.skip("Deprecated")
  103. def test_plugin(self):
  104. def _my_plugin(d, k):
  105. def _inner():
  106. return k
  107. return _inner
  108. u2.plugin_clear()
  109. u2.plugin_register('my', _my_plugin, 'pp')
  110. self.assertEqual(self.d.ext_my(), 'pp')
  111. def test_send_keys(dev):
  112. d = dev
  113. d.xpath("App").click()
  114. d.xpath("Search").click()
  115. d.xpath('//*[@text="Invoke Search"]').click()
  116. d.xpath('@io.appium.android.apis:id/txt_query_prefill').click()
  117. d.send_keys("hello", clear=True)
  118. assert d.xpath('io.appium.android.apis:id/txt_query_prefill').info['text'] == 'hello'
  119. if __name__ == '__main__':
  120. unittest.main()