-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lib: add drag events helper function #21296
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -194,6 +194,33 @@ window.ph_mouse = function(sel, type, x, y, btn, ctrlKey, shiftKey, altKey, meta | |
throw new Error(sel + " is disabled or somehow doesn't process events"); | ||
}; | ||
|
||
window.ph_drag_event = function(sel, eventType, filename) { | ||
const el = window.ph_find(sel); | ||
|
||
/* The element has to be visible, and not collapsed */ | ||
if (el.offsetWidth <= 0 && el.offsetHeight <= 0 && el.tagName != 'svg') | ||
throw new Error(sel + " is not visible"); | ||
Comment on lines
+201
to
+202
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please call |
||
|
||
let dataTransfer = null; | ||
if (filename) { | ||
const content = "Random file content: 4"; | ||
const file = new File([content], filename, { type: "text/plain" }); | ||
dataTransfer = new DataTransfer(); | ||
dataTransfer.dropEffect = "move"; | ||
|
||
// The DataTransfer.files property is read-only as this object has a processing | ||
// and security check that is handled by the browser during drag-and-drop events. | ||
// Work around it and create a DataTransfer object with a single small textfile. | ||
Object.defineProperty(dataTransfer, 'files', { | ||
value: [file], | ||
writable: false, | ||
}); | ||
} | ||
|
||
const ev = new DragEvent(eventType, { bubbles: true, dataTransfer }); | ||
el.dispatchEvent(ev); | ||
}; | ||
|
||
window.ph_get_checked = function(sel) { | ||
const el = window.ph_find(sel); | ||
if (el.checked === undefined) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -575,6 +575,19 @@ def click(self, selector: str) -> None: | |
""" | ||
self.mouse(selector + ":not([disabled]):not([aria-disabled=true])", "click") | ||
|
||
def drag(self, selector: str, dragType: str, filename: str | None = None) -> None: | ||
"""Simulate drag events | ||
|
||
When filename is specified a file is created to simulate an upload for drop event | ||
|
||
:param selector: target element | ||
:param dragType: the drag event to simulate (dragenter, dragleave, dragover, drop, ...) | ||
:param filename: name of file to be uploaded | ||
""" | ||
|
||
self.wait_visible(selector) | ||
return self.call_js_func('ph_drag_event', selector, dragType, filename) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We really shouldn't use this "create direct Event objects" approach for generic DnD events. We found a lot of bugs when we moved Firefox tests to actual mouse clicks/moves/etc. I understand why we need it for that specific "file DnD" use case, though. |
||
|
||
def val(self, selector: str) -> Any: | ||
"""Get the value attribute of a selector. | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rename that to drag_file_event() -- it is (or rather, should be) specific to dnd'ing files, not e.g. an object from/to within the page.