diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a3a1838 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "python.testing.pytestArgs": [ + "tests" + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true +} diff --git a/__pycache__/conftest.cpython-313-pytest-8.3.5.pyc b/__pycache__/conftest.cpython-313-pytest-8.3.5.pyc new file mode 100644 index 0000000..a3c1921 Binary files /dev/null and b/__pycache__/conftest.cpython-313-pytest-8.3.5.pyc differ diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000..0892767 --- /dev/null +++ b/conftest.py @@ -0,0 +1,14 @@ +import logging +import pytest + + +# 在测试开始前配置日志 +@pytest.fixture(autouse=True, scope="session") +def configure_logging(): + logging.basicConfig( + level=logging.INFO, + format="%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)", + datefmt="%Y-%m-%d %H:%M:%S", + ) + # 返回一个日志对象供测试函数使用 + return logging.getLogger() diff --git a/examples/ziniao-login.py b/examples/ziniao-login.py new file mode 100644 index 0000000..880cf3b --- /dev/null +++ b/examples/ziniao-login.py @@ -0,0 +1,13 @@ +import sys +import os + +sys.path.append( + os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "src") +) + + +from ziniao import Ziniao + +if __name__ == "__main__": + ziniao = Ziniao() + ziniao.launch_ziniao() diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..44e1c32 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,11 @@ +[pytest] +# 显示日志输出 +log_cli = True +# 设置日志级别 +log_cli_level = INFO +# 日志格式 +log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s) +# 时间戳格式 +log_cli_date_format = %Y-%m-%d %H:%M:%S +# 捕获stdout/stderr输出 +addopts = -v --capture=tee-sys diff --git a/src/__pycache__/ziniao.cpython-313.pyc b/src/__pycache__/ziniao.cpython-313.pyc new file mode 100644 index 0000000..dee8144 Binary files /dev/null and b/src/__pycache__/ziniao.cpython-313.pyc differ diff --git a/src/ziniao.py b/src/ziniao.py index 058c2d2..a5e9729 100644 --- a/src/ziniao.py +++ b/src/ziniao.py @@ -1,7 +1,13 @@ +import os +import time import winreg +import win32gui + +import subprocess + class Ziniao: - def __init__(self, name): + def __init__(self): pass def login(self, username, password): @@ -16,22 +22,64 @@ class Ziniao: def close(self): pass + def launch_ziniao(self): + """启动紫鸟并等待窗口出现""" + exe_path = self.get_ziniao_exe_path() + # 启动紫鸟的exe文件, 独立进程 + subprocess.Popen( + exe_path, creationflags=0x01000000 | 0x00000008, close_fds=True + ) + + # 延迟导入win32gui以避免在测试收集阶段崩溃 + try: + + # 等待窗口出现 + max_wait_time = 60 # 最大等待时间(秒) + start_time = time.time() + + print("等待紫鸟窗口出现...") + + # 延迟导入 + + # 使用win32gui查找窗口 + def enum_windows_callback(hwnd, found_windows): + if win32gui.IsWindowVisible(hwnd): + window_title = win32gui.GetWindowText(hwnd) + if "紫鸟" in window_title: + found_windows.append(hwnd) + return True + + while time.time() - start_time < max_wait_time: + found_windows = [] + win32gui.EnumWindows(enum_windows_callback, found_windows) + if found_windows: + print("紫鸟窗口已出现") + return + time.sleep(1) # 每秒检查一次 + + if time.time() - start_time >= max_wait_time: + print("紫鸟窗口未在规定时间内出现") + + except ImportError: + print("警告: 未安装win32gui模块,无法检测紫鸟窗口出现") + except Exception as e: + print(f"检测窗口出错: {e}") + def get_ziniao_exe_path(self): - """ - Returns: - str: The DisplayIcon value if found, None otherwise - """ + """返回紫鸟的安装路径""" try: # Open the registry key reg_path = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\8c327052-d9e8-5248-931f-e5b8c47f82b5" key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, reg_path) - # Query the DisplayIcon value - display_icon, _ = winreg.QueryValueEx(key, "DisplayIcon") + # Query the UninstallString value + uninstall_string, _ = winreg.QueryValueEx(key, "UninstallString") winreg.CloseKey(key) - return display_icon - except FileNotFoundError: - return "Registry key not found" - except WindowsError as e: - return f"Error accessing registry: {e}" + uninstall_exe_path = uninstall_string.rsplit(" ", 1)[0].strip('"') + os.path.split(uninstall_exe_path)[0] # 获取路径部分 + exe_path = os.path.join(os.path.split(uninstall_exe_path)[0], "ziniao.exe") + + return exe_path + except Exception as e: + raise RuntimeError(f"获取紫鸟EXE路径失败: {e}") from e diff --git a/tests/__pycache__/test_ziniao.cpython-313-pytest-8.3.5.pyc b/tests/__pycache__/test_ziniao.cpython-313-pytest-8.3.5.pyc new file mode 100644 index 0000000..0e6b4f4 Binary files /dev/null and b/tests/__pycache__/test_ziniao.cpython-313-pytest-8.3.5.pyc differ diff --git a/tests/test-ziniao.py b/tests/test-ziniao.py new file mode 100644 index 0000000..84e3534 --- /dev/null +++ b/tests/test-ziniao.py @@ -0,0 +1,37 @@ +import os +import pytest +import logging + +from src.ziniao import Ziniao + + +@pytest.fixture +def ziniao_instance(): + """创建并返回一个Ziniao实例,可以被多个测试函数重复使用""" + return Ziniao() + + +def test_get_ziniao_exe_path(ziniao_instance): + """测试get_ziniao_exe_path方法是否返回合法的可执行文件路径""" + exe_path = ziniao_instance.get_ziniao_exe_path() + # 使用pytest的日志功能替代print + logging.info(f"紫鸟EXE路径: {exe_path}") + assert isinstance(exe_path, str) + assert len(exe_path) > 0 + # 验证路径是否存在(如果此测试在未安装紫鸟的环境中可能会失败) + assert os.path.exists(exe_path) + + +def test_launch_ziniao(ziniao_instance): + """测试launch_ziniao方法是否能成功启动紫鸟""" + try: + ziniao_instance.launch_ziniao() + logging.info("紫鸟启动成功") + except Exception as e: + pytest.fail(f"启动紫鸟失败: {e}") + + +if __name__ == "__main__": + # 保留原来的手动测试代码,但仅在直接运行此文件时执行 + ziniao_instance = Ziniao() + print(f"紫鸟EXE路径: {ziniao_instance.get_ziniao_exe_path()}") diff --git a/tests/test_ziniao.py b/tests/test_ziniao.py deleted file mode 100644 index 8b13789..0000000 --- a/tests/test_ziniao.py +++ /dev/null @@ -1 +0,0 @@ -