Skip to content Skip to sidebar Skip to footer

How To Click On Qmessagebox With Pytest-qt?

I am creating some unit tests for a PyQt application with pytest-qt. And I would like to create open the graphical window, do some tests then close the window, rather than open a n

Solution 1:

In your attempt you are creating a new QMessageBox that is different from the one created with the static method QMessageBox::question() so even if you click it will not work.

The idea is to obtain the QMessageBox shown, and in this case we will take advantage of that since it is the active window so we can obtain it using QApplication::activeWindow(). Another way to get the QMessageBox is to use the relationship between imageViewer and the QMessageBox through findChild():

@pytest.fixture(scope="module")defViewer(request):
    print("  SETUP GUI")

    app, imageViewer = GUI.main_GUI()
    qtbotbis = QtBot(app)
    QtTest.QTest.qWait(0.5 * 1000)

    yield app, imageViewer, qtbotbis

    defhandle_dialog():
        messagebox = QtWidgets.QApplication.activeWindow()
        # or# messagebox = imageViewer.findChild(QtWidgets.QMessageBox)
        yes_button = messagebox.button(QtWidgets.QMessageBox.Yes)
        qtbotbis.mouseClick(yes_button, QtCore.Qt.LeftButton, delay=1)

    QtCore.QTimer.singleShot(100, handle_dialog)
    qtbotbis.mouseClick(imageViewer.btn_quit, QtCore.Qt.LeftButton, delay=1)
    assert imageViewer.isHidden()

Post a Comment for "How To Click On Qmessagebox With Pytest-qt?"