How to use __shadow_click method in SeleniumBase

Best Python code snippet using SeleniumBase

base_case.py

Source:base_case.py Github

copy

Full Screen

...30 selector, by = self.__recalculate_selector(selector, by)31 if delay and (type(delay) in [int, float]) and delay > 0:32 time.sleep(delay)33 if self.__is_shadow_selector(selector):34 self.__shadow_click(selector)35 return36 element = page_actions.wait_for_element_visible(self.driver, selector, by, timeout=timeout)37 if scroll:38 self.__scroll_to_element(element, selector, by)39 element.click()40 def tap(self, selector, by=MobileBy.ACCESSIBILITY_ID, timeout=None, delay=0):41 self.__check_scope()42 if not timeout:43 timeout = settings.SMALL_TIMEOUT44 selector, by = self.__recalculate_selector(selector, by)45 if delay and (type(delay) in [int, float]) and delay > 0:46 time.sleep(delay)47 element = page_actions.wait_for_element_visible(self.driver, selector, by, timeout=timeout)48 actions = TouchAction(self.driver)49 actions.tap(element).perform()50 def back(self):51 self.driver.back()52 def close(self):53 self.driver.close_app()54 def launch(self):55 self.driver.launch_app()56 def swipe_between_element(self, start_selector, dest_selector, by=MobileBy.ACCESSIBILITY_ID, timeout=None):57 self.__check_scope()58 if not timeout:59 timeout = settings.SMALL_TIMEOUT60 start_selector, by = self.__recalculate_selector(start_selector, by)61 dest_selector, by = self.__recalculate_selector(dest_selector, by)62 start_element = page_actions.wait_for_element_visible(self.driver, start_selector, by, timeout=timeout)63 dest_element = page_actions.wait_for_element_visible(self.driver, dest_selector, by, timeout=timeout)64 self.driver.scroll(start_element,dest_element)65 def swipe_to_element(self, selector, by=MobileBy.ACCESSIBILITY_ID, start_x=100, start_y=100, end_x=0, end_y=0, duration=0, count=10, timeout=None):66 self.__check_scope()67 if not timeout:68 timeout = settings.SMALL_TIMEOUT69 selector, by = self.__recalculate_selector(selector, by)70 for i in range(count):71 try:72 self.is_element_visible(selector,by)73 break74 except Exception as e:75 self.driver.swipe(start_x, start_y, end_x, end_y, duration)76 def tap_by_coordinates(self, x, y):77 self.__check_scope()78 time.sleep(2)79 actions = TouchAction(self.driver)80 actions.tap(x=x, y=y).perform()81 def double_tap(self, selector, by=MobileBy.ACCESSIBILITY_ID, timeout=None, delay=0):82 self.__check_scope()83 if not timeout:84 timeout = settings.SMALL_TIMEOUT85 selector, by = self.__recalculate_selector(selector, by)86 if delay and (type(delay) in [int, float]) and delay > 0:87 time.sleep(delay)88 element = page_actions.wait_for_element_visible(self.driver, selector, by, timeout=timeout)89 actions = TouchAction(self.driver)90 actions.tap(element, count=2).perform()91 def scroll_to_element(self, selector, by=MobileBy.ACCESSIBILITY_ID, timeout=None):92 self.scroll_to(selector, by=by, timeout=timeout)93 def scroll_to(self, selector, by=MobileBy.ACCESSIBILITY_ID, timeout=None):94 """ Fast scroll to destination """95 self.__check_scope()96 if not timeout:97 timeout = settings.SMALL_TIMEOUT98 element = self.wait_for_element_visible(99 selector, by=by, timeout=timeout100 )101 try:102 self.__scroll_to_element(element, selector, by)103 except (StaleElementReferenceException, ENI_Exception):104 time.sleep(0.12)105 element = self.wait_for_element_visible(106 selector, by=by, timeout=timeout107 )108 self.__scroll_to_element(element, selector, by)109 def __recalculate_selector(self, selector, by, xp_ok=True):110 """Use autodetection to return the correct selector with "by" updated.111 If "xp_ok" is False, don't call convert_css_to_xpath(), which is112 used to make the ":contains()" selector valid outside JS calls."""113 _type = type(selector) # First make sure the selector is a string114 not_string = False115 if sys.version_info[0] < 3:116 if _type is not str and _type is not unicode: # noqa: F821117 not_string = True118 else:119 if _type is not str:120 not_string = True121 if not_string:122 msg = "Expecting a selector of type: \"<class 'str'>\" (string)!"123 raise Exception('Invalid selector type: "%s"\n%s' % (_type, msg))124 if page_utils.is_xpath_selector(selector):125 by = MobileBy.XPATH126 if page_utils.is_link_text_selector(selector):127 selector = page_utils.get_link_text_from_selector(selector)128 by = MobileBy.LINK_TEXT129 if page_utils.is_partial_link_text_selector(selector):130 selector = page_utils.get_partial_link_text_from_selector(selector)131 by = MobileBy.PARTIAL_LINK_TEXT132 if page_utils.is_name_selector(selector):133 name = page_utils.get_name_from_selector(selector)134 selector = '[name="%s"]' % name135 by = MobileBy.CSS_SELECTOR136 if page_utils.is_id_selector(selector):137 by = MobileBy.ID138 return (selector, by)139 def __is_shadow_selector(self, selector):140 self.__fail_if_invalid_shadow_selector_usage(selector)141 if "::shadow " in selector:142 return True143 return False144 def __fail_if_invalid_shadow_selector_usage(self, selector):145 if selector.strip().endswith("::shadow"):146 msg = (147 "A Shadow DOM selector cannot end on a shadow root element!"148 " End the selector with an element inside the shadow root!"149 )150 raise Exception(msg)151 def double_click(self, selector, by=MobileBy.ACCESSIBILITY_ID, timeout=None):152 from selenium.webdriver.common.action_chains import ActionChains153 self.__check_scope()154 if not timeout:155 timeout = settings.SMALL_TIMEOUT156 original_selector = selector157 original_by = by158 selector, by = self.__recalculate_selector(selector, by)159 element = page_actions.wait_for_element_visible(160 self.driver, selector, by, timeout=timeout161 )162 # Find the element one more time in case scrolling hid it163 element = page_actions.wait_for_element_visible(164 self.driver, selector, by, timeout=timeout165 )166 actions = ActionChains(self.driver)167 actions.double_click(element).perform()168 def go_back(self):169 self.__check_scope()170 self.__last_page_load_url = None171 self.driver.back()172 def scroll_screen(self, start_x=100, start_y=100, end_x=0, end_y=0, duration=0):173 """Swipe from one point to another point, for an optional duration.174 Args:175 start_x: x-coordinate at which to start176 start_y: y-coordinate at which to start177 end_x: x-coordinate at which to stop178 end_y: y-coordinate at which to stop179 duration: time to take the swipe, in ms.180 """181 self.driver.swipe(start_x, start_y, end_x, end_y, duration)182 def is_checked(self, selector, by=MobileBy.ACCESSIBILITY_ID, timeout=None):183 """Determines if a checkbox or a radio button element is checked.184 Returns True if the element is checked.185 Returns False if the element is not checked.186 If the element is not present on the page, raises an exception.187 If the element is not a checkbox or radio, raises an exception."""188 self.__check_scope()189 if not timeout:190 timeout = settings.SMALL_TIMEOUT191 selector, by = self.__recalculate_selector(selector, by)192 kind = self.get_attribute(selector, "type", by=by, timeout=timeout)193 if kind != "checkbox" and kind != "radio":194 raise Exception("Expecting a checkbox or a radio button element!")195 is_checked = self.get_attribute(196 selector, "checked", by=by, timeout=timeout, hard_fail=False197 )198 if is_checked:199 return True200 else: # (NoneType)201 return False202 def __select_option(203 self,204 dropdown_selector,205 option,206 dropdown_by=MobileBy.ACCESSIBILITY_ID,207 option_by="text",208 timeout=None,209 ):210 """Selects an HTML <select> option by specification.211 Option specifications are by "text", "index", or "value".212 Defaults to "text" if option_by is unspecified or unknown."""213 from selenium.webdriver.support.ui import Select214 self.__check_scope()215 if not timeout:216 timeout = settings.SMALL_TIMEOUT217 dropdown_selector, dropdown_by = self.__recalculate_selector(218 dropdown_selector, dropdown_by219 )220 element = self.wait_for_element_present(221 dropdown_selector, by=dropdown_by, timeout=timeout222 )223 try:224 if option_by == "index":225 Select(element).select_by_index(option)226 elif option_by == "value":227 Select(element).select_by_value(option)228 else:229 Select(element).select_by_visible_text(option)230 except (StaleElementReferenceException, ENI_Exception):231 time.sleep(0.14)232 element = self.wait_for_element_present(233 dropdown_selector, by=dropdown_by, timeout=timeout234 )235 if option_by == "index":236 Select(element).select_by_index(option)237 elif option_by == "value":238 Select(element).select_by_value(option)239 else:240 Select(element).select_by_visible_text(option)241 def select_option_by_text(242 self,243 dropdown_selector,244 option,245 dropdown_by=MobileBy.ACCESSIBILITY_ID,246 timeout=None,247 ):248 """Selects an HTML <select> option by option text.249 @Params250 dropdown_selector - the <select> selector.251 option - the text of the option.252 """253 self.__check_scope()254 if not timeout:255 timeout = settings.SMALL_TIMEOUT256 self.__select_option(257 dropdown_selector,258 option,259 dropdown_by=dropdown_by,260 option_by="text",261 timeout=timeout,262 )263 def select_option_by_index(264 self,265 dropdown_selector,266 option,267 dropdown_by=MobileBy.ACCESSIBILITY_ID,268 timeout=None,269 ):270 """Selects an HTML <select> option by option index.271 @Params272 dropdown_selector - the <select> selector.273 option - the index number of the option.274 """275 self.__check_scope()276 if not timeout:277 timeout = settings.SMALL_TIMEOUT278 self.__select_option(279 dropdown_selector,280 option,281 dropdown_by=dropdown_by,282 option_by="index",283 timeout=timeout,284 )285 def select_option_by_value(286 self,287 dropdown_selector,288 option,289 dropdown_by=MobileBy.ACCESSIBILITY_ID,290 timeout=None,291 ):292 """Selects an HTML <select> option by option value.293 @Params294 dropdown_selector - the <select> selector.295 option - the value property of the option.296 """297 self.__check_scope()298 if not timeout:299 timeout = settings.SMALL_TIMEOUT300 self.__select_option(301 dropdown_selector,302 option,303 dropdown_by=dropdown_by,304 option_by="value",305 timeout=timeout,306 )307 def save_screenshot(self, name, folder=None):308 """Saves a screenshot of the current page.309 If no folder is specified, uses the folder where pytest was called.310 The screenshot will be in PNG format."""311 return page_actions.save_screenshot(self.driver, name, folder)312 def sleep(self, seconds):313 self.__check_scope()314 time.sleep(seconds)315 if seconds <= 0.3:316 time.sleep(seconds)317 else:318 start_ms = time.time() * 1000.0319 stop_ms = start_ms + (seconds * 1000.0)320 for x in range(int(seconds * 5)):321 now_ms = time.time() * 1000.0322 if now_ms >= stop_ms:323 break324 time.sleep(0.2)325 def is_selected(self, selector, by=MobileBy.ACCESSIBILITY_ID, timeout=None):326 """ Same as is_checked() """327 return self.is_checked(selector, by=by, timeout=timeout)328 def check_if_unchecked(self, selector, by=MobileBy.ACCESSIBILITY_ID):329 """ If a checkbox or radio button is not checked, will check it. """330 self.__check_scope()331 selector, by = self.__recalculate_selector(selector, by)332 if not self.is_checked(selector, by=by):333 if self.is_element_visible(selector, by=by):334 self.click(selector, by=by)335 def select_if_unselected(self, selector, by=MobileBy.ACCESSIBILITY_ID):336 """ Same as check_if_unchecked() """337 self.check_if_unchecked(selector, by=by)338 def uncheck_if_checked(self, selector, by=MobileBy.ACCESSIBILITY_ID):339 """ If a checkbox is checked, will uncheck it. """340 self.__check_scope()341 selector, by = self.__recalculate_selector(selector, by)342 if self.is_checked(selector, by=by):343 if self.is_element_visible(selector, by=by):344 self.click(selector, by=by)345 def unselect_if_selected(self, selector, by=MobileBy.ACCESSIBILITY_ID):346 """ Same as uncheck_if_checked() """347 self.uncheck_if_checked(selector, by=by)348 def is_element_visible(self, selector, by=MobileBy.ACCESSIBILITY_ID):349 selector, by = self.__recalculate_selector(selector, by)350 return page_actions.is_element_visible(self.driver, selector, by)351 def get_attribute(352 self,353 selector,354 attribute,355 by=MobileBy.ACCESSIBILITY_ID,356 timeout=None,357 hard_fail=True,358 ):359 """ This method uses JavaScript to get the value of an attribute. """360 self.__check_scope()361 if not timeout:362 timeout = settings.SMALL_TIMEOUT363 selector, by = self.__recalculate_selector(selector, by)364 time.sleep(0.01)365 element = page_actions.wait_for_element_present(366 self.driver, selector, by, timeout367 )368 try:369 attribute_value = element.get_attribute(attribute)370 except (StaleElementReferenceException, ENI_Exception):371 time.sleep(0.14)372 element = page_actions.wait_for_element_present(373 self.driver, selector, by, timeout374 )375 attribute_value = element.get_attribute(attribute)376 if attribute_value is not None:377 return attribute_value378 else:379 if hard_fail:380 raise Exception(381 "Element {%s} has no attribute {%s}!"382 % (selector, attribute)383 )384 else:385 return None386 def __shadow_click(self, selector):387 element = self.__get_shadow_element(selector)388 element.click()389 def __get_shadow_element(self, selector, timeout=None):390 if timeout is None:391 timeout = settings.SMALL_TIMEOUT392 elif timeout == 0:393 timeout = 0.1 # Use for: is_shadow_element_* (* = present/visible)394 self.__fail_if_invalid_shadow_selector_usage(selector)395 if "::shadow " not in selector:396 raise Exception(397 'A Shadow DOM selector must contain at least one "::shadow "!'398 )399 selectors = selector.split("::shadow ")400 element = self.get_element(selectors[0])...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run SeleniumBase automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful