Skip to content

Commit 6d71c18

Browse files
authored
add send_keys and clear_text method (#48)
1 parent b408325 commit 6d71c18

5 files changed

Lines changed: 34 additions & 8 deletions

File tree

uiautodev/command_proxy.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pydantic import BaseModel
1414

1515
from uiautodev.command_types import AppLaunchRequest, AppTerminateRequest, By, Command, CurrentAppResponse, \
16-
DumpResponse, FindElementRequest, FindElementResponse, InstallAppRequest, InstallAppResponse, TapRequest, \
16+
DumpResponse, FindElementRequest, FindElementResponse, InstallAppRequest, InstallAppResponse, SendKeysRequest, TapRequest, \
1717
WindowSizeResponse
1818
from uiautodev.driver.base_driver import BaseDriver
1919
from uiautodev.exceptions import ElementNotFoundError
@@ -39,7 +39,7 @@ def get_command_params_type(command: Command) -> Optional[BaseModel]:
3939
return type_hints.get("params")
4040

4141

42-
def send_command(driver: BaseDriver, command: Union[str, Command], params=None):
42+
def send_command(driver: BaseDriver, command: Command, params=None):
4343
if command not in COMMANDS:
4444
raise NotImplementedError(f"command {command} not implemented")
4545
func = COMMANDS[command]
@@ -142,6 +142,14 @@ def dump(driver: BaseDriver) -> DumpResponse:
142142
def wake_up(driver: BaseDriver):
143143
driver.wake_up()
144144

145+
@register(Command.SEND_KEYS)
146+
def send_keys(driver: BaseDriver, params: SendKeysRequest):
147+
driver.send_keys(params.text)
148+
149+
@register(Command.CLEAR_TEXT)
150+
def clear_text(driver: BaseDriver):
151+
driver.clear_text()
152+
145153

146154
def node_match(node: Node, by: By, value: str) -> bool:
147155
if by == By.ID:

uiautodev/command_types.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class Command(str, enum.Enum):
3939
VOLUME_UP = "volumeUp"
4040
VOLUME_DOWN = "volumeDown"
4141
VOLUME_MUTE = "volumeMute"
42+
SEND_KEYS = "sendKeys"
43+
CLEAR_TEXT = "clearText"
4244

4345

4446
class TapRequest(BaseModel):
@@ -94,4 +96,8 @@ class FindElementRequest(BaseModel):
9496

9597
class FindElementResponse(BaseModel):
9698
count: int
97-
value: List[Node]
99+
value: List[Node]
100+
101+
102+
class SendKeysRequest(BaseModel):
103+
text: str

uiautodev/driver/android.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def volume_down(self):
127127
def volume_mute(self):
128128
self.adb_device.keyevent("VOLUME_MUTE")
129129

130-
def get_app_version(self, package_name: str) -> Optional[dict]:
130+
def get_app_version(self, package_name: str) -> dict:
131131
"""
132132
Get the version information of an app, including mainVersion and subVersion.
133133
@@ -172,11 +172,17 @@ def app_list(self) -> List[AppInfo]:
172172

173173
def open_app_file(self, package: str) -> Iterator[bytes]:
174174
line = self.adb_device.shell(f"pm path {package}")
175+
assert isinstance(line, str)
175176
if not line.startswith("package:"):
176177
raise AndroidDriverException(f"Failed to get package path: {line}")
177178
remote_path = line.split(':', 1)[1]
178179
yield from self.adb_device.sync.iter_content(remote_path)
179-
180+
181+
def send_keys(self, text: str):
182+
self.ud.send_keys(text)
183+
184+
def clear_text(self):
185+
self.ud.clear_text()
180186

181187

182188
def parse_xml(xml_data: str, wsize: WindowSize, display_id: Optional[int] = None) -> Node:

uiautodev/driver/base_driver.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,11 @@ def app_list(self) -> List[AppInfo]:
102102
def open_app_file(self, package: str) -> Iterator[bytes]:
103103
""" open app file """
104104
raise NotImplementedError()
105-
105+
106+
def send_keys(self, text: str):
107+
""" send keys to device """
108+
raise NotImplementedError()
109+
110+
def clear_text(self):
111+
""" clear text input on device """
112+
raise NotImplementedError()

uiautodev/router/proxy.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import asyncio
2+
import logging
23

34
import httpx
45
import websockets
56
from fastapi import APIRouter, HTTPException, Request, WebSocket, WebSocketDisconnect
67
from fastapi.responses import Response
7-
import logging
8-
98

109
logger = logging.getLogger(__name__)
1110
router = APIRouter()

0 commit comments

Comments
 (0)