How to handle modal dialog in pytest-qt without mocking the dialog

空扰寡人 提交于 2021-01-29 07:30:47

问题


I am using pytest-qt to automate the testing of a PyQt GUI. The dialogs need to be handled as a part of the testing(dialogs should not be mocked).

For example, file dialog that comes after a button-click has to be handled. There are 2 problems

  1. After the button click command, the program control goes to the event handler and not to the next line where I can try to send mouseclick/keystrokes to the dialog.

  2. Since the QDialog is not added to the main widget, it is not being listed among the children of the main widget. So how to get the reference of the QDialog?

I tried multi-threading but that didn't work, later I found that QObjects are not thread-safe.

def test_filedialog(qtbot, window):
    qtbot.mouseClick(window.browseButton, QtCore.Qt.LeftButton, delay=1)
    print("After mouse click")
    #This is where I need to get the reference of QDialog and handle it

回答1:


It can be done using QTimer.

def test_filedialog(qtbot, window):
    def handle_dialog():
        # get a reference to the dialog and handle it here
    QTimer.singleShot(500, handle_dialog)
    qtbot.mouseClick(window.browseButton, QtCore.Qt.LeftButton, delay=1)

Refer this link for more details



来源:https://stackoverflow.com/questions/55336575/how-to-handle-modal-dialog-in-pytest-qt-without-mocking-the-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!