Skip to content Skip to sidebar Skip to footer

How To Automate Mouse Drag Using Pytest-qt?

I have a pyqt window which tracks mouse movement while the mouse is pressed. I'm trying to write a test to automate this movement using pytest-qt. Here is an example class: from Py

Solution 1:

- Using qtbot.mouseMove():

pytest-qt makes a wrapper of QtTest, that is, the function qtbot.mouseMove() is the same as QTest::mouseMove(). And this function has a bug that is reported QTBUG-5232 that will be fixed in Qt6/PyQt6, In the comments of the report there are several a workaround that is to replace that function by emulating QMouseEvent that will not make the cursor move but if it calls the mouseMoveEvent method so that in order to work correctly you will have to modify your code.

from PyQt5.QtWidgets import QApplication, QDialog, QLabel, QVBoxLayout


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

    defmouseReleaseEvent(self, e):
        pos = self.mapToGlobal(e.pos())
        self.location = pos.x(), pos.y()
        returnsuper().mouseReleaseEvent(e)

    defmouseMoveEvent(self, e):
        pos = self.mapToGlobal(e.pos())
        self.label.setText(f"x: {pos.x()}, y: {pos.y()}")
        returnsuper().mouseMoveEvent(e)


if __name__ == "__main__":

    import sys

    app = QApplication(sys.argv)
    window = Tracker()
    sys.exit(app.exec_())
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())

- Using pyautogui:

With this method it is not necessary to make any changes.

def test_pyautogui(qtbot):
    start_pos, end_pos = QtCore.QPoint(300, 300), QtCore.QPoint(400, 300)
    track = Tracker()
    qtbot.waitUntil(track.isVisible)

    def on_value_changed(value):
        pyautogui.dragTo(value.x(), value.y(), button="left")

    animation = QtCore.QVariantAnimation(
        startValue=start_pos, endValue=end_pos, duration=5000
    )
    pyautogui.moveTo(start_pos.x(), start_pos.y())
    pyautogui.mouseDown(button="left")
    animation.valueChanged.connect(on_value_changed)
    with qtbot.waitSignal(animation.finished, timeout=10000):
        animation.start()

    track.location == (end_pos.x(), end_pos.y())

Post a Comment for "How To Automate Mouse Drag Using Pytest-qt?"