def drag_to_element(self, to_element: 'Element', jquery_version='3.5.1') -> 'Element': """ Drag the current element to the given element. Args: to_element: The Element to drag to. jquery_version: The version of jQuery to use for this action. Returns: The current element. Examples: column = py.get('#done-column') py.get('#draggable-card').drag_to_element(column) """ self.py.log.info( ' [STEP] .drag_to_element() - Drag this element to another element' ) self.py.load_jquery(jquery_version) dnd_javascript = utils.read_script_from_file('drag_and_drop.js') self.py.execute_script( dnd_javascript + "$(arguments[0]).simulateDragDrop({ dropTarget: arguments[1] });", self.webelement, to_element.webelement) return self
def drag_to_element(self, to_element: 'Element') -> 'Element': """ Drag the current element to the given element. Args: to_element: The Element to drag to. Returns: The current element. """ dnd_javascript = utils.read_script_from_file('drag_and_drop.js') self.py.execute_script( dnd_javascript + "$(arguments[0]).simulateDragDrop({ dropTarget: arguments[1]});", self.webelement, to_element.webelement) return self
def drag_to(self, css: str) -> 'Element': """ Drag the current element to another element given its CSS selector. Args: css: The CSS selector of the element to drag to. Returns: The current element. """ to_element = self.py.get(css).webelement dnd_javascript = utils.read_script_from_file('drag_and_drop.js') self.py.execute_script( dnd_javascript + "$(arguments[0]).simulateDragDrop({ dropTarget: arguments[1]});", self.webelement, to_element) return self
def load_jquery(self, version: str = '3.5.1', timeout=None) -> 'Pylenium': """ Load jQuery onto the current page if it doesn't already exist. Args: version: The version of jQuery to load. Default version = 3.5.1 timeout: The number of seconds to give this command to complete. Examples: py.load_jquery('3.5.1') """ self.log.info( '[STEP] .load_jquery() - Load jQuery onto the current page') jquery_url = f'https://code.jquery.com/jquery-{version}.min.js' load_jquery = utils.read_script_from_file('load_jquery.js') self.webdriver.execute_async_script(load_jquery, jquery_url) self.wait(timeout=timeout) \ .until(lambda _: self.execute_script('return typeof(jQuery) !== "undefined";'), message='jQuery was "undefined" which means it did not load within timeout.') return self
def inject(driver: WebDriver, version='3.5.1', timeout=10): """ Inject the given jQuery version to the current context and any iframes within it. Args: driver: The instance of WebDriver to attach to. version: The jQuery version. (Default is 3.5.1) timeout: The max number of seconds to wait for jQuery to be loaded. (Default is 10) """ jquery_url = f'https://code.jquery.com/jquery-{version}.min.js' load_jquery = utils.read_script_from_file('load_jquery.js') driver.execute_async_script(load_jquery, jquery_url, None) WebDriverWait(driver, timeout).until( lambda drvr: drvr.execute_script( 'return typeof(jQuery) !== "undefined";'), message= 'jQuery was "undefined" which means it did not load within timeout.') iframes = driver.find_elements_by_tag_name('iframe') for iframe in iframes: driver.execute_async_script(load_jquery, jquery_url, iframe)
def drag_and_drop(driver: WebDriver, drag_element: WebElement, drop_element: WebElement, version='3.5.1', timeout=10): """ Simulate Drag and Drop using jQuery. Args: driver: The driver that will simulate the drag and drop. drag_element: The element to be dragged. drop_element: The element to drop to. version: The jQuery version to use. timeout: The max of number of seconds to wait for jQuery to be loaded. """ inject(driver, version) dnd_js = utils.read_script_from_file('drag_and_drop.js') driver.execute_script( dnd_js + "jQuery(arguments[0]).simulateDragDrop({ dropTarget: arguments[1] });", drag_element, drop_element)