Skip to content

Commit

Permalink
实现连接策略的插件化
Browse files Browse the repository at this point in the history
  • Loading branch information
kaluluosi committed Oct 23, 2023
1 parent 28d1891 commit 31bdd16
Show file tree
Hide file tree
Showing 14 changed files with 1,046 additions and 1,008 deletions.
12 changes: 9 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
[tool.poetry]
name = "robotframework-airtest"
version = "0.1.1"
version = "0.1.2"
description = "网易Airtest/pocoui的Robotframework测试库"
authors = ["kaluluosi <[email protected]>"]
license = "MIT"
readme = "README.md"
packages = [{ include = "robotframework_airtest" }]
repository = "https://github.com/kaluluosi/robotframework-airtest"

[tool.poetry.plugins."vmg.generator"]
unity = "robotframework_airtest.vmg.generators.unity"
[tool.poetry.plugins."robotframework_airtest.vmg.generator"]
unity = "robotframework_airtest.vmg.generators.unity:generate"

[tool.poetry.plugins."robotframework_airtest.device.connect_strategy"]
android = "robotframework_airtest.device.connects.impl.android:AndroidConnectStrategy"
windows = "robotframework_airtest.device.connects.impl.windows:WindowsConnectStrategy"
unity = "robotframework_airtest.device.connects.impl.unity:UnityConnectStrategy"


[tool.poetry.scripts]
ra = "robotframework_airtest.cli:main"
Expand Down
4 changes: 2 additions & 2 deletions robotframework_airtest/device/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from airtest.core.settings import Settings
from robot.api import logger, deco
from robot.libraries.BuiltIn import BuiltIn
from .connects import ConnectStrategy, factory
from .connects import ConnectStrategyBase, factory

# 坐标点类型声明
Point = Tuple[float, float]
Expand All @@ -26,7 +26,7 @@ def __init__(
self.auto_start_app = auto_start_app or self.var("${auto_start_app}") == "True"
self.device_uri = device_uri or self.var("${device_uri}")
self.pkg_name = pkg_name or self.var("${pkg_name}")
self.conn: ConnectStrategy = None
self.conn: ConnectStrategyBase = None
logger.console(
"DeviceLibrary初始化 device_uri:{} pkg_name:{} auto_start_app:{}".format(
self.device_uri,
Expand Down
4 changes: 2 additions & 2 deletions robotframework_airtest/device/connects/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .connect_strategy import ConnectStrategy
from .connect_strategy import ConnectStrategyBase
from .connect import factory

__all__ = ["ConnectStrategy", "factory"]
__all__ = ["ConnectStrategyBase", "factory"]
41 changes: 26 additions & 15 deletions robotframework_airtest/device/connects/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,40 @@
找到匹配platform的入口,加载这个入获取ConnectStrategy类,然后用这个类来实例化具体的ConnectStrategy。
"""

import importlib
from importlib import metadata
from typing import Dict
from urllib.parse import urlparse
from .connect_strategy import ConnectStrategy
from . import impl
from .connect_strategy import ConnectStrategyBase


def factory(device_uri: str, pkg_name: str) -> ConnectStrategy:
def get_valid_connect_strategy() -> Dict[str, ConnectStrategyBase]:
"""获取所有支持的连接策略
Returns:
Dict[str,ConnectStrategy]: 支持的连接策略
"""
entry_points = metadata.entry_points().get(
"robotframework_airtest.device.connect_strategy", []
)
all_connect_strategies = {}
for ep in entry_points:
connect_strategy = ep.load()
all_connect_strategies[ep.name] = connect_strategy

return all_connect_strategies


def factory(device_uri: str, pkg_name: str) -> ConnectStrategyBase:
"""动态加载实现的工厂,用来创建连接策略实例
Returns:
ConnectStrategy: 连接策略实例
"""
res = urlparse(device_uri)
platform = res.scheme
try:
module = importlib.import_module("." + platform.lower(), impl.__package__)
cls_connect_strategy = getattr(
module, "{}ConnectStrategy".format(platform.title())
)
except Exception as e:
# 预设的连接策略里没有找到该平台的连接策略,尝试去插件里找
# TODO: 要重新实现插件,这里先直接抛错

raise RuntimeError(f"没有合适的连接策略,不支持该平台连接 {e}")
all_connect_strategies = get_valid_connect_strategy()
connect_strategy = all_connect_strategies.get(platform, None)
if connect_strategy is None:
raise RuntimeError(f"没有合适的连接策略,不支持 {platform} 平台连接")

return cls_connect_strategy(device_uri, pkg_name)
return connect_strategy(device_uri, pkg_name)
2 changes: 1 addition & 1 deletion robotframework_airtest/device/connects/connect_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from airtest.core.helper import G


class ConnectStrategy(ABC):
class ConnectStrategyBase(ABC):
def __init__(self, device_uri: str, pkg_name: str):
"""初始化连接策略
Expand Down
4 changes: 2 additions & 2 deletions robotframework_airtest/device/connects/impl/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from robot.api import logger
from airtest.core.api import connect_device
from airtest.core.helper import G
from ..connect_strategy import ConnectStrategy
from ..connect_strategy import ConnectStrategyBase


class AndroidConnectStrategy(ConnectStrategy):
class AndroidConnectStrategy(ConnectStrategyBase):
def connect(self, auto_start_app=False) -> Device:
self.device = connect_device(self.device_uri)
logger.console("设备<{device}>:连接".format(device=self.device_uri))
Expand Down
4 changes: 2 additions & 2 deletions robotframework_airtest/device/connects/impl/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from robot.api import logger

from ..connect_strategy import ConnectStrategy
from ..connect_strategy import ConnectStrategyBase


DLL_PATH = pkg_resources.resource_filename(robotframework_airtest.__package__, "dll")
Expand Down Expand Up @@ -130,7 +130,7 @@ def _snapshot(self, *args, **kwargs):
return win_device


class WindowsConnectStrategy(ConnectStrategy):
class WindowsConnectStrategy(ConnectStrategyBase):
def connect(self, auto_start_app: bool = False) -> Device:
if auto_start_app:
# 尝试连接一下看是不是正有一个客户端开着
Expand Down
12 changes: 7 additions & 5 deletions robotframework_airtest/reporter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@


class AirtestReporter:
"""这个模块是Robotframework监听器,通过再robot命令后面追加
--listener robotframework_airtest.reporter.AirtestReporter,可以在测试执行的时候
生成airtest的测试报告。
"""
Airtest日志导出报告监听器
NOTE: 如果robot没有指明这个监听器,那么将不会导出airtest的报告也不会插入到robot的报告里
通过再robot命令后面追加
`--listener robotframework_airtest.reporter.AirtestReporter` 可以在测试执行的时候生成airtest的测试报告。
Returns:
[type]: [description]
"""

ROBOT_LISTENER_API_VERSION = 2
Expand Down
10 changes: 6 additions & 4 deletions robotframework_airtest/vmg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@


def get_valid_generators() -> dict:
entrypoints = metadata.entry_points().get("vmg.generator", [])
entrypoints = metadata.entry_points().get(
"robotframework_airtest.vmg.generator", []
)
all_generators = {}
for ep in entrypoints:
try:
module: ModuleType = ep.load()
all_generators[ep.name] = getattr(module, "generate")
generator = ep.load()
all_generators[ep.name] = generator
except Exception as e:
logger.error(f"{module.__name__} {e}")
logger.error(f"{ep.__name__} {e}")

return all_generators

Expand Down
Loading

0 comments on commit 31bdd16

Please sign in to comment.