Best Python code snippet using SeleniumBase
page_actions.py
Source:page_actions.py
...92 element = driver.find_element(by=by, value=selector)93 return element.is_displayed() and text in element.text94 except Exception:95 return False96def is_attribute_present(97 driver, selector, attribute, value=None, by=MobileBy.ACCESSIBILITY_ID98):99 """100 Returns whether the specified attribute is present in the given selector.101 @Params102 driver - the webdriver object (required)103 selector - the locator for identifying the page element (required)104 attribute - the attribute that is expected for the element (required)105 value - the attribute value that is expected (Default: None)106 by - the type of selector being used (Default: MobileBy.CSS_SELECTOR)107 @Returns108 Boolean (is attribute present)109 """110 try:111 element = driver.find_element(by=by, value=selector)112 found_value = element.get_attribute(attribute)113 if found_value is None:114 raise Exception()115 if value is not None:116 if found_value == value:117 return True118 else:119 raise Exception()120 else:121 return True122 except Exception:123 return False124def hover_on_element(driver, selector, by=MobileBy.ACCESSIBILITY_ID):125 """126 Fires the hover event for the specified element by the given selector.127 @Params128 driver - the webdriver object (required)129 selector - the locator for identifying the page element (required)130 by - the type of selector being used (Default: MobileBy.CSS_SELECTOR)131 """132 element = driver.find_element(by=by, value=selector)133 hover = ActionChains(driver).move_to_element(element)134 hover.perform()135def hover_element(driver, element):136 """137 Similar to hover_on_element(), but uses found element, not a selector.138 """139 hover = ActionChains(driver).move_to_element(element)140 hover.perform()141def timeout_exception(exception, message):142 exception, message = s_utils.format_exc(exception, message)143 raise exception(message)144def hover_and_click(145 driver,146 hover_selector,147 click_selector,148 hover_by=MobileBy.ACCESSIBILITY_ID,149 click_by=MobileBy.ACCESSIBILITY_ID,150 timeout=settings.SMALL_TIMEOUT,151):152 """153 Fires the hover event for a specified element by a given selector, then154 clicks on another element specified. Useful for dropdown hover based menus.155 @Params156 driver - the webdriver object (required)157 hover_selector - the css selector to hover over (required)158 click_selector - the css selector to click on (required)159 hover_by - the hover selector type to search by (Default: MobileBy.CSS_SELECTOR)160 click_by - the click selector type to search by (Default: MobileBy.CSS_SELECTOR)161 timeout - number of seconds to wait for click element to appear after hover162 """163 start_ms = time.time() * 1000.0164 stop_ms = start_ms + (timeout * 1000.0)165 element = driver.find_element(by=hover_by, value=hover_selector)166 hover = ActionChains(driver).move_to_element(element)167 for x in range(int(timeout * 10)):168 try:169 hover.perform()170 element = driver.find_element(by=click_by, value=click_selector)171 element.click()172 return element173 except Exception:174 now_ms = time.time() * 1000.0175 if now_ms >= stop_ms:176 break177 time.sleep(0.1)178 plural = "s"179 if timeout == 1:180 plural = ""181 message = "Element {%s} was not present after %s second%s!" % (182 click_selector,183 timeout,184 plural,185 )186 timeout_exception(NoSuchElementException, message)187def hover_element_and_click(188 driver,189 element,190 click_selector,191 click_by=MobileBy.ACCESSIBILITY_ID,192 timeout=settings.SMALL_TIMEOUT,193):194 """195 Similar to hover_and_click(), but assumes top element is already found.196 """197 start_ms = time.time() * 1000.0198 stop_ms = start_ms + (timeout * 1000.0)199 hover = ActionChains(driver).move_to_element(element)200 for x in range(int(timeout * 10)):201 try:202 hover.perform()203 element = driver.find_element(by=click_by, value=click_selector)204 element.click()205 return element206 except Exception:207 now_ms = time.time() * 1000.0208 if now_ms >= stop_ms:209 break210 time.sleep(0.1)211 plural = "s"212 if timeout == 1:213 plural = ""214 message = "Element {%s} was not present after %s second%s!" % (215 click_selector,216 timeout,217 plural,218 )219 timeout_exception(NoSuchElementException, message)220def hover_element_and_double_click(221 driver,222 element,223 click_selector,224 click_by=MobileBy.ACCESSIBILITY_ID,225 timeout=settings.SMALL_TIMEOUT,226):227 start_ms = time.time() * 1000.0228 stop_ms = start_ms + (timeout * 1000.0)229 hover = ActionChains(driver).move_to_element(element)230 for x in range(int(timeout * 10)):231 try:232 hover.perform()233 element_2 = driver.find_element(by=click_by, value=click_selector)234 actions = ActionChains(driver)235 actions.move_to_element(element_2)236 actions.double_click(element_2)237 actions.perform()238 return element_2239 except Exception:240 now_ms = time.time() * 1000.0241 if now_ms >= stop_ms:242 break243 time.sleep(0.1)244 plural = "s"245 if timeout == 1:246 plural = ""247 message = "Element {%s} was not present after %s second%s!" % (248 click_selector,249 timeout,250 plural,251 )252 timeout_exception(NoSuchElementException, message)253def wait_for_element_present(driver, selector, by=MobileBy.ACCESSIBILITY_ID, timeout=settings.LARGE_TIMEOUT):254 element = None255 start_ms = time.time() * 1000.0256 stop_ms = start_ms + (timeout * 1000.0)257 for x in range(int(timeout * 10)):258 #s_utils.check_if_time_limit_exceeded()259 try:260 element = driver.find_element(by=by, value=selector)261 return element262 except Exception:263 now_ms = time.time() * 1000.0264 if now_ms >= stop_ms:265 break266 time.sleep(0.1)267 plural = "s"268 if timeout == 1:269 plural = ""270 if not element:271 message = "Element {%s} was not present after %s second%s!" % (272 selector,273 timeout,274 plural,275 )276 timeout_exception(NoSuchElementException, message)277def wait_for_element_visible(278 driver, selector, by=MobileBy.ACCESSIBILITY_ID, timeout=settings.LARGE_TIMEOUT279):280 """281 Searches for the specified element by the given selector. Returns the282 element object if the element is present and visible on the page.283 Raises NoSuchElementException if the element does not exist284 within the specified timeout.285 Raises ElementNotVisibleException if the element exists,286 but is not visible (eg. opacity is "0") within the specified timeout.287 @Params288 driver - the webdriver object (required)289 selector - the locator for identifying the page element (required)290 by - the type of selector being used (Default: MobileBy.CSS_SELECTOR)291 timeout - the time to wait for elements in seconds292 @Returns293 A web element object294 """295 element = None296 is_present = False297 start_ms = time.time() * 1000.0298 stop_ms = start_ms + (timeout * 1000.0)299 for x in range(int(timeout * 10)):300 try:301 element = driver.find_element(by=by, value=selector)302 is_present = True303 if element.is_displayed():304 return element305 else:306 element = None307 raise Exception()308 except Exception:309 now_ms = time.time() * 1000.0310 if now_ms >= stop_ms:311 break312 time.sleep(0.1)313 plural = "s"314 if timeout == 1:315 plural = ""316 if not element and by != MobileBy.LINK_TEXT:317 if not is_present:318 # The element does not exist in the HTML319 message = "Element {%s} was not present after %s second%s!" % (320 selector,321 timeout,322 plural,323 )324 timeout_exception(NoSuchElementException, message)325 # The element exists in the HTML, but is not visible326 message = "Element {%s} was not visible after %s second%s!" % (327 selector,328 timeout,329 plural,330 )331 timeout_exception(ElementNotVisibleException, message)332 if not element and by == MobileBy.LINK_TEXT:333 message = "Link text {%s} was not visible after %s second%s!" % (334 selector,335 timeout,336 plural,337 )338 timeout_exception(ElementNotVisibleException, message)339def wait_for_text_visible(340 driver, text, selector, by=MobileBy.ACCESSIBILITY_ID, timeout=settings.LARGE_TIMEOUT341):342 """343 Searches for the specified element by the given selector. Returns the344 element object if the text is present in the element and visible345 on the page.346 Raises NoSuchElementException if the element does not exist in the HTML347 within the specified timeout.348 Raises ElementNotVisibleException if the element exists in the HTML,349 but the text is not visible within the specified timeout.350 @Params351 driver - the webdriver object (required)352 text - the text that is being searched for in the element (required)353 selector - the locator for identifying the page element (required)354 by - the type of selector being used (Default: MobileBy.CSS_SELECTOR)355 timeout - the time to wait for elements in seconds356 @Returns357 A web element object that contains the text searched for358 """359 element = None360 is_present = False361 start_ms = time.time() * 1000.0362 stop_ms = start_ms + (timeout * 1000.0)363 for x in range(int(timeout * 10)):364 s_utils.check_if_time_limit_exceeded()365 try:366 element = driver.find_element(by=by, value=selector)367 is_present = True368 if element.is_displayed() and text in element.text:369 return element370 else:371 element = None372 raise Exception()373 except Exception:374 now_ms = time.time() * 1000.0375 if now_ms >= stop_ms:376 break377 time.sleep(0.1)378 plural = "s"379 if timeout == 1:380 plural = ""381 if not element:382 if not is_present:383 # The element does not exist in the HTML384 message = "Element {%s} was not present after %s second%s!" % (385 selector,386 timeout,387 plural,388 )389 timeout_exception(NoSuchElementException, message)390 # The element exists in the HTML, but the text is not visible391 message = (392 "Expected text {%s} for {%s} was not visible after %s second%s!"393 % (text, selector, timeout, plural)394 )395 timeout_exception(ElementNotVisibleException, message)396def wait_for_exact_text_visible(397 driver, text, selector, by=MobileBy.ACCESSIBILITY_ID, timeout=settings.LARGE_TIMEOUT398):399 """400 Searches for the specified element by the given selector. Returns the401 element object if the text matches exactly with the text in the element,402 and the text is visible.403 Raises NoSuchElementException if the element does not exist in the HTML404 within the specified timeout.405 Raises ElementNotVisibleException if the element exists in the HTML,406 but the exact text is not visible within the specified timeout.407 @Params408 driver - the webdriver object (required)409 text - the exact text that is expected for the element (required)410 selector - the locator for identifying the page element (required)411 by - the type of selector being used (Default: MobileBy.CSS_SELECTOR)412 timeout - the time to wait for elements in seconds413 @Returns414 A web element object that contains the text searched for415 """416 element = None417 is_present = False418 start_ms = time.time() * 1000.0419 stop_ms = start_ms + (timeout * 1000.0)420 for x in range(int(timeout * 10)):421 s_utils.check_if_time_limit_exceeded()422 try:423 element = driver.find_element(by=by, value=selector)424 is_present = True425 if element.is_displayed() and text.strip() == element.text.strip():426 return element427 else:428 element = None429 raise Exception()430 except Exception:431 now_ms = time.time() * 1000.0432 if now_ms >= stop_ms:433 break434 time.sleep(0.1)435 plural = "s"436 if timeout == 1:437 plural = ""438 if not element:439 if not is_present:440 # The element does not exist in the HTML441 message = "Element {%s} was not present after %s second%s!" % (442 selector,443 timeout,444 plural,445 )446 timeout_exception(NoSuchElementException, message)447 # The element exists in the HTML, but the exact text is not visible448 message = (449 "Expected exact text {%s} for {%s} was not visible "450 "after %s second%s!" % (text, selector, timeout, plural)451 )452 timeout_exception(ElementNotVisibleException, message)453def wait_for_attribute(454 driver,455 selector,456 attribute,457 value=None,458 by=MobileBy.ACCESSIBILITY_ID,459 timeout=settings.LARGE_TIMEOUT,460):461 """462 Searches for the specified element attribute by the given selector.463 Returns the element object if the expected attribute is present464 and the expected attribute value is present (if specified).465 Raises NoSuchElementException if the element does not exist in the HTML466 within the specified timeout.467 Raises NoSuchAttributeException if the element exists in the HTML,468 but the expected attribute/value is not present within the timeout.469 @Params470 driver - the webdriver object (required)471 selector - the locator for identifying the page element (required)472 attribute - the attribute that is expected for the element (required)473 value - the attribute value that is expected (Default: None)474 by - the type of selector being used (Default: MobileBy.CSS_SELECTOR)475 timeout - the time to wait for the element attribute in seconds476 @Returns477 A web element object that contains the expected attribute/value478 """479 element = None480 element_present = False481 attribute_present = False482 found_value = None483 start_ms = time.time() * 1000.0484 stop_ms = start_ms + (timeout * 1000.0)485 for x in range(int(timeout * 10)):486 s_utils.check_if_time_limit_exceeded()487 try:488 element = driver.find_element(by=by, value=selector)489 element_present = True490 attribute_present = False491 found_value = element.get_attribute(attribute)492 if found_value is not None:493 attribute_present = True494 else:495 element = None496 raise Exception()497 if value is not None:498 if found_value == value:499 return element500 else:501 element = None502 raise Exception()503 else:504 return element505 except Exception:506 now_ms = time.time() * 1000.0507 if now_ms >= stop_ms:508 break509 time.sleep(0.1)510 plural = "s"511 if timeout == 1:512 plural = ""513 if not element:514 if not element_present:515 # The element does not exist in the HTML516 message = "Element {%s} was not present after %s second%s!" % (517 selector,518 timeout,519 plural,520 )521 timeout_exception(NoSuchElementException, message)522 if not attribute_present:523 # The element does not have the attribute524 message = (525 "Expected attribute {%s} of element {%s} was not present "526 "after %s second%s!" % (attribute, selector, timeout, plural)527 )528 timeout_exception(NoSuchAttributeException, message)529 # The element attribute exists, but the expected value does not match530 message = (531 "Expected value {%s} for attribute {%s} of element {%s} was not "532 "present after %s second%s! (The actual value was {%s})"533 % (value, attribute, selector, timeout, plural, found_value)534 )535 timeout_exception(NoSuchAttributeException, message)536def wait_for_element_absent(537 driver, selector, by=MobileBy.ACCESSIBILITY_ID, timeout=settings.LARGE_TIMEOUT538):539 """540 Searches for the specified element by the given selector.541 Raises an exception if the element is still present after the542 specified timeout.543 @Params544 driver - the webdriver object545 selector - the locator for identifying the page element (required)546 by - the type of selector being used (Default: MobileBy.CSS_SELECTOR)547 timeout - the time to wait for elements in seconds548 """549 start_ms = time.time() * 1000.0550 stop_ms = start_ms + (timeout * 1000.0)551 for x in range(int(timeout * 10)):552 s_utils.check_if_time_limit_exceeded()553 try:554 driver.find_element(by=by, value=selector)555 now_ms = time.time() * 1000.0556 if now_ms >= stop_ms:557 break558 time.sleep(0.1)559 except Exception:560 return True561 plural = "s"562 if timeout == 1:563 plural = ""564 message = "Element {%s} was still present after %s second%s!" % (565 selector,566 timeout,567 plural,568 )569 timeout_exception(Exception, message)570def wait_for_element_not_visible(571 driver, selector, by=MobileBy.ACCESSIBILITY_ID, timeout=settings.LARGE_TIMEOUT572):573 """574 Searches for the specified element by the given selector.575 Raises an exception if the element is still visible after the576 specified timeout.577 @Params578 driver - the webdriver object (required)579 selector - the locator for identifying the page element (required)580 by - the type of selector being used (Default: MobileBy.CSS_SELECTOR)581 timeout - the time to wait for the element in seconds582 """583 start_ms = time.time() * 1000.0584 stop_ms = start_ms + (timeout * 1000.0)585 for x in range(int(timeout * 10)):586 s_utils.check_if_time_limit_exceeded()587 try:588 element = driver.find_element(by=by, value=selector)589 if element.is_displayed():590 now_ms = time.time() * 1000.0591 if now_ms >= stop_ms:592 break593 time.sleep(0.1)594 else:595 return True596 except Exception:597 return True598 plural = "s"599 if timeout == 1:600 plural = ""601 message = "Element {%s} was still visible after %s second%s!" % (602 selector,603 timeout,604 plural,605 )606 timeout_exception(Exception, message)607def wait_for_text_not_visible(608 driver, text, selector, by=MobileBy.ACCESSIBILITY_ID, timeout=settings.LARGE_TIMEOUT609):610 """611 Searches for the text in the element of the given selector on the page.612 Returns True if the text is not visible on the page within the timeout.613 Raises an exception if the text is still present after the timeout.614 @Params615 driver - the webdriver object (required)616 text - the text that is being searched for in the element (required)617 selector - the locator for identifying the page element (required)618 by - the type of selector being used (Default: MobileBy.CSS_SELECTOR)619 timeout - the time to wait for elements in seconds620 @Returns621 A web element object that contains the text searched for622 """623 start_ms = time.time() * 1000.0624 stop_ms = start_ms + (timeout * 1000.0)625 for x in range(int(timeout * 10)):626 s_utils.check_if_time_limit_exceeded()627 if not is_text_visible(driver, text, selector, by=by):628 return True629 now_ms = time.time() * 1000.0630 if now_ms >= stop_ms:631 break632 time.sleep(0.1)633 plural = "s"634 if timeout == 1:635 plural = ""636 message = "Text {%s} in {%s} was still visible after %s second%s!" % (637 text,638 selector,639 timeout,640 plural,641 )642 timeout_exception(Exception, message)643def wait_for_attribute_not_present(644 driver,645 selector,646 attribute,647 value=None,648 by=MobileBy.ACCESSIBILITY_ID,649 timeout=settings.LARGE_TIMEOUT650):651 """652 Searches for the specified element attribute by the given selector.653 Returns True if the attribute isn't present on the page within the timeout.654 Also returns True if the element is not present within the timeout.655 Raises an exception if the attribute is still present after the timeout.656 @Params657 driver - the webdriver object (required)658 selector - the locator for identifying the page element (required)659 attribute - the element attribute (required)660 value - the attribute value (Default: None)661 by - the type of selector being used (Default: MobileBy.CSS_SELECTOR)662 timeout - the time to wait for the element attribute in seconds663 """664 start_ms = time.time() * 1000.0665 stop_ms = start_ms + (timeout * 1000.0)666 for x in range(int(timeout * 10)):667 s_utils.check_if_time_limit_exceeded()668 if not is_attribute_present(669 driver, selector, attribute, value=value, by=by670 ):671 return True672 now_ms = time.time() * 1000.0673 if now_ms >= stop_ms:674 break675 time.sleep(0.1)676 plural = "s"677 if timeout == 1:678 plural = ""679 message = (680 "Attribute {%s} of element {%s} was still present after %s second%s!"681 "" % (attribute, selector, timeout, plural)682 )...
browser_actions_wrapper.py
Source:browser_actions_wrapper.py
...234 except Exception as e:235 self.log.error("Exception - get_elenemt_attribute " + str(e))236 self.log.error("Element " + locator)237 raise238 def is_attribute_present(self, locator: str, locator_type: str, attribute: str):239 try:240 element = self.get_element(locator=locator, locator_type=locator_type)241 if element.get_attribute(attribute) is not None:242 return True243 else:244 return False245 except Exception as e:246 self.log.error("Exception - is_attribute_present " + str(e))247 self.log.error("Element " + locator)248 raise249 def is_attribute_present_elements(self, locator: str, locator_type: str, index: int, attribute: str):250 try:251 elements = self.get_elements(locator=locator, locator_type=locator_type)252 if elements[index].get_attribute(attribute) is not None:...
PerformanceProfilesValidation.py
Source:PerformanceProfilesValidation.py
...120 self.validate_cpu_values(profile, cpus)121 def validate_caas_cpu_pools(self, profile, pools):122 sum_ratio = 0123 self.allowed_attributes(profile, pools, self.CAAS_CPU_POOL_ATTRIBUTES)124 self.is_attribute_present(profile, pools, self.CAAS_CPU_POOL_ATTRIBUTES)125 for value in pools.values():126 if not isinstance(value, int) or (value > 100) or (value < 0):127 self.raise_error(profile, self.ERR_CAAS_CPU_POOL_TYPE)128 sum_ratio += value129 if sum_ratio > 100:130 self.raise_error(profile, self.ERR_CPU_POOL_RATIO)131 def validate_tuning(self, profile, option):132 if option not in self.TUNING_OPTIONS:133 self.raise_error(profile, self.ERR_TUNING)134 def allowed_attributes(self, profile, entries, allowed_attributes):135 for key in entries.keys():136 if key not in allowed_attributes:137 self.raise_error(profile, 'Attribute %s is not allowed in profile %s, '138 'allowed attributes: \"%s\"' %139 (key, profile, str(",".join(allowed_attributes))))140 def is_attribute_present(self, profile, entries, attributes):141 is_present = False142 for key in entries.keys():143 if key in attributes:144 is_present = True145 if not is_present:146 self.raise_error(profile, 'Profile: %s should contain at least one of the following '147 'attributes: \"%s\"' % (profile, str(",".join(attributes))))148 def validate_caas_cpu_pool_share(self, value):149 if not isinstance(value, (int)) or (value > 100) or (value < 0):...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!