From e81db322a94e23bf2fd6d07fd307fcf787147297 Mon Sep 17 00:00:00 2001 From: Sakib75 Date: Tue, 27 Aug 2024 15:41:05 +0600 Subject: [PATCH 1/2] delay drag and drop support --- .../Web/Selenium/BuiltInFunctions.py | 55 ++++++++++++++++++- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/Framework/Built_In_Automation/Web/Selenium/BuiltInFunctions.py b/Framework/Built_In_Automation/Web/Selenium/BuiltInFunctions.py index 746ac80bc..2e75c7759 100644 --- a/Framework/Built_In_Automation/Web/Selenium/BuiltInFunctions.py +++ b/Framework/Built_In_Automation/Web/Selenium/BuiltInFunctions.py @@ -4886,11 +4886,13 @@ def get_offsets(location: str, Element: WebElement) -> tuple[int]: def drag_and_drop(dataset): sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME global selenium_driver + from time import sleep try: source = [] destination = [] destination_offset = None - param_dict = {"elementparameter": "element parameter", "parentparameter": "parent parameter", "siblingparameter": "sibling parameter", "childparameter": "child parameter"} + delay = None + param_dict = {"elementparameter": "element parameter", "parentparameter": "parent parameter", "siblingparameter": "sibling parameter", "childparameter": "child parameter","optionalparameter": "optional parameter"} for left, mid, right in dataset: if mid.startswith("src") or mid.startswith("source"): mid = mid.replace("src", "").replace(" ", "").replace("source", "") @@ -4907,6 +4909,8 @@ def drag_and_drop(dataset): destination.append((left, mid, right)) elif left.strip().lower() == "destination offset" and mid.strip().lower() == 'optional parameter': destination_offset = right + elif left.strip().lower() == "delay": + delay = int(right.strip()) if not source: CommonUtil.ExecLog(sModuleInfo, 'Please provide source element with "src element parameter", "src parent parameter" etc. Example:\n'+ @@ -4928,7 +4932,54 @@ def drag_and_drop(dataset): CommonUtil.ExecLog(sModuleInfo, "Destination Element is not found", 3) return "zeuz_failed" - if destination_offset: + if delay: + if destination_offset: + destination_x, destination_y = get_offsets(destination_offset, destination_element) + else: + destination_x = destination_element.location['x'] + destination_y = destination_element.location['y'] + distance_x = destination_x - source_element.location['x'] + distance_y = destination_y - source_element.location['y'] + total_time = delay + total_distance = (distance_x**2 + distance_y**2)**0.5 + + pixels_per_step = 5 + steps = int(total_distance / pixels_per_step) + + # Calculate the ideal time per step to fit within the total time + ideal_time_per_step = total_time / steps + + # Start the high-resolution timer + start_time = time.perf_counter() + + # Create an ActionChains object + actions = ActionChains(selenium_driver) + + # Click and hold the source element + actions.click_and_hold(source_element).perform() + + # Manually move the mouse to the target element in small increments + for i in range(steps): + # Calculate the movement for this step + move_x = distance_x / steps + move_y = distance_y / steps + + # Move the mouse by the calculated offset + actions.move_by_offset(move_x, move_y).perform() + + # Calculate elapsed time and adjust the sleep time + elapsed_time = time.perf_counter() - start_time + remaining_time = total_time - elapsed_time + time_per_step = remaining_time / (steps - i) + + if time_per_step > 0: + time.sleep(time_per_step) + + # Release the mouse button to drop the element + actions.release().perform() + sleep(2) + + elif destination_offset: x, y = get_offsets(destination_offset, destination_element) ActionChains(selenium_driver).click_and_hold(source_element).move_to_element_with_offset(destination_element, x, y).release().perform() else: From 509bd7297b6b0edf398cd7ad9b85eb20597d6885 Mon Sep 17 00:00:00 2001 From: test Date: Wed, 28 Aug 2024 14:39:16 +0600 Subject: [PATCH 2/2] delay fix --- .../Web/Selenium/BuiltInFunctions.py | 110 ++++++++++-------- 1 file changed, 59 insertions(+), 51 deletions(-) diff --git a/Framework/Built_In_Automation/Web/Selenium/BuiltInFunctions.py b/Framework/Built_In_Automation/Web/Selenium/BuiltInFunctions.py index 2e75c7759..86cda330c 100644 --- a/Framework/Built_In_Automation/Web/Selenium/BuiltInFunctions.py +++ b/Framework/Built_In_Automation/Web/Selenium/BuiltInFunctions.py @@ -4910,7 +4910,7 @@ def drag_and_drop(dataset): elif left.strip().lower() == "destination offset" and mid.strip().lower() == 'optional parameter': destination_offset = right elif left.strip().lower() == "delay": - delay = int(right.strip()) + delay = float(right.strip()) if not source: CommonUtil.ExecLog(sModuleInfo, 'Please provide source element with "src element parameter", "src parent parameter" etc. Example:\n'+ @@ -4932,59 +4932,67 @@ def drag_and_drop(dataset): CommonUtil.ExecLog(sModuleInfo, "Destination Element is not found", 3) return "zeuz_failed" - if delay: - if destination_offset: - destination_x, destination_y = get_offsets(destination_offset, destination_element) - else: - destination_x = destination_element.location['x'] - destination_y = destination_element.location['y'] - distance_x = destination_x - source_element.location['x'] - distance_y = destination_y - source_element.location['y'] - total_time = delay - total_distance = (distance_x**2 + distance_y**2)**0.5 - - pixels_per_step = 5 - steps = int(total_distance / pixels_per_step) - - # Calculate the ideal time per step to fit within the total time - ideal_time_per_step = total_time / steps - - # Start the high-resolution timer - start_time = time.perf_counter() - - # Create an ActionChains object - actions = ActionChains(selenium_driver) - - # Click and hold the source element - actions.click_and_hold(source_element).perform() - - # Manually move the mouse to the target element in small increments - for i in range(steps): - # Calculate the movement for this step - move_x = distance_x / steps - move_y = distance_y / steps - - # Move the mouse by the calculated offset - actions.move_by_offset(move_x, move_y).perform() - - # Calculate elapsed time and adjust the sleep time - elapsed_time = time.perf_counter() - start_time - remaining_time = total_time - elapsed_time - time_per_step = remaining_time / (steps - i) - - if time_per_step > 0: - time.sleep(time_per_step) - - # Release the mouse button to drop the element - actions.release().perform() - sleep(2) + """ The following code does not work with mentioned delay time. delay=2 takes 25 seconds for a dnd """ + # if delay: + # if destination_offset: + # destination_x, destination_y = get_offsets(destination_offset, destination_element) + # else: + # destination_x = destination_element.location['x'] + # destination_y = destination_element.location['y'] + # distance_x = destination_x - source_element.location['x'] + # distance_y = destination_y - source_element.location['y'] + # total_time = delay + # total_distance = (distance_x**2 + distance_y**2)**0.5 + # + # pixels_per_step = 5 + # steps = int(total_distance / pixels_per_step) + # + # # Calculate the ideal time per step to fit within the total time + # ideal_time_per_step = total_time / steps + # + # # Start the high-resolution timer + # start_time = time.perf_counter() + # + # # Create an ActionChains object + # actions = ActionChains(selenium_driver) + # + # # Click and hold the source element + # actions.click_and_hold(source_element).perform() + # + # # Manually move the mouse to the target element in small increments + # for i in range(steps): + # # Calculate the movement for this step + # move_x = distance_x / steps + # move_y = distance_y / steps + # + # # Move the mouse by the calculated offset + # actions.move_by_offset(move_x, move_y).perform() + # + # # Calculate elapsed time and adjust the sleep time + # elapsed_time = time.perf_counter() - start_time + # remaining_time = total_time - elapsed_time + # time_per_step = remaining_time / (steps - i) + # + # if time_per_step > 0: + # time.sleep(time_per_step) + # + # # Release the mouse button to drop the element + # actions.release().perform() + # sleep(2) - elif destination_offset: + if destination_offset: x, y = get_offsets(destination_offset, destination_element) - ActionChains(selenium_driver).click_and_hold(source_element).move_to_element_with_offset(destination_element, x, y).release().perform() + if delay: + """ This line of code was not tested, just keeping here""" + ActionChains(selenium_driver).click_and_hold(source_element).move_to_element_with_offset(destination_element, x, y).pause(0.5).release().perform() + else: + ActionChains(selenium_driver).click_and_hold(source_element).move_to_element_with_offset(destination_element, x, y).release().perform() + else: - ActionChains(selenium_driver).drag_and_drop(source_element, destination_element).perform() - # ActionChains(selenium_driver).click_and_hold(source_element).move_to_element(destination_element).pause(0.5).release(destination_element).perform() + if delay: + ActionChains(selenium_driver).click_and_hold(source_element).move_to_element(destination_element).pause(0.5).release(destination_element).perform() + else: + ActionChains(selenium_driver).drag_and_drop(source_element, destination_element).perform() CommonUtil.ExecLog(sModuleInfo, "Drag and drop completed from source to destination", 1) return "passed"