Skip to content

Caicai000 master (for harmony) #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,5 @@ cython_debug/
#.idea/

poetry.lock
window_dump.xml
window_dump.xml
.DS_Store
59 changes: 59 additions & 0 deletions e2etests/test_harmony_driver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# coding: utf-8
#
# 参考:https://github.com/codematrixer/awesome-hdc

import pytest

from uiautodev.driver.harmony import HDC
from uiautodev.driver.harmony import HarmonyDriver

@pytest.fixture
def hdc() -> HDC:
return HDC()

@pytest.fixture
def serial(hdc: HDC) -> str:
devices = hdc.list_device()
assert len(devices) == 1
return devices[0]


def test_list_device(hdc: HDC):
devices = hdc.list_device()
assert len(devices) == 1


def test_shell(hdc: HDC, serial: str):
assert hdc.shell(serial, 'pwd') == '/'

def test_get_model(hdc: HDC, serial: str):
assert hdc.get_model(serial) == 'ohos'


def test_screenshot(hdc: HDC, serial: str):
image = hdc.screenshot(serial)
assert image is not None
assert image.size is not None


def test_dump_layout(hdc: HDC, serial: str):
layout = hdc.dump_layout(serial)
assert layout is not None
assert isinstance(layout, dict)


@pytest.fixture
def driver(hdc: HDC, serial: str) -> HarmonyDriver:
return HarmonyDriver(hdc, serial)


def test_window_size(driver: HarmonyDriver):
size = driver.window_size()
assert size.width > 0
assert size.height > 0


def test_dump_hierarchy(driver: HarmonyDriver):
xml, hierarchy = driver.dump_hierarchy()
assert xml is not None
assert hierarchy is not None
16 changes: 11 additions & 5 deletions uiautodev/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
from pathlib import Path
from typing import List

import uvicorn
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, JSONResponse, RedirectResponse
from pydantic import BaseModel
import uvicorn

from uiautodev import __version__
from uiautodev.common import convert_bytes_to_image, get_webpage_url, ocr_image
from uiautodev.model import Node
from uiautodev.provider import AndroidProvider, IOSProvider, MockProvider
from uiautodev.provider import AndroidProvider, HarmonyProvider, IOSProvider, MockProvider
from uiautodev.router.device import make_router
from uiautodev.router.xml import router as xml_router
from uiautodev.utils.envutils import Environment
Expand All @@ -39,16 +39,19 @@

android_router = make_router(AndroidProvider())
ios_router = make_router(IOSProvider())
harmony_router = make_router(HarmonyProvider())
mock_router = make_router(MockProvider())

app.include_router(mock_router, prefix="/api/mock", tags=["mock"])

if Environment.UIAUTODEV_MOCK:
app.include_router(mock_router, prefix="/api/android", tags=["mock"])
app.include_router(mock_router, prefix="/api/ios", tags=["mock"])
app.include_router(mock_router, prefix="/api/harmony", tags=["mock"])
else:
app.include_router(android_router, prefix="/api/android", tags=["android"])
app.include_router(ios_router, prefix="/api/ios", tags=["ios"])
app.include_router(harmony_router, prefix="/api/harmony", tags=["harmony"])

app.include_router(xml_router, prefix="/api/xml", tags=["xml"])

Expand All @@ -61,6 +64,7 @@ class InfoResponse(BaseModel):
cwd: str
drivers: List[str]


@app.get("/api/info")
def info() -> InfoResponse:
"""Information about the application"""
Expand All @@ -70,16 +74,18 @@ def info() -> InfoResponse:
platform=platform.system(), # Linux | Darwin | Windows
code_language="Python",
cwd=os.getcwd(),
drivers=["android", "ios"],
drivers=["android", "ios", "harmony"],
)


@app.post('/api/ocr_image')
async def _ocr_image(file: UploadFile = File(...)) -> List[Node]:
"""OCR an image"""
image_data = await file.read()
image = convert_bytes_to_image(image_data)
return ocr_image(image)


@app.get("/shutdown")
def shutdown() -> str:
"""Shutdown the server"""
Expand All @@ -88,7 +94,7 @@ def shutdown() -> str:


@app.get("/demo")
def demo() -> str:
def demo():
"""Demo endpoint"""
static_dir = Path(__file__).parent / "static"
print(static_dir / "demo.html")
Expand All @@ -104,4 +110,4 @@ def index_redirect():


if __name__ == '__main__':
uvicorn.run("uiautodev.app:app", port=4000, reload=True, use_colors=True)
uvicorn.run("uiautodev.app:app", port=4000, reload=True, use_colors=True)
2 changes: 1 addition & 1 deletion uiautodev/command_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
WindowSizeResponse
from uiautodev.driver.base_driver import BaseDriver
from uiautodev.exceptions import ElementNotFoundError
from uiautodev.model import Node, AppInfo
from uiautodev.model import AppInfo, Node
from uiautodev.utils.common import node_travel

COMMANDS: Dict[Command, Callable] = {}
Expand Down
1 change: 1 addition & 0 deletions uiautodev/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import locale
import logging
from typing import List

from PIL import Image

from uiautodev.model import Node, OCRNode
Expand Down
2 changes: 1 addition & 1 deletion uiautodev/driver/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from uiautodev.command_types import CurrentAppResponse
from uiautodev.driver.base_driver import BaseDriver
from uiautodev.exceptions import AndroidDriverException, RequestError
from uiautodev.model import Node, AppInfo, Rect, ShellResponse, WindowSize
from uiautodev.model import AppInfo, Node, Rect, ShellResponse, WindowSize
from uiautodev.utils.common import fetch_through_socket

logger = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion uiautodev/driver/base_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pydantic import BaseModel

from uiautodev.command_types import CurrentAppResponse
from uiautodev.model import Node, AppInfo, ShellResponse, WindowSize
from uiautodev.model import AppInfo, Node, ShellResponse, WindowSize


class BaseDriver(abc.ABC):
Expand Down
Loading
Loading