【问题标题】:How to handle modal dialog in pytest-qt without mocking the dialog如何在 pytest-qt 中处理模态对话框而不模拟对话框
【发布时间】:2019-03-25 11:16:08
【问题描述】:

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

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

  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

【问题讨论】:

  • 试试:print(QtGui.QApplication.topLevelWidgets())
  • 谢谢,@eyllanesc 我可以尝试获取对话框参考。但我需要解决第一个问题才能尝试。

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


【解决方案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)

更多详情请参考link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-27
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多