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 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_())
I'd like to write a test case that opens the window then drags the mouse 100 pixels to the right and releases.
Here's what I've tried:
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)
I've also tried using pyautogui:
track = Tracker()
x, y = pyautogui.position()
pyautogui.dragTo(x + 100, y, button='left')
assert track.location == (x + 100, y)
When running the test it appears the left button of the mouse is not held down while dragging. The label will not update and location attribute doesn't change.