Skip to content

Commit

Permalink
[#511] NEW: add pause to drag_and_drop (IN PROGRESS)
Browse files Browse the repository at this point in the history
  • Loading branch information
yashaka committed Jan 25, 2024
1 parent 91ff35a commit 0082cb2
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions selene/core/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,33 +123,58 @@ def func(element: Element):


def drag_and_drop_to(
target: Element, _assert_location_changed: bool = False
target: Element,
*,
_pause_in_between: float = 0.0,
_assert_location_changed: bool = False,
) -> Command[Element]:
"""
Args:
target: a destination element to drag and drop to
_pause_in_between: pause in seconds between all intermediate actions
during drag and drop.
_assert_location_changed: False by default, but if True,
then will assert that element was dragged to the new location,
hence forcing a command retry if command was under waiting.
This option is marked as experimental (by leading underscore),
it may be renamed or removed completely.
The options marked by leading underscore are experimental
and may be renamed or removed completely in the future.
"""

def func(source: Element):
source_webelement = source.locate()
source_location = (
source_webelement.location if _assert_location_changed else None
)
actions = ActionChains(source.config.driver)

ActionChains(source.config.driver).drag_and_drop(
source_webelement,
target.locate(),
).perform()
actions.move_to_element(source_webelement)
actions.w3c_actions.pointer_action.click_and_hold()

# key_action.pause accepts int, how does it even work?
# how to make it usable in our case?
actions.w3c_actions.key_action.pause(round(_pause_in_between))

actions.move_to_element(target.locate())

# this is the only pause that was not present in original selenium's code
# of actions.drag_and_drop:
actions.pause(_pause_in_between)

actions.w3c_actions.pointer_action.release()

actions.w3c_actions.key_action.pause(round(_pause_in_between))

actions.perform()

if _assert_location_changed and source_location == source.locate().location:
raise _SeleneError('Element was not dragged to the new place')

return Command(f'drag and drop to: {target}', func)
return Command(
f'drag and drop to: {target} '
f'with pause in between: {_pause_in_between} seconds',
func,
)


# TODO: consider adding 0 as default for x and y
Expand Down

0 comments on commit 0082cb2

Please sign in to comment.