Skip to content

Commit

Permalink
Fix dump with new apk version (#964)
Browse files Browse the repository at this point in the history
* fix app_current currentPackageName sometimes empty
* try to fix dump_hierarchy error
* use new apk version to fix dump error
  • Loading branch information
codeskyblue authored May 1, 2024
1 parent 78cb8bc commit 33fbf00
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ cov:
--cov-report xml \
--cov-report term

sync:
cd uiautomator2/assets; ./sync.sh; cd -

build:
poetry self add "poetry-dynamic-versioning[plugin]"
cd uiautomator2/assets; ./sync.sh; cd -
Expand All @@ -24,3 +27,4 @@ init:
wget https://github.com/appium/appium/raw/master/packages/appium/sample-code/apps/ApiDemos-debug.apk; \
fi
poetry run python -m adbutils -i ./ApiDemos-debug.apk

4 changes: 4 additions & 0 deletions docs/2to3.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,7 @@ print(d.device_info)
'arch': 'arm64-v8a',
'version': 12}
```

### app_current
- 2.x raise `OSError` if couldn't get focused app
- 3.x raise `DeviceError` if couldn't get focused app
37 changes: 27 additions & 10 deletions uiautomator2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from uiautomator2 import xpath
from uiautomator2._proto import HTTP_TIMEOUT, SCROLL_STEPS, Direction
from uiautomator2._selector import Selector, UiObject
from uiautomator2.exceptions import AdbShellError, BaseException, HierarchyEmptyError, SessionBrokenError
from uiautomator2.exceptions import AdbShellError, BaseException, DeviceError, HierarchyEmptyError, SessionBrokenError
from uiautomator2.settings import Settings
from uiautomator2.swipe import SwipeExt
from uiautomator2.utils import list2cmdline
Expand Down Expand Up @@ -133,7 +133,7 @@ def shell(self, cmdargs: Union[str, List[str]], timeout=60) -> ShellResponse:
raise AdbShellError(e)

@property
def info(self):
def info(self) -> Dict[str, Any]:
return self.jsonrpc.deviceInfo(http_timeout=10)

@property
Expand Down Expand Up @@ -442,14 +442,10 @@ def press(self, key: Union[int, str], meta=None):
delete(or del), recent(recent apps), volume_up, volume_down,
volume_mute, camera, power.
"""
if key in ("home", "back", "enter", "volume_up", "volume_down", "volume_mute", "power", "wakeup"):
self.keyevent(key)
return
if isinstance(key, int):
return self.jsonrpc.pressKeyCode(
key, meta) if meta else self.jsonrpc.pressKeyCode(key)
else:
# FIXME: not working with Huiawei P50
return self.jsonrpc.pressKey(key)

def screen_on(self):
Expand Down Expand Up @@ -636,15 +632,15 @@ def app_current(self):
dict(package, activity, pid?)
Raises:
OSError
DeviceError
For developer:
Function reset_uiautomator need this function, so can't use jsonrpc here.
"""
info = self.adb_device.app_current()
if info:
return dataclasses.asdict(info)
raise OSError("Couldn't get focused app")
raise DeviceError("Couldn't get focused app")

def app_install(self, data: str):
"""
Expand Down Expand Up @@ -1022,6 +1018,29 @@ def swipe_ext(self) -> SwipeExt:
class Device(_Device, _AppMixIn, _PluginMixIn, _InputMethodMixIn, _DeprecatedMixIn):
""" Device object """

@property
def info(self) -> Dict[str, Any]:
""" return device info, make sure currentPackageName is set
Return example:
{'currentPackageName': 'io.appium.android.apis',
'displayHeight': 720,
'displayRotation': 3,
'displaySizeDpX': 780,
'displaySizeDpY': 360,
'displayWidth': 1560,
'productName': 'ELE-AL00',
'screenOn': True,
'sdkInt': 29,
'naturalOrientation': False}
"""
_info = super().info
if _info.get('currentPackageName') is None:
try:
_info['currentPackageName'] = self.app_current().get('package')
except DeviceError:
pass
return _info

class Session(Device):
"""Session keeps watch the app status
Expand Down Expand Up @@ -1061,8 +1080,6 @@ def __exit__(self, exc_type, exc_val, exc_tb):
self.close()




def connect(serial: Union[str, adbutils.AdbDevice] = None) -> Device:
"""
Args:
Expand Down
6 changes: 3 additions & 3 deletions uiautomator2/assets/sync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

set -e

APK_VERSION="2.3.3"
APK_VERSION="2.3.6"
# AGENT_VERSION="0.10.1"

cd "$(dirname $0)"
Expand All @@ -22,7 +22,7 @@ function download_apk(){
VERSION=$1
NAME=$2
URL="https://github.com/openatx/android-uiautomator-server/releases/download/$VERSION/$NAME"
echo "$URL"
echo ">> download $URL -> $NAME"
curl -L "$URL" --output "$NAME"
unzip -tq "$NAME"
}
Expand All @@ -33,4 +33,4 @@ function download_apk(){
download_apk "$APK_VERSION" "app-uiautomator.apk"
download_apk "$APK_VERSION" "app-uiautomator-test.apk"

echo "apk_version: $APK_VERSION" > version.txt
echo "apk_version: $APK_VERSION" > version.txt
7 changes: 6 additions & 1 deletion uiautomator2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,16 @@ def _jsonrpc_call(dev: adbutils.AdbDevice, method: str, params: Any, timeout: fl
logger.debug("jsonrpc error: %s", data)
code = data['error'].get('code')
message = data['error'].get('message')
stacktrace = data['error'].get('data')
if "UiAutomation not connected" in r.text:
raise UiAutomationNotConnectedError("UiAutomation not connected")
if "android.os.DeadObjectException" in message:
# https://developer.android.com/reference/android/os/DeadObjectException
raise UiAutomationNotConnectedError("android.os.DeadObjectException")
if "uiautomator.UiObjectNotFoundException" in message:
raise UiObjectNotFoundError(code, message, params)
raise RPCUnknownError(f"Unknown RPC error: {code} {message}", params)
raise RPCUnknownError(f"Unknown RPC error: {code} {message}", params, stacktrace)


if "result" not in data:
raise RPCInvalidError("Unknown RPC error: no result field")
Expand Down

0 comments on commit 33fbf00

Please sign in to comment.