如何在不模拟对话框的情况下处理 pytest-qt 中的模态对话框 [英] How to handle modal dialog in pytest-qt without mocking the dialog

查看:214
本文介绍了如何在不模拟对话框的情况下处理 pytest-qt 中的模态对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 在按钮单击命令之后,程序控制转到事件处理程序,而不是转到下一行,我可以尝试将鼠标单击/击键发送到对话框.

  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.

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

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?

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

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

推荐答案

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

请参阅此链接了解更多详情

这篇关于如何在不模拟对话框的情况下处理 pytest-qt 中的模态对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆