如何使用pytest-qt自动进行鼠标拖动?

问题描述 投票:0回答:1

我有一个pyqt窗口,该窗口跟踪按下鼠标时的鼠标移动。我正在尝试编写一个测试以使用pytest-qt自动执行此动作。

这里是一个示例类:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import QCursor
from PyQt5.QtWidgets import QApplication

class Tracker(QDialog):
    def __init__(self, parent=None):
        super(Tracker, self).__init__(parent)
        self.location = None
        self.cur = QCursor()
        layout = QVBoxLayout()
        self.label = QLabel()
        layout.addWidget(self.label)
        self.setLayout(layout)
        self.setModal(True)
        self.showFullScreen()

    def mouseReleaseEvent(self, e):
        x = self.cur.pos().x()
        y = self.cur.pos().y()
        self.location = (x, y)
        return super().mouseReleaseEvent(e)

    def mouseMoveEvent(self, e):
        x = self.cur.pos().x()
        y = self.cur.pos().y()
        self.label.setText(f'x: {x}, y: {y}')
        return super().mouseMoveEvent(e)

if __name__ == '__main__':

    import sys

    app = QApplication(sys.argv)
    window = Tracker()
    sys.exit(app.exec_())

我想编写一个测试用例,该用例将打开窗口,然后将鼠标向右拖动100像素并释放。

这是我尝试过的:

track = Tracker()
qtbot.mousePress(track, QtCore.Qt.LeftButton, pos=QPoint(300, 300))
qtbot.mouseMove(track, pos=QPoint(400, 300))
qtbot.mouseRelease(track, QtCore.Qt.LeftButton)
assert track.location == (400, 300)

我也尝试过使用pyautogui:

track = Tracker()
x, y = pyautogui.position()
pyautogui.dragTo(x + 100, y, button='left')
assert track.location == (x + 100, y)

[运行测试时,在拖动时似乎没有按下鼠标左键。标签不会更新,位置属性也不会更改。

python pyqt pyqt5 pyautogui pytest-qt
1个回答
0
投票

-使用qtbot.mouseMove()

pytest-qt构成QtTest的包装,也就是说,函数qtbot.mouseMove()与QTest::mouseMove()相同。并且此函数有一个已报告的错误QTest::mouseMove(),该错误将在Qt6 / PyQt6中修复。在报告的注释中,有几种解决方法是通过模拟QMouseEvent来替换该函数,这不会使光标移动,但是如果它调用mouseMoveEvent方法,以便正常工作,您必须修改代码。

QTBUG-5232
from PyQt5.QtWidgets import QApplication, QDialog, QLabel, QVBoxLayout


class Tracker(QDialog):
    def __init__(self, parent=None):
        super(Tracker, self).__init__(parent)
        self.location = None
        self.label = QLabel()

        layout = QVBoxLayout(self)
        layout.addWidget(self.label)
        self.setModal(True)
        self.showFullScreen()

    def mouseReleaseEvent(self, e):
        pos = self.mapToGlobal(e.pos())
        self.location = pos.x(), pos.y()
        return super().mouseReleaseEvent(e)

    def mouseMoveEvent(self, e):
        pos = self.mapToGlobal(e.pos())
        self.label.setText(f"x: {pos.x()}, y: {pos.y()}")
        return super().mouseMoveEvent(e)


if __name__ == "__main__":

    import sys

    app = QApplication(sys.argv)
    window = Tracker()
    sys.exit(app.exec_())

-使用pyautogui:

使用此方法,无需进行任何更改。

def test_emulate_QMouseEvent(qtbot):
    start_pos, end_pos = QtCore.QPoint(300, 300), QtCore.QPoint(400, 300)

    track = Tracker()

    def on_value_changed(value):
        event = QtGui.QMouseEvent(
            QtCore.QEvent.MouseMove,
            value,
            QtCore.Qt.NoButton,
            QtCore.Qt.LeftButton,
            QtCore.Qt.NoModifier,
        )
        QtCore.QCoreApplication.sendEvent(track, event)

    animation = QtCore.QVariantAnimation(
        startValue=start_pos, endValue=end_pos, duration=5000
    )
    qtbot.mousePress(track, QtCore.Qt.LeftButton, pos=QtCore.QPoint(300, 300))
    animation.valueChanged.connect(on_value_changed)
    with qtbot.waitSignal(animation.finished, timeout=10000):
        animation.start()
    qtbot.mouseRelease(track, QtCore.Qt.LeftButton)
    track.location == (end_pos.x(), end_pos.y())
© www.soinside.com 2019 - 2024. All rights reserved.