Best Python code snippet using SeleniumBase
base_case.py
Source:base_case.py
...136 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])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_chain...
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!!