Handle QMessageBox in python unittest with QTest

170 Views Asked by At

I automate UI tests in python. I'm using pyqt and qtest. How handle QMessageBox and close it in unittest.

1

There are 1 best solutions below

0
KanekiSenpai On

Start timer which will looking for active modal widget in app and close it.

def test_test(self):
[...]
    # app = QApplication object
    # ui = QWidget object
    qTimer = QTimer(ui)
    qTimer.timeout.connect(handleQMessageBox)
    qTimer.start(100)
    QTest.mouseClick(oneGrButton, QtCore.Qt.LeftButton) # -> this will call QMessageBox

def handleQMessageBox():
    widget = app.activeModalWidget()
    print(widget.text())
    widget.close()