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

Python 如何在pytest qt中处理模态对话框而不模拟对话框,python,pyqt4,ui-automation,black-box-testing,pytest-qt,Python,Pyqt4,Ui Automation,Black Box Testing,Pytest Qt,我正在使用pytest-qt自动化PyQt GUI的测试。对话框需要作为测试的一部分进行处理(不应模拟对话框) 例如,必须处理按钮单击后出现的文件对话框。有两个问题 单击按钮命令后,程序控件将转到事件处理程序,而不是下一行,在该行中,我可以尝试向对话框发送鼠标单击/按键 由于QDialog没有添加到主小部件中,因此它没有列在主小部件的子部件中。那么如何获得QDialog的引用呢 我尝试了多线程,但没有成功,后来我发现QObject不是线程安全的 def test_filedialog(qtbot

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

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

  • 单击按钮命令后,程序控件将转到事件处理程序,而不是下一行,在该行中,我可以尝试向对话框发送鼠标单击/按键

  • 由于QDialog没有添加到主小部件中,因此它没有列在主小部件的子部件中。那么如何获得QDialog的引用呢

  • 我尝试了多线程,但没有成功,后来我发现QObject不是线程安全的

    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
    

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

    有关更多详细信息,请参阅此可以使用
    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)
    

    有关更多详细信息,请参阅此

    尝试使用:
    print(QtGui.QApplication.topLevelWidgets())
    谢谢,@eyllansc我可以尝试使用它来获取对话框参考。但我需要解决第一个问题才能尝试此操作。请尝试:
    print(QtGui.QApplication.topLevelWidgets())
    谢谢,@eyllansc我可以尝试此操作以获取对话框引用。但是我需要解决第一个问题来尝试这个。