|
29 | 29 | import re |
30 | 30 | import json |
31 | 31 | from time import time |
| 32 | +from pathlib import Path |
32 | 33 |
|
33 | 34 |
|
34 | 35 | @wrapt.decorator |
@@ -509,20 +510,39 @@ def drag_and_drop(self: "SeleniumTestability", locator: LocatorType, target: Loc |
509 | 510 | and the ``target`` is the locator of the target. See the |
510 | 511 | `Locating elements` section for details about the locator syntax. |
511 | 512 |
|
| 513 | + If you wish to drag and drop a file from a local filesystem, you can specify the locator as `file:/full/path/to/filename` |
| 514 | + and SeleniumTestability will generate a drag'n'drop events to upload a file into a given `target` element. |
| 515 | +
|
512 | 516 | ``html5`` parameter is optional and if provided, `drag_and_drop`will utilize |
513 | 517 | javascript to trigger the suitable events ensuring that html5 applications |
514 | | - receive the right events |
| 518 | + receive the right events. If `locator` starts with file: prefix, html5 defaults to True. |
515 | 519 |
|
516 | 520 | Example: |
517 | | - | `Drag And Drop` | css:div#element | css:div.target | True | |
| 521 | + | `Drag And Drop` | css:div#element | css:div.target | html5=True | |
| 522 | + | `Drag And Drop` | file:/home/rasjani/testfile.txt | id:demo-upload | |
518 | 523 | """ |
| 524 | + file_prefix = "file:" |
519 | 525 | html5 = is_truthy(html5) |
| 526 | + if file_prefix in locator: |
| 527 | + html5 = True |
| 528 | + |
520 | 529 | if not html5: |
521 | 530 | self.el.drag_and_drop(locator, target) |
522 | 531 | else: |
523 | | - from_element = self.el.find_element(locator) |
524 | 532 | to_element = self.el.find_element(target) |
525 | | - self.ctx.driver.execute_script(JS_LOOKUP["dragdrop"], from_element, to_element) |
| 533 | + filename = None |
| 534 | + if type(locator) == str and file_prefix in locator: |
| 535 | + filename = locator[locator.startswith(file_prefix) and len(file_prefix):] |
| 536 | + |
| 537 | + if filename is not None: |
| 538 | + if Path(filename).exists(): |
| 539 | + file_input = self.driver.execute_script(JS_LOOKUP["drag_and_drop_file"], to_element, 0, 0) |
| 540 | + file_input.send_keys(filename) |
| 541 | + else: |
| 542 | + raise RuntimeError(f"Unable to upload {filename} - its missing") |
| 543 | + else: |
| 544 | + from_element = self.el.find_element(locator) |
| 545 | + self.ctx.driver.execute_script(JS_LOOKUP["dragdrop"], from_element, to_element) |
526 | 546 |
|
527 | 547 | @log_wrapper |
528 | 548 | @keyword |
|
0 commit comments