52 lines
2.0 KiB
Python
52 lines
2.0 KiB
Python
"""UI Automation helpers scoped to one toolbar process."""
|
|
from pywinauto import Application
|
|
from packaged_menu import wait_for
|
|
|
|
TOOLBAR_TITLE = "一叶知秋悬浮工具栏"
|
|
PANEL_TITLE = "一叶知秋_重构版"
|
|
PRIMARY_FEATURES = ("自动位号", "属性批改", "属性显示", "设计检查", "网络工具", "BOM 工具", "PDF 导出")
|
|
|
|
class ToolbarUI:
|
|
def __init__(self, pid):
|
|
self.app = Application(backend="uia").connect(process=pid, timeout=15)
|
|
self.toolbar = self.app.window(title=TOOLBAR_TITLE)
|
|
self.panel = self.app.window(title=PANEL_TITLE)
|
|
self.toolbar.wait("visible", timeout=15)
|
|
|
|
def menu_action(self, name):
|
|
more = next(button for button in self.toolbar.descendants(control_type="Button")
|
|
if button.window_text() in ("更多", "更多功能"))
|
|
more.invoke()
|
|
def find_action():
|
|
for window in self.app.windows():
|
|
for item in window.descendants(control_type="MenuItem"):
|
|
if item.window_text() == name:
|
|
return item
|
|
wait_for(find_action, 5).invoke()
|
|
|
|
def feature(self, name):
|
|
button = self.toolbar.child_window(title=name, control_type="Button")
|
|
if button.exists(timeout=0.2) and button.is_visible():
|
|
button.invoke()
|
|
else:
|
|
self.menu_action(name)
|
|
self.panel.wait("visible", timeout=15)
|
|
assert name in self.texts(self.panel)
|
|
|
|
@staticmethod
|
|
def texts(window):
|
|
return [item.window_text() for item in window.descendants(control_type="Text")]
|
|
|
|
def collapse(self):
|
|
self.toolbar.child_window(title="收起面板", control_type="Button").invoke()
|
|
self.panel.wait_not("visible", timeout=10)
|
|
assert self.toolbar.is_visible()
|
|
|
|
def expand(self):
|
|
self.toolbar.child_window(title="展开面板", control_type="Button").invoke()
|
|
self.panel.wait("visible", timeout=10)
|
|
|
|
def exit(self):
|
|
self.menu_action("退出工具")
|
|
self.toolbar.wait_not("exists", timeout=15)
|