How to use __assert_shadow_element_visible method in SeleniumBase

Best Python code snippet using SeleniumBase

asserts.py

Source:asserts.py Github

copy

Full Screen

...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_duration183 )184 except Exception:185 pass186 def __assert_eq(self, *args, **kwargs):187 """ Minified assert_equal() using only the list diff. """188 minified_exception = None189 try:190 self.assertEqual(*args, **kwargs)191 except Exception as e:192 str_e = str(e)193 minified_exception = "\nAssertionError:\n"194 lines = str_e.split("\n")195 countdown = 3196 countdown_on = False197 first_differing = False198 skip_lines = False199 for line in lines:200 if countdown_on:201 if not skip_lines:202 minified_exception += line + "\n"203 countdown = countdown - 1204 if countdown == 0:205 countdown_on = False206 skip_lines = False207 elif line.startswith("First differing"):208 first_differing = True209 countdown_on = True210 countdown = 3211 minified_exception += line + "\n"212 elif line.startswith("First list"):213 countdown_on = True214 countdown = 3215 if not first_differing:216 minified_exception += line + "\n"217 else:218 skip_lines = True219 elif line.startswith("F"):220 countdown_on = True221 countdown = 3222 minified_exception += line + "\n"223 elif line.startswith("+") or line.startswith("-"):224 minified_exception += line + "\n"225 elif line.startswith("?"):226 minified_exception += line + "\n"227 elif line.strip().startswith("*"):228 minified_exception += line + "\n"229 if minified_exception:230 raise Exception(minified_exception)231 def __assert_exact_shadow_text_visible(self, text, selector, timeout):232 self.__wait_for_exact_shadow_text_visible(text, selector, timeout)233 if self.demo_mode:234 a_t = "ASSERT EXACT TEXT"235 i_n = "in"236 by = By.CSS_SELECTOR237 if self._language != "English":238 from seleniumbase.fixtures.words import SD239 a_t = SD.translate_assert_exact_text(self._language)240 i_n = SD.translate_in(self._language)241 messenger_post = "%s: {%s} %s %s: %s" % (242 a_t,243 text,244 i_n,245 by.upper(),246 selector,247 )248 try:249 js_utils.activate_jquery(self.driver)250 js_utils.post_messenger_success_message(251 self.driver, messenger_post, self.message_duration252 )253 except Exception:254 pass255 def __assert_shadow_text_visible(self, text, selector, timeout):256 self.__wait_for_shadow_text_visible(text, selector, timeout)257 if self.demo_mode:258 a_t = "ASSERT TEXT"259 i_n = "in"260 by = By.CSS_SELECTOR261 if self._language != "English":262 from seleniumbase.fixtures.words import SD263 a_t = SD.translate_assert_text(self._language)264 i_n = SD.translate_in(self._language)265 messenger_post = "%s: {%s} %s %s: %s" % (266 a_t,267 text,268 i_n,269 by.upper(),270 selector,271 )272 try:273 js_utils.activate_jquery(self.driver)274 js_utils.post_messenger_success_message(275 self.driver, messenger_post, self.message_duration276 )277 except Exception:278 pass279 def assert_attribute_not_present(280 self, selector, attribute, value=None, by=By.CSS_SELECTOR, timeout=None281 ):282 """Similar to wait_for_attribute_not_present()283 Raises an exception if the attribute is still present after timeout.284 Returns True if successful. Default timeout = SMALL_TIMEOUT."""285 self.__check_scope__()286 timeout = self.get_timeout(timeout, constants.SMALL_TIMEOUT)287 return self.wait_for_attribute_not_present(288 selector, attribute, value=value, by=by, timeout=timeout289 )290 def assert_element(self, selector, by=By.CSS_SELECTOR, timeout=None):291 """Similar to wait_for_element_visible(), but returns nothing.292 As above, will raise an exception if nothing can be found.293 Returns True if successful. Default timeout = SMALL_TIMEOUT."""294 self.__check_scope()295 timeout = self.get_timeout(timeout, constants.SMALL_TIMEOUT)296 if type(selector) is list:297 self.assert_elements(selector, by=by, timeout=timeout)298 return True299 if self.__is_shadow_selector(selector):300 self.__assert_shadow_element_visible(selector)301 return True302 self.wait_for_element_visible(selector, by=by, timeout=timeout)303 if self.demo_mode:304 selector, by = self.__recalculate_selector(305 selector, by, xp_ok=False306 )307 a_t = "ASSERT"308 if self._language != "English":309 from seleniumbase.fixtures.words import SD310 a_t = SD.translate_assert(self._language)311 messenger_post = "%s %s: %s" % (a_t, by.upper(), selector)312 self.__highlight_with_assert_success(messenger_post, selector, by)313 return True314 def assert_element_visible(315 self, selector, by=By.CSS_SELECTOR, timeout=None316 ):317 """Same as self.assert_element()318 As above, will raise an exception if nothing can be found."""319 self.__check_scope()320 timeout = self.get_timeout(timeout, constants.SMALL_TIMEOUT)321 self.assert_element(selector, by=by, timeout=timeout)322 return True323 def assert_elements(self, *args, **kwargs):324 """Similar to self.assert_element(), but can assert multiple elements.325 The input is a list of elements.326 Optional kwargs include "by" and "timeout" (used by all selectors).327 Raises an exception if any of the elements are not visible.328 Examples:329 self.assert_elements("h1", "h2", "h3")330 OR331 self.assert_elements(["h1", "h2", "h3"])"""332 self.__check_scope()333 selectors = []334 timeout = None335 by = By.CSS_SELECTOR336 for kwarg in kwargs:337 if kwarg == "timeout":338 timeout = kwargs["timeout"]339 elif kwarg == "by":340 by = kwargs["by"]341 elif kwarg == "selector":342 selector = kwargs["selector"]343 if type(selector) is str:344 selectors.append(selector)345 elif type(selector) is list:346 selectors_list = selector347 for selector in selectors_list:348 if type(selector) is str:349 selectors.append(selector)350 else:351 raise Exception('Unknown kwarg: "%s"!' % kwarg)352 timeout = self.get_timeout(timeout, constants.SMALL_TIMEOUT)353 for arg in args:354 if type(arg) is list:355 for selector in arg:356 if type(selector) is str:357 selectors.append(selector)358 elif type(arg) is str:359 selectors.append(arg)360 for selector in selectors:361 if self.__is_shadow_selector(selector):362 self.__assert_shadow_element_visible(selector)363 continue364 self.wait_for_element_visible(selector, by=by, timeout=timeout)365 if self.demo_mode:366 selector, by = self.__recalculate_selector(selector, by)367 a_t = "ASSERT"368 if self._language != "English":369 from seleniumbase.fixtures.words import SD370 a_t = SD.translate_assert(self._language)371 messenger_post = "%s %s: %s" % (a_t, by.upper(), selector)372 self.__highlight_with_assert_success(373 messenger_post, selector, by374 )375 continue376 return True377 def assert_elements_visible(self, *args, **kwargs):378 """Same as self.assert_elements()379 Raises an exception if any element cannot be found."""380 return self.assert_elements(*args, **kwargs)381 def assert_element_absent(382 self, selector, by=By.CSS_SELECTOR, timeout=None383 ):384 """Similar to wait_for_element_absent()385 As above, will raise an exception if the element stays present.386 A hidden element counts as a present element, which fails this assert.387 If you want to assert that elements are hidden instead of nonexistent,388 use assert_element_not_visible() instead.389 (Note that hidden elements are still present in the HTML of the page.)390 Returns True if successful. Default timeout = SMALL_TIMEOUT."""391 self.__check_scope()392 timeout = self.get_timeout(timeout, constants.SMALL_TIMEOUT)393 self.wait_for_element_absent(selector, by=by, timeout=timeout)394 return True395 def assert_element_not_present(396 self, selector, by=By.CSS_SELECTOR, timeout=None397 ):398 """Same as self.assert_element_absent()399 Will raise an exception if the element stays present.400 A hidden element counts as a present element, which fails this assert.401 If you want to assert that elements are hidden instead of nonexistent,402 use assert_element_not_visible() instead.403 (Note that hidden elements are still present in the HTML of the page.)404 Returns True if successful. Default timeout = SMALL_TIMEOUT."""405 self.__check_scope()406 timeout = self.get_timeout(timeout, constants.SMALL_TIMEOUT)407 self.wait_for_element_absent(selector, by=by, timeout=timeout)408 return True409 def assert_element_not_visible(410 self, selector, by=By.CSS_SELECTOR, timeout=None411 ):412 """Similar to wait_for_element_not_visible()413 As above, will raise an exception if the element stays visible.414 Returns True if successful. Default timeout = SMALL_TIMEOUT."""415 self.__check_scope__()416 timeout = self.get_timeout(timeout, constants.SMALL_TIMEOUT)417 self.wait_for_element_not_visible(selector, by=by, timeout=timeout)418 return True419 def assert_element_present(420 self, selector, by=By.CSS_SELECTOR, timeout=None421 ):422 """Similar to wait_for_element_present(), but returns nothing.423 Waits for an element to appear in the HTML of a page.424 The element does not need be visible (it may be hidden).425 Returns True if successful. Default timeout = SMALL_TIMEOUT."""426 self.__check_scope()427 timeout = self.get_timeout(timeout, constants.SMALL_TIMEOUT)428 if type(selector) is list:429 self.assert_elements_present(selector, by=by, timeout=timeout)430 return True431 if self.__is_shadow_selector(selector):432 self.__assert_shadow_element_present(selector)433 return True434 self.wait_for_element_present(selector, by=by, timeout=timeout)435 return True436 def assert_elements_present(self, *args, **kwargs):437 """Similar to self.assert_element_present(),438 but can assert that multiple elements are present in the HTML.439 The input is a list of elements.440 Optional kwargs include "by" and "timeout" (used by all selectors).441 Raises an exception if any of the elements are not visible.442 Examples:443 self.assert_elements_present("head", "style", "script", "body")444 OR445 self.assert_elements_present(["head", "body", "h1", "h2"])446 """447 self.__check_scope()448 selectors = []449 timeout = None450 by = By.CSS_SELECTOR451 for kwarg in kwargs:452 if kwarg == "timeout":453 timeout = kwargs["timeout"]454 elif kwarg == "by":455 by = kwargs["by"]456 elif kwarg == "selector":457 selector = kwargs["selector"]458 if type(selector) is str:459 selectors.append(selector)460 elif type(selector) is list:461 selectors_list = selector462 for selector in selectors_list:463 if type(selector) is str:464 selectors.append(selector)465 else:466 raise Exception('Unknown kwarg: "%s"!' % kwarg)467 timeout = self.get_timeout(timeout, constants.SMALL_TIMEOUT)468 for arg in args:469 if type(arg) is list:470 for selector in arg:471 if type(selector) is str:472 selectors.append(selector)473 elif type(arg) is str:474 selectors.append(arg)475 for selector in selectors:476 if self.__is_shadow_selector(selector):477 self.__assert_shadow_element_visible(selector)478 continue479 self.wait_for_element_present(selector, by=by, timeout=timeout)480 continue481 return True482 def assert_text_visible(483 self, text, selector="html", by=By.CSS_SELECTOR, timeout=None484 ):485 """ Same as assert_text() """486 self.__check_scope()487 timeout = self.get_timeout(timeout, constants.SMALL_TIMEOUT)488 return self.assert_text(text, selector, by=by, timeout=timeout)489 def assert_text(490 self, text, selector="html", by=By.CSS_SELECTOR, timeout=None491 ):...

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