如何让Squish Java GUI测试自动适配Windows系统主鼠标按钮?
解决Squish适配Windows主鼠标按钮的问题
优先方案:用click()替代mouseClick()
Squish的click()是高层API,模拟的是用户的"主按钮点击"逻辑动作,会自动适配Windows的鼠标主键设置(不管你把左键还是右键设为主键)。
替换原来硬编码左键的代码:
# 原代码(硬指定硬件左键) mouseClick(waitForObject("your_target_component"), Button=LeftButton) # 替换为 click(waitForObject("your_target_component"))
这个方法零额外逻辑,直接解决问题,是最推荐的方式。
进阶方案:动态检测系统鼠标设置适配mouseClick()
如果因为需要指定点击坐标、修饰键等场景必须用mouseClick(),可以通过读取Windows注册表获取鼠标键交换状态,动态选择点击按钮:
import _winreg def get_primary_mouse_button(): try: # 读取Windows用户鼠标设置注册表项 key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, r"Control Panel\Mouse") swap_status, _ = _winreg.QueryValueEx(key, "SwapMouseButtons") _winreg.CloseKey(key) # SwapMouseButtons值为"1"表示右键是主键,"0"为默认左键 return "RightButton" if swap_status == "1" else "LeftButton" except Exception as e: # 读取失败时 fallback 到左键 print(f"Failed to fetch mouse settings: {e}") return "LeftButton" # 使用示例:带坐标的点击 primary_btn = get_primary_mouse_button() mouseClick(waitForObject("your_target_component"), 15, 20, Button=primary_btn)
不推荐方案:修改Squish全局配置
部分旧版Squish支持通过配置文件调整鼠标映射,但这种方式兼容性差,且无法应对不同测试环境的鼠标设置差异,远不如前两种脚本层面的方案灵活可靠。
内容的提问来源于stack exchange,提问作者LJenkins
相关产品推荐
相关产品推荐

