-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
QE-13247 Add Drag and Drop file upload step (#361)
Add a drag & drop style file upload step. Tested it in E2E tests: https://github.com/cerebrotech/domino/pull/36973 Will add functional tests later: https://dominodatalab.atlassian.net/browse/QE-13261
- Loading branch information
Showing
5 changed files
with
80 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "cucu" | ||
version = "0.159.0" | ||
version = "0.160.0" | ||
license = "MIT" | ||
description = "Easy BDD web testing" | ||
authors = ["Domino Data Lab <[email protected]>"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import os | ||
|
||
import humanize | ||
|
||
from cucu import fuzzy, logger, step | ||
|
||
|
||
def find_file_input(ctx, name, index=0): | ||
""" | ||
* <input type="file"> | ||
parameters: | ||
ctx(object): behave context object used to share data between steps | ||
name(str): name that identifies the desired button on screen | ||
index(str): the index of the button if there are duplicates | ||
returns: | ||
the WebElement that matches the provided arguments. | ||
""" | ||
ctx.check_browser_initialized() | ||
_input = fuzzy.find(ctx.browser, name, ['input[type="file"]'], index=index) | ||
|
||
prefix = "" if index == 0 else f"{humanize.ordinal(index)} " | ||
|
||
if _input is None: | ||
raise RuntimeError(f'unable to find the {prefix}file input "{name}"') | ||
|
||
return _input | ||
|
||
|
||
@step('I upload the file "{filepath}" to the file input "{name}"') | ||
def upload_file_to_input(ctx, filepath, name): | ||
_input = find_file_input(ctx, name) | ||
_input.send_keys(os.path.abspath(filepath)) | ||
|
||
|
||
JS_DROP_FILE = """ | ||
var target = arguments[0], | ||
offsetX = arguments[1], | ||
offsetY = arguments[2], | ||
document = target.ownerDocument || document, | ||
window = document.defaultView || window; | ||
var input = document.createElement('INPUT'); | ||
input.type = 'file'; | ||
input.onchange = function () { | ||
var rect = target.getBoundingClientRect(), | ||
x = rect.left + (offsetX || (rect.width >> 1)), | ||
y = rect.top + (offsetY || (rect.height >> 1)), | ||
dataTransfer = { files: this.files }; | ||
['dragenter', 'dragover', 'drop'].forEach(function (name) { | ||
var evt = document.createEvent('MouseEvent'); | ||
evt.initMouseEvent(name, !0, !0, window, 0, 0, 0, x, y, !1, !1, !1, !1, 0, null); | ||
evt.dataTransfer = dataTransfer; | ||
target.dispatchEvent(evt); | ||
}); | ||
setTimeout(function () { document.body.removeChild(input); }, 25); | ||
}; | ||
document.body.appendChild(input); | ||
return input; | ||
""" | ||
|
||
|
||
@step('I drag and drop the file "{filepath}" to "{name}"') | ||
def drag_and_drop_file(ctx, name, filepath): | ||
drop_target = fuzzy.find(ctx.browser, name, ["*"]) | ||
drop_target_html = drop_target.get_attribute("outerHTML") | ||
logger.debug( | ||
f'looked for drag & drop target "{name}" and found "{drop_target_html}"' | ||
) | ||
file_input = ctx.browser.execute(JS_DROP_FILE, drop_target, 0, 0) | ||
file_input.send_keys(filepath) |