Files
yiyezhqiu-refactored/tools/acceptance/toolbar_ui.py
T

76 lines
3.3 KiB
Python

"""UI Automation helpers scoped to one toolbar process."""
from pywinauto import Application
import win32gui
import win32process
from packaged_menu import wait_for
TOOLBAR_TITLE = "一叶知秋悬浮工具栏"
PANEL_TITLE = "一叶知秋_重构版"
PRIMARY_FEATURES = ("自动位号", "属性批改", "属性显示", "设计检查", "网络工具", "BOM 工具", "PDF 导出")
class ToolbarUI:
def __init__(self, pid):
self.pid = 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):
overflow = [feature for feature in PRIMARY_FEATURES
if not self.toolbar.child_window(title=feature).exists(timeout=0.1)]
actions = overflow + ['操作历史', '设置', '刷新设计快照', '切换明暗主题', '退出工具']
assert name in actions, (name, actions)
more = next(button for button in self.toolbar.descendants(control_type="Button")
if button.window_text() in ("更多", "更多功能"))
more.click_input()
def popup_handle():
handles = []
def inspect(handle, _):
if (win32process.GetWindowThreadProcessId(handle)[1] == self.pid
and win32gui.IsWindowVisible(handle)
and 'Popup' in win32gui.GetClassName(handle)):
handles.append(handle)
win32gui.EnumWindows(inspect, None)
return handles[0] if handles else None
popup = wait_for(popup_handle, 5)
# Qt menu actions lack a UIA Invoke provider. Address the owned popup
# directly so keyboard events never reach the user's host application.
for key in [0x24] + [0x28] * actions.index(name) + [0x0D]:
win32gui.PostMessage(popup, 0x100, key, 0)
win32gui.PostMessage(popup, 0x101, key, 0)
wait_for(lambda: not win32gui.IsWindowVisible(popup), 5)
def feature(self, name):
button = self.toolbar.child_window(title=name)
if button.exists(timeout=0.2) and button.is_visible():
button.click_input()
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 status_detail(self):
for item in self.toolbar.descendants(control_type='Text'):
if item.window_text().startswith(('● ', '○ ')):
element = item.element_info._element
return str(element.GetCurrentPropertyValue(30159) or element.GetCurrentPropertyValue(30013) or '')
return ''
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)