How to use __get_shadow_element method in SeleniumBase

Best Python code snippet using SeleniumBase

base_case.py

Source:base_case.py Github

copy

Full Screen

...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])401 selector_chain = selectors[0]402 for selector_part in selectors[1:]:403 shadow_root = self.execute_script(404 "return arguments[0].shadowRoot", element405 )406 if timeout == 0.1 and not shadow_root:407 raise Exception(408 "Element {%s} has no shadow root!" % selector_chain409 )410 elif not shadow_root:411 time.sleep(2) # Wait two seconds for the shadow root to appear412 shadow_root = self.execute_script(413 "return arguments[0].shadowRoot", element414 )415 if not shadow_root:416 raise Exception(417 "Element {%s} has no shadow root!" % selector_chain418 )419 selector_chain += "::shadow "420 selector_chain += selector_part421 try:422 element = page_actions.wait_for_element_present(423 shadow_root,424 selector_part,425 by=MobileBy.CSS_SELECTOR,426 timeout=timeout,427 )428 except Exception:429 msg = (430 "Shadow DOM Element {%s} was not present after %s seconds!"431 % (selector_chain, timeout)432 )433 page_actions.timeout_exception("NoSuchElementException", msg)434 return element435 def execute_script(self, script, *args, **kwargs):436 self.__check_scope()437 return self.driver.execute_script(script, *args, **kwargs)438 def get_element(self, selector, by=MobileBy.CSS_SELECTOR, timeout=None):439 """Same as wait_for_element_present() - returns the element.440 The element does not need be visible (it may be hidden)."""441 self.__check_scope()442 if not timeout:443 timeout = settings.SMALL_TIMEOUT444 selector, by = self.__recalculate_selector(selector, by)445 return self.wait_for_element_present(selector, by=by, timeout=timeout)446 def wait_for_element_visible(447 self, selector, by=MobileBy.ACCESSIBILITY_ID, timeout=None448 ):449 """ Same as self.wait_for_element() """450 self.__check_scope()451 if not timeout:452 timeout = settings.LARGE_TIMEOUT453 selector, by = self.__recalculate_selector(selector, by)454 if self.__is_shadow_selector(selector):455 return self.__wait_for_shadow_element_visible(selector)456 return page_actions.wait_for_element_visible(457 self.driver, selector, by, timeout458 )459 def wait_for_element_present(460 self, selector, by=MobileBy.ACCESSIBILITY_ID, timeout=None461 ):462 """Waits for an element to appear in the HTML of a page.463 The element does not need be visible (it may be hidden)."""464 self.__check_scope()465 if not timeout:466 timeout = settings.LARGE_TIMEOUT467 selector, by = self.__recalculate_selector(selector, by)468 if self.__is_shadow_selector(selector):469 return self.__wait_for_shadow_element_present(selector)470 return page_actions.wait_for_element_present(471 self.driver, selector, by, timeout472 )473 def wait_for_element(self, selector, by=MobileBy.ACCESSIBILITY_ID, timeout=None):474 """Waits for an element to appear in the HTML of a page.475 The element must be visible (it cannot be hidden)."""476 self.__check_scope()477 if not timeout:478 timeout = settings.LARGE_TIMEOUT479 selector, by = self.__recalculate_selector(selector, by)480 if self.__is_shadow_selector(selector):481 return self.__wait_for_shadow_element_visible(selector)482 return page_actions.wait_for_element_visible(483 self.driver, selector, by, timeout484 )485 def __wait_for_shadow_element_present(self, selector):486 element = self.__get_shadow_element(selector)487 return element488 def __wait_for_shadow_element_visible(self, selector):489 element = self.__get_shadow_element(selector)490 if not element.is_displayed():491 msg = "Shadow DOM Element {%s} was not visible!" % selector492 page_actions.timeout_exception("NoSuchElementException", msg)493 return element494 def __get_shadow_text(self, selector):495 element = self.__get_shadow_element(selector)496 return element.text497 def __wait_for_shadow_text_visible(self, text, selector):498 start_ms = time.time() * 1000.0499 stop_ms = start_ms + (settings.SMALL_TIMEOUT * 1000.0)500 for x in range(int(settings.SMALL_TIMEOUT * 10)):501 try:502 actual_text = self.__get_shadow_text(selector).strip()503 text = text.strip()504 if text not in actual_text:505 msg = (506 "Expected text {%s} in element {%s} was not visible!"507 % (text, selector)508 )509 page_actions.timeout_exception(...

Full Screen

Full Screen

asserts.py

Source:asserts.py Github

copy

Full Screen

...148 )149 self.__highlight_with_assert_success(messenger_post, selector, by)150 return True151 def __assert_shadow_element_present(self, selector):152 self.__get_shadow_element(selector)153 if self.demo_mode:154 a_t = "ASSERT"155 by = By.CSS_SELECTOR156 if self._language != "English":157 from seleniumbase.fixtures.words import SD158 a_t = SD.translate_assert(self._language)159 messenger_post = "%s %s: %s" % (a_t, by.upper(), selector)160 try:161 js_utils.activate_jquery(self.driver)162 js_utils.post_messenger_success_message(163 self.driver, messenger_post, self.message_duration164 )165 except Exception:166 pass167 def __assert_shadow_element_visible(self, selector):168 element = self.__get_shadow_element(selector)169 if not element.is_displayed():170 msg = "Shadow DOM Element {%s} was not visible!" % selector171 page_actions.timeout_exception("NoSuchElementException", msg)172 if self.demo_mode:173 a_t = "ASSERT"174 by = By.CSS_SELECTOR175 if self._language != "English":176 from seleniumbase.fixtures.words import SD177 a_t = SD.translate_assert(self._language)178 messenger_post = "%s %s: %s" % (a_t, by.upper(), selector)179 try:180 js_utils.activate_jquery(self.driver)181 js_utils.post_messenger_success_message(182 self.driver, messenger_post, self.message_duration...

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