Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions test/common/test-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

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.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please call ph_is_visible(). But moreover, don't call this at all here. testlib.py already calls wait_visible() on the selector, so this is redundant.


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)
Expand Down
13 changes: 13 additions & 0 deletions test/common/testlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The 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.

Expand Down
Loading