Skip to content

Commit b0eacf0

Browse files
Merge pull request #488 from AutomationSolutionz/delay-drag-n-drop
Delay support in Drag and Drop Action
2 parents 3718f11 + 509bd72 commit b0eacf0

File tree

1 file changed

+63
-4
lines changed

1 file changed

+63
-4
lines changed

Framework/Built_In_Automation/Web/Selenium/BuiltInFunctions.py

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4886,11 +4886,13 @@ def get_offsets(location: str, Element: WebElement) -> tuple[int]:
48864886
def drag_and_drop(dataset):
48874887
sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME
48884888
global selenium_driver
4889+
from time import sleep
48894890
try:
48904891
source = []
48914892
destination = []
48924893
destination_offset = None
4893-
param_dict = {"elementparameter": "element parameter", "parentparameter": "parent parameter", "siblingparameter": "sibling parameter", "childparameter": "child parameter"}
4894+
delay = None
4895+
param_dict = {"elementparameter": "element parameter", "parentparameter": "parent parameter", "siblingparameter": "sibling parameter", "childparameter": "child parameter","optionalparameter": "optional parameter"}
48944896
for left, mid, right in dataset:
48954897
if mid.startswith("src") or mid.startswith("source"):
48964898
mid = mid.replace("src", "").replace(" ", "").replace("source", "")
@@ -4907,6 +4909,8 @@ def drag_and_drop(dataset):
49074909
destination.append((left, mid, right))
49084910
elif left.strip().lower() == "destination offset" and mid.strip().lower() == 'optional parameter':
49094911
destination_offset = right
4912+
elif left.strip().lower() == "delay":
4913+
delay = float(right.strip())
49104914

49114915
if not source:
49124916
CommonUtil.ExecLog(sModuleInfo, 'Please provide source element with "src element parameter", "src parent parameter" etc. Example:\n'+
@@ -4928,12 +4932,67 @@ def drag_and_drop(dataset):
49284932
CommonUtil.ExecLog(sModuleInfo, "Destination Element is not found", 3)
49294933
return "zeuz_failed"
49304934

4935+
""" The following code does not work with mentioned delay time. delay=2 takes 25 seconds for a dnd """
4936+
# if delay:
4937+
# if destination_offset:
4938+
# destination_x, destination_y = get_offsets(destination_offset, destination_element)
4939+
# else:
4940+
# destination_x = destination_element.location['x']
4941+
# destination_y = destination_element.location['y']
4942+
# distance_x = destination_x - source_element.location['x']
4943+
# distance_y = destination_y - source_element.location['y']
4944+
# total_time = delay
4945+
# total_distance = (distance_x**2 + distance_y**2)**0.5
4946+
#
4947+
# pixels_per_step = 5
4948+
# steps = int(total_distance / pixels_per_step)
4949+
#
4950+
# # Calculate the ideal time per step to fit within the total time
4951+
# ideal_time_per_step = total_time / steps
4952+
#
4953+
# # Start the high-resolution timer
4954+
# start_time = time.perf_counter()
4955+
#
4956+
# # Create an ActionChains object
4957+
# actions = ActionChains(selenium_driver)
4958+
#
4959+
# # Click and hold the source element
4960+
# actions.click_and_hold(source_element).perform()
4961+
#
4962+
# # Manually move the mouse to the target element in small increments
4963+
# for i in range(steps):
4964+
# # Calculate the movement for this step
4965+
# move_x = distance_x / steps
4966+
# move_y = distance_y / steps
4967+
#
4968+
# # Move the mouse by the calculated offset
4969+
# actions.move_by_offset(move_x, move_y).perform()
4970+
#
4971+
# # Calculate elapsed time and adjust the sleep time
4972+
# elapsed_time = time.perf_counter() - start_time
4973+
# remaining_time = total_time - elapsed_time
4974+
# time_per_step = remaining_time / (steps - i)
4975+
#
4976+
# if time_per_step > 0:
4977+
# time.sleep(time_per_step)
4978+
#
4979+
# # Release the mouse button to drop the element
4980+
# actions.release().perform()
4981+
# sleep(2)
4982+
49314983
if destination_offset:
49324984
x, y = get_offsets(destination_offset, destination_element)
4933-
ActionChains(selenium_driver).click_and_hold(source_element).move_to_element_with_offset(destination_element, x, y).release().perform()
4985+
if delay:
4986+
""" This line of code was not tested, just keeping here"""
4987+
ActionChains(selenium_driver).click_and_hold(source_element).move_to_element_with_offset(destination_element, x, y).pause(0.5).release().perform()
4988+
else:
4989+
ActionChains(selenium_driver).click_and_hold(source_element).move_to_element_with_offset(destination_element, x, y).release().perform()
4990+
49344991
else:
4935-
ActionChains(selenium_driver).drag_and_drop(source_element, destination_element).perform()
4936-
# ActionChains(selenium_driver).click_and_hold(source_element).move_to_element(destination_element).pause(0.5).release(destination_element).perform()
4992+
if delay:
4993+
ActionChains(selenium_driver).click_and_hold(source_element).move_to_element(destination_element).pause(0.5).release(destination_element).perform()
4994+
else:
4995+
ActionChains(selenium_driver).drag_and_drop(source_element, destination_element).perform()
49374996

49384997
CommonUtil.ExecLog(sModuleInfo, "Drag and drop completed from source to destination", 1)
49394998
return "passed"

0 commit comments

Comments
 (0)