【问题标题】:simulating mouse click in menu bar and toolbar using Qtbot使用 Qtbot 在菜单栏和工具栏中模拟鼠标单击
【发布时间】:2019-06-30 01:35:04
【问题描述】:
我是使用 PYtestqt 在 PYQT5 中开发的记事本 GUI 自动化。当我尝试使用 qtbot 单击菜单栏或工具栏选项时,它没有模拟单击
def test_quit(qtbot):
window = MainWindow()
qtbot.add_widget(window)
window.show()
qtbot.wait_for_window_shown(window)
qtbot.mouseClick(window.file_menu, QtCore.Qt.LeftButton)
【问题讨论】:
标签:
python-3.x
pyqt5
pytest-qt
【解决方案1】:
我试图找到一种方法来触发菜单下的操作。由于您很可能想要触发一个动作(菜单下的项目),这也可能对您有所帮助。不使用qtbot,直接使用window,调用trigger。所以是这样的:
def test_quit(qtbot):
window = MainWindow()
qtbot.add_widget(window)
window.show()
qtbot.wait_for_window_shown(window)
window.file_quit_action.trigger()
【解决方案2】:
如果你只想触发点击事件连接动作,你可以试试这个。
在 MainWindow 类中:
# binding action and function
action_open = QAction("Open", self)
action_open.setCheckable(False)
action_open.setObjectName("action_open") # Notice set a object name
action_open.triggered.connect(self.file_open)
在测试脚本中:
def test_file_open_action(qtbot):
window = MainWindow()
qtbot.add_widget(window)
window.show()
win.findChild(QAction, 'action_open').trigger() # call the method
所以你在模拟click memubar widget后可以得到同样的结果。