如何在pytest-qt中处理模态对话框而不模拟对话框

时间:2019-03-25 11:16:09

标签: python pyqt4 ui-automation black-box-testing pytest-qt

我正在使用pytest-qt自动化PyQt GUI的测试。对话框需要作为测试的一部分进行处理(不应模拟对话)。

例如,必须处理单击按钮后出现的文件对话框。有两个问题

  1. 在单击按钮之后,程序控制转到事件处理程序,而不是下一行,我可以尝试将鼠标单击/击键发送到对话框。

  2. 由于QDialog未添加到主窗口小部件,因此未在主窗口小部件的子级中列出。那么如何获取QDialog的引用呢?

我尝试了多线程,但是没有用,后来我发现QObjects不是线程安全的。

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 个答案:

答案 0 :(得分:0)

可以使用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)

请参阅此link以获取更多详细信息