[英]How to handle modal dialog in pytest-qt without mocking the dialog
I am using pytest-qt to automate the testing of a PyQt GUI. 我使用pytest-qt自动测试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
有两个问题
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. 按钮单击命令后,程序控件转到事件处理程序,而不是下一行,我可以尝试向对话框发送鼠标点击/按键。
Since the QDialog is not added to the main widget, it is not being listed among the children of the main widget. 由于QDialog未添加到主窗口小部件,因此它不会列在主窗口小部件的子项中。 So how to get the reference of the QDialog?
那么如何获得QDialog的参考?
I tried multi-threading but that didn't work, later I found that QObjects are not thread-safe. 我尝试了多线程,但是没有用,后来我发现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
It can be done using QTimer
. 它可以使用
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.