Skip to content

Commit

Permalink
Merge pull request #2 from openatx/master
Browse files Browse the repository at this point in the history
Merge from openatx/uiautomator2
  • Loading branch information
finfou committed Jun 25, 2018
2 parents c5048a4 + d08e2e8 commit ba290cf
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 46 deletions.
53 changes: 44 additions & 9 deletions README.md
Expand Up @@ -708,6 +708,9 @@ Selector supports below parameters. Refer to [UiSelector Java doc](http://develo
```python
d(text="Settings").exists # True if exists, else False
d.exists(text="Settings") # alias of above property.

# advanced usage
d(text="Settings").exists(timeout=3) # wait Settings appear in 3s, same as .wait(3)
```

* Retrieve the info of the specific UI object
Expand Down Expand Up @@ -943,6 +946,21 @@ You can register [watchers](http://developer.android.com/tools/help/uiautomator/
d.watchers.run()
```

* Run all watchers when page update.

通常可以用来自动点击权限确认框,或者自动安装

```python
d.watcher("OK").when(text="OK").click(text="OK")
# enable auto trigger watchers
d.watchers.watched = True

# disable auto trigger watchers
d.watchers.watched = False

# get current trigger watchers status
assert d.watchers.watched == False
```

另外文档还是有很多没有写,推荐直接去看源码[__init__.py](uiautomator2/__init__.py)

Expand All @@ -958,19 +976,19 @@ d.wait_timeout = 30.0 # default 20.0
UiAutomator中的超时设置(隐藏方法)

```python
>> d.jsonrpc.getConfigurator()
{'actionAcknowledgmentTimeout': 3000,
>> d.jsonrpc.getConfigurator()
{'actionAcknowledgmentTimeout': 500,
'keyInjectionDelay': 0,
'scrollAcknowledgmentTimeout': 200,
'waitForIdleTimeout': 10000,
'waitForSelectorTimeout': 10000}
'waitForIdleTimeout': 0,
'waitForSelectorTimeout': 0}

>> d.jsonrpc.setConfigurator({"waitForIdleTimeout": 100})
{'actionAcknowledgmentTimeout': 3000,
{'actionAcknowledgmentTimeout': 500,
'keyInjectionDelay': 0,
'scrollAcknowledgmentTimeout': 200,
'waitForIdleTimeout': 100,
'waitForSelectorTimeout': 10000}
'waitForSelectorTimeout': 0}
```

为了防止客户端程序响应超时,`waitForIdleTimeout``waitForSelectorTimeout`目前已改为`0`
Expand All @@ -988,11 +1006,28 @@ d.set_fastinput_ime(False) # 切换成正常的输入法
```

### Toast
显示Toast
Show Toast

```python
d.toast.show("Hello world")
d.toast.show("Hello world", 1.0) # show for 1.0s, default 1.0s
```

Get Toast

```python
d.make_toast("Hello world")
d.make_toast("Hello world", 1.5) # show for 1.5s
# [Args]
# 5.0: max wait timeout. Default 10.0
# 10.0: cache time. return cache toast if already toast already show up in recent 10 seconds. Default 10.0 (Maybe change in the furture)
# "default message": return if no toast finally get. Default None
d.toast.get_message(5.0, 10.0, "default message")

# common usage
assert "Short message" in d.toast.get_message(5.0, default="")

# clear cached toast
d.toast.reset()
# Now d.toast.get_message(0) is None
```

## 测试方法
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Expand Up @@ -7,3 +7,5 @@ retry>=0.9.2
whichcraft
# remove pillow because pillow can't installed on QPython
# pillow
pillow ; sys_platform == "win32" or sys_platform == "darwin"
lxml ; sys_platform == "win32" or sys_platform == "darwin"
64 changes: 64 additions & 0 deletions tests/test_toast.py
@@ -0,0 +1,64 @@
# coding: utf-8
#
# Test apk Download from
# https://github.com/appium/java-client/raw/master/src/test/java/io/appium/java_client/ApiDemos-debug.apk

import uiautomator2 as u2
import unittest
import time


class ToastTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.d = u2.connect_usb()

def setUp(self):
self.sess = self.d.session("io.appium.android.apis")

def test_toast_get_message(self):
d = self.sess
d.toast.reset()
assert d.toast.get_message(0) is None
assert d.toast.get_message(0, default="d") == "d"
d(text="App").click()
d(text="Notification").click()
d(text="NotifyWithText").click()
d(text="Show Short Notification").click()
self.assertEqual(d.toast.get_message(2, 5, ""), "Short notification")
time.sleep(.5)
self.assertIsNone(d.toast.get_message(0, 0.4))
# d.toast.reset()
# d.toast.show("Hello world")
# self.assertEqual(d.toast.get_message(5, 5), "Hello world")

def test_scroll(self):
d = self.sess
d(text="App").click()
d(scrollable=True).scroll.to(text="Voice Recognition")

def test_watchers(self):
"""
App -> Notification -> Status Bar
"""
d = self.sess
d.watchers.remove()
d.watchers.watched = False

d.watcher("N").when(text='Notification').click(text='Notification')
d(text="App").click()
d.watchers.run()
self.assertTrue(d(text="Status Bar").wait(timeout=3))
d.press("back")
d.press("back")
# Should auto click Notification when show up
self.assertFalse(d.watchers.watched)
d.watchers.watched = True
self.assertTrue(d.watchers.watched)
d(text="App").click()
self.assertTrue(d(text="Status Bar").exists(timeout=3))
d.watchers.watched = False


if __name__ == '__main__':
unittest.main()

0 comments on commit ba290cf

Please sign in to comment.