其他分享
首页 > 其他分享> > 如何使用QTest发送菜单项的键盘快捷键?

如何使用QTest发送菜单项的键盘快捷键?

作者:互联网

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

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

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")

一些问题:

>我应该将此发送到窗口本身吗?还是到菜单栏?似乎都不起作用…
>这是发送键盘快捷键的正确方法吗?

解决方法:

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

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

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

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

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

编辑:

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

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版本:

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()

标签:python,testing,keyboard-shortcuts,pyside,qttest
来源: https://codeday.me/bug/20191014/1912181.html