3

I'm automation a notepad GUI developed in PYQT5 using PYtestqt. when I try to click the menu bar or toolbar options using qtbot it is not simulating the click

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)

2 Answers2

0

I was trying to find a way to trigger an action under a menu. Since you most likely want to trigger an action (item under the menu), this may help you as well. Instead of using qtbot, use the window directly and call trigger. So something like this:

    def test_quit(qtbot):
        window = MainWindow()
        qtbot.add_widget(window)
        window.show()
        qtbot.wait_for_window_shown(window)
        window.file_quit_action.trigger()
ak22
  • 158
  • 2
  • 14
0

if you wanna just trigger the click event connected action, you can try this.

In MainWindow class:

    # 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)

In test scripts:

def test_file_open_action(qtbot):
    window = MainWindow()
    qtbot.add_widget(window)
    window.show()
    win.findChild(QAction, 'action_open').trigger()  # call the method

So you can get the same result after simulate click memubar widget.