如何使用QTest发送菜单项的键盘快捷键? [英] How to send a keyboard shortcut for a menu item with QTest?

查看:173
本文介绍了如何使用QTest发送菜单项的键盘快捷键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在单元测试中,我试图发送键盘快捷键Command + N(在Mac OS上),该快捷方式与我的应用程序中的菜单项相对应.我正在使用PySide.QtTest模块.

In a unit test, I'm trying to send the keyboard shortcut Command+N (on Mac OS), which corresponds to a menu item in my app. I'm using the PySide.QtTest module.

在下面的代码中,我使用的是QTest.keyClicks,它不能产生预期的效果.快捷方式对应的操作不会被调用.

In the code below, I'm using QTest.keyClicks, which doesn't produce what I'm expecting. The action corresponding to the shortcut is not called.

class AppTestCase(TestCase):

    def setUp(self):
        qApp = QApplication.instance()
        if qApp is None:
            self.app = QApplication([])
        else:
            self.app = qApp

class IdfEditorTestCase(th.AppTestCase):

    def setUp(self):
        super(IdfEditorTestCase, self).setUp()
        self.window = IdfEditorWindow()

    def test_input_object_in_new_file(self):
        if os.path.exists("current_running_test.idf"):
            os.remove("current_running_test.idf")

        self.window.selectClass("ScheduleTypeLimits")
        QTest.keyClicks(self.window, "n", Qt.ControlModifier)
        self.window.saveFileAs("current_running_test.idf")
        self.assertIdfFileContentEquals("current_running_test.idf", "ScheduleTypeLimits,,,,,;\n")

一些问题:

  • 我应该将其发送到窗口本身吗?还是到菜单栏?似乎都不起作用...
  • 发送键盘快捷键是否正确?

推荐答案

对于常规"键单击测试(例如在行编辑中输入文本),没有必要显示窗口.这与您在正常运行应用程序期间将键事件发送到隐藏的小部件时所期望的一致.

For "normal" key-click tests (like entering text in a line-edit), it is not necessary to show the window. This is in line with what you'd expect if you sent key events to a hidden widget during normal running of the application.

但是对于测试快捷方式,必须显示目标窗口 -再次符合您的期望.如果目标窗口不可见,则键盘快捷键不应在正常运行期间激活命令.

But for testing shortcuts, the target window must be shown - which is again in line with what you'd expect. A keyboard shortcut should not activate commands during normal running if the target window is not visible.

因此,您的设置代码可能应包含以下内容:

So your setup code should probably include something like this:

    self.window.show()
    QTest.qWaitForWindowShown(self.window)

在装有Windows的系统上,必须进行 qWaitForWindowShown 调用异步显示(对于Qt5,请使用 qWaitForWindowExposed ).

The qWaitForWindowShown call is necessary on systems where windows are shown asynchronously (for Qt5, use qWaitForWindowExposed).

编辑:

这是一个对我有用的测试脚本:

Here's a test script that works for me:

import unittest
from PySide.QtCore import Qt
from PySide.QtGui import QApplication, QMainWindow, QLineEdit
from PySide.QtTest import QTest

class Window(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        menu = self.menuBar().addMenu('File')
        menu.addAction('Test', self.handleTest, 'Ctrl+N')
        self.edit = QLineEdit(self)
        self.setCentralWidget(self.edit)

    def handleTest(self):
        self.edit.setText('test')

class AppTestCase(unittest.TestCase):
    def setUp(self):
        qApp = QApplication.instance()
        if qApp is None:
            self.app = QApplication([])
        else:
            self.app = qApp

class WindowTestCase(AppTestCase):
    def setUp(self):
        super(WindowTestCase, self).setUp()
        self.window = Window()
        self.window.show()
        QTest.qWaitForWindowShown(self.window)

    def test_input_object_in_new_file(self):
        text = 'test'
        self.assertNotEqual(text, self.window.edit.text())
        QTest.keyClicks(self.window, 'n', Qt.ControlModifier)
        self.assertEqual(text, self.window.edit.text())

    def test_enter_text(self):
        text = 'foobar'
        self.assertNotEqual(text, self.window.edit.text())
        QTest.keyClicks(self.window.edit, text)
        self.assertEqual(text, self.window.edit.text())

if __name__ == "__main__":

    unittest.main()

更新:

以下是上述脚本的PyQt5版本:

Here's a PyQt5 version of the above script:

import unittest
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QLineEdit
from PyQt5.QtTest import QTest

class Window(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        menu = self.menuBar().addMenu('File')
        menu.addAction('Test', self.handleTest, 'Ctrl+N')
        self.edit = QLineEdit(self)
        self.setCentralWidget(self.edit)

    def handleTest(self):
        self.edit.setText('test')

class AppTestCase(unittest.TestCase):
    def setUp(self):
        qApp = QApplication.instance()
        if qApp is None:
            self.app = QApplication([''])
        else:
            self.app = qApp

class WindowTestCase(AppTestCase):
    def setUp(self):
        super(WindowTestCase, self).setUp()
        self.window = Window()
        self.window.show()
        QTest.qWaitForWindowExposed(self.window)

    def test_input_object_in_new_file(self):
        text = 'test'
        self.assertNotEqual(text, self.window.edit.text())
        QTest.keyClicks(self.window, 'n', Qt.ControlModifier)
        self.assertEqual(text, self.window.edit.text())

    def test_enter_text(self):
        text = 'foobar'
        self.assertNotEqual(text, self.window.edit.text())
        QTest.keyClicks(self.window.edit, text)
        self.assertEqual(text, self.window.edit.text())

if __name__ == "__main__":

    unittest.main()

这篇关于如何使用QTest发送菜单项的键盘快捷键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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