How to use __make_css_match_first_element_only method in SeleniumBase

Best Python code snippet using SeleniumBase

base_case.py

Source:base_case.py Github

copy

Full Screen

...955 duration - (Bootstrap Tours ONLY) The amount of time, in seconds,956 before automatically advancing to the next tour step.957 """958 if selector != "html":959 selector = self.__make_css_match_first_element_only(selector)960 element_row = "element: '%s'," % selector961 else:962 element_row = ""963 if not duration:964 duration = "0"965 else:966 duration = str(float(duration) * 1000.0)967 step = ("""{968 %s969 title: '%s',970 content: '%s',971 orphan: true,972 placement: 'auto %s',973 smartPlacement: true,974 duration: %s,975 },""" % (element_row, title, message, alignment, duration))976 self._tour_steps[name].append(step)977 def __add_hopscotch_tour_step(self, message, selector=None, name=None,978 title=None, alignment=None):979 """ Allows the user to add tour steps for a website.980 @Params981 message - The message to display.982 selector - The CSS Selector of the Element to attach to.983 name - If creating multiple tours at the same time,984 use this to select the tour you wish to add steps to.985 title - Additional header text that appears above the message.986 alignment - Choose from "top", "bottom", "left", and "right".987 ("bottom" is the default alignment).988 """989 arrow_offset_row = None990 if not selector or selector == "html":991 selector = "head"992 alignment = "bottom"993 arrow_offset_row = "arrowOffset: '200',"994 else:995 arrow_offset_row = ""996 step = ("""{997 target: '%s',998 title: '%s',999 content: '%s',1000 %s1001 showPrevButton: 'true',1002 scrollDuration: '550',1003 placement: '%s'},1004 """ % (selector, title, message, arrow_offset_row, alignment))1005 self._tour_steps[name].append(step)1006 def __add_introjs_tour_step(self, message, selector=None, name=None,1007 title=None, alignment=None):1008 """ Allows the user to add tour steps for a website.1009 @Params1010 message - The message to display.1011 selector - The CSS Selector of the Element to attach to.1012 name - If creating multiple tours at the same time,1013 use this to select the tour you wish to add steps to.1014 title - Additional header text that appears above the message.1015 alignment - Choose from "top", "bottom", "left", and "right".1016 ("top" is the default alignment).1017 """1018 if selector != "html":1019 element_row = "element: '%s'," % selector1020 else:1021 element_row = ""1022 if title:1023 message = "<center><b>" + title + "</b></center><hr>" + message1024 message = '<font size=\"3\" color=\"#33475B\">' + message + '</font>'1025 step = ("""{%s1026 intro: '%s',1027 position: '%s'},1028 """ % (element_row, message, alignment))1029 self._tour_steps[name].append(step)1030 def play_tour(self, name=None, interval=0):1031 """ Plays a tour on the current website.1032 @Params1033 name - If creating multiple tours at the same time,1034 use this to select the tour you wish to add steps to.1035 interval - The delay time between autoplaying tour steps.1036 If set to 0 (default), the tour is fully manual control.1037 """1038 if self.headless:1039 return # Tours should not run in headless mode.1040 if not name:1041 name = "default"1042 if name not in self._tour_steps:1043 raise Exception("Tour {%s} does not exist!" % name)1044 if "Bootstrap" in self._tour_steps[name][0]:1045 tour_helper.play_bootstrap_tour(1046 self.driver, self._tour_steps, self.browser,1047 self.message_duration, name=name, interval=interval)1048 elif "Hopscotch" in self._tour_steps[name][0]:1049 tour_helper.play_hopscotch_tour(1050 self.driver, self._tour_steps, self.browser,1051 self.message_duration, name=name, interval=interval)1052 elif "IntroJS" in self._tour_steps[name][0]:1053 tour_helper.play_introjs_tour(1054 self.driver, self._tour_steps, self.browser,1055 self.message_duration, name=name, interval=interval)1056 else:1057 # "Shepherd"1058 tour_helper.play_shepherd_tour(1059 self.driver, self._tour_steps,1060 self.message_duration, name=name, interval=interval)1061 def export_tour(self, name=None, filename="my_tour.js"):1062 """ Exports a tour as a JS file.1063 You can call self.export_tour() anywhere where you could1064 normally use self.play_tour()1065 It will include necessary resources as well, such as jQuery.1066 You'll be able to copy the tour directly into the Console of1067 any web browser to play the tour outside of SeleniumBase runs.1068 @Params1069 name - If creating multiple tours at the same time,1070 use this to select the tour you wish to add steps to.1071 filename - The name of the javascript file that you wish to1072 save the tour to. """1073 tour_helper.export_tour(self._tour_steps, name=name, filename=filename)1074 def activate_jquery_confirm(self):1075 """ See https://craftpip.github.io/jquery-confirm/ for usage. """1076 js_utils.activate_jquery_confirm(self.driver)1077 def activate_messenger(self):1078 js_utils.activate_messenger(self.driver)1079 def set_messenger_theme(self, theme="default", location="default",1080 max_messages="default"):1081 js_utils.set_messenger_theme(1082 self.driver, theme=theme,1083 location=location, max_messages=max_messages)1084 def post_message(self, message, style="info", duration=None):1085 """ Post a message on the screen with Messenger.1086 Arguments:1087 message: The message to display.1088 style: "info", "success", or "error".1089 duration: The time until the message vanishes.1090 You can also post messages by using =>1091 self.execute_script('Messenger().post("My Message")') """1092 js_utils.post_message(1093 self.driver, message, self.message_duration,1094 style=style, duration=duration)1095 def get_property_value(self, selector, property, by=By.CSS_SELECTOR,1096 timeout=settings.SMALL_TIMEOUT):1097 """ Returns the property value of a page element's computed style.1098 Example:1099 opacity = self.get_property_value("html body a", "opacity")1100 self.assertTrue(float(opacity) > 0, "Element not visible!") """1101 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1102 timeout = self.__get_new_timeout(timeout)1103 if page_utils.is_xpath_selector(selector):1104 by = By.XPATH1105 if page_utils.is_link_text_selector(selector):1106 selector = page_utils.get_link_text_from_selector(selector)1107 by = By.LINK_TEXT1108 self.wait_for_ready_state_complete()1109 page_actions.wait_for_element_present(1110 self.driver, selector, by, timeout)1111 try:1112 selector = self.convert_to_css_selector(selector, by=by)1113 except Exception:1114 # Don't run action if can't convert to CSS_Selector for JavaScript1115 raise Exception(1116 "Exception: Could not convert {%s}(by=%s) to CSS_SELECTOR!" % (1117 selector, by))1118 selector = re.escape(selector)1119 selector = self.__escape_quotes_if_needed(selector)1120 script = ("""var $elm = document.querySelector('%s');1121 $val = window.getComputedStyle($elm).getPropertyValue('%s');1122 return $val;"""1123 % (selector, property))1124 value = self.execute_script(script)1125 if value is not None:1126 return value1127 else:1128 return "" # Return an empty string if the property doesn't exist1129 def bring_to_front(self, selector, by=By.CSS_SELECTOR):1130 """ Updates the Z-index of a page element to bring it into view.1131 Useful when getting a WebDriverException, such as the one below:1132 { Element is not clickable at point (#, #).1133 Other element would receive the click: ... } """1134 if page_utils.is_xpath_selector(selector):1135 by = By.XPATH1136 self.find_element(selector, by=by, timeout=settings.SMALL_TIMEOUT)1137 try:1138 selector = self.convert_to_css_selector(selector, by=by)1139 except Exception:1140 # Don't run action if can't convert to CSS_Selector for JavaScript1141 return1142 selector = re.escape(selector)1143 selector = self.__escape_quotes_if_needed(selector)1144 script = ("""document.querySelector('%s').style.zIndex = '9999';"""1145 % selector)1146 self.execute_script(script)1147 def highlight_click(self, selector, by=By.CSS_SELECTOR,1148 loops=3, scroll=True):1149 if not self.demo_mode:1150 self.highlight(selector, by=by, loops=loops, scroll=scroll)1151 self.click(selector, by=by)1152 def highlight_update_text(self, selector, new_value, by=By.CSS_SELECTOR,1153 loops=3, scroll=True):1154 if not self.demo_mode:1155 self.highlight(selector, by=by, loops=loops, scroll=scroll)1156 self.update_text(selector, new_value, by=by)1157 def highlight(self, selector, by=By.CSS_SELECTOR,1158 loops=settings.HIGHLIGHTS, scroll=True):1159 """ This method uses fancy JavaScript to highlight an element.1160 Used during demo_mode.1161 @Params1162 selector - the selector of the element to find1163 by - the type of selector to search by (Default: CSS)1164 loops - # of times to repeat the highlight animation1165 (Default: 4. Each loop lasts for about 0.18s)1166 scroll - the option to scroll to the element first (Default: True)1167 """1168 selector, by = self.__recalculate_selector(selector, by)1169 element = self.find_element(1170 selector, by=by, timeout=settings.SMALL_TIMEOUT)1171 if scroll:1172 self.__slow_scroll_to_element(element)1173 try:1174 selector = self.convert_to_css_selector(selector, by=by)1175 except Exception:1176 # Don't highlight if can't convert to CSS_SELECTOR1177 return1178 if self.highlights:1179 loops = self.highlights1180 if self.browser == 'ie':1181 loops = 1 # Override previous setting because IE is slow1182 loops = int(loops)1183 o_bs = '' # original_box_shadow1184 style = element.get_attribute('style')1185 if style:1186 if 'box-shadow: ' in style:1187 box_start = style.find('box-shadow: ')1188 box_end = style.find(';', box_start) + 11189 original_box_shadow = style[box_start:box_end]1190 o_bs = original_box_shadow1191 if ":contains" not in selector and ":first" not in selector:1192 selector = re.escape(selector)1193 selector = self.__escape_quotes_if_needed(selector)1194 self.__highlight_with_js(selector, loops, o_bs)1195 else:1196 selector = self.__make_css_match_first_element_only(selector)1197 selector = re.escape(selector)1198 selector = self.__escape_quotes_if_needed(selector)1199 try:1200 self.__highlight_with_jquery(selector, loops, o_bs)1201 except Exception:1202 pass # JQuery probably couldn't load. Skip highlighting.1203 time.sleep(0.065)1204 def __highlight_with_js(self, selector, loops, o_bs):1205 js_utils.highlight_with_js(self.driver, selector, loops, o_bs)1206 def __highlight_with_jquery(self, selector, loops, o_bs):1207 js_utils.highlight_with_jquery(self.driver, selector, loops, o_bs)1208 def scroll_to(self, selector, by=By.CSS_SELECTOR,1209 timeout=settings.SMALL_TIMEOUT):1210 ''' Fast scroll to destination '''1211 if self.demo_mode:1212 self.slow_scroll_to(selector, by=by, timeout=timeout)1213 return1214 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1215 timeout = self.__get_new_timeout(timeout)1216 element = self.wait_for_element_visible(1217 selector, by=by, timeout=timeout)1218 try:1219 self.__scroll_to_element(element)1220 except (StaleElementReferenceException, ENI_Exception):1221 self.wait_for_ready_state_complete()1222 time.sleep(0.05)1223 element = self.wait_for_element_visible(1224 selector, by=by, timeout=timeout)1225 self.__scroll_to_element(element)1226 def slow_scroll_to(self, selector, by=By.CSS_SELECTOR,1227 timeout=settings.SMALL_TIMEOUT):1228 ''' Slow motion scroll to destination '''1229 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1230 timeout = self.__get_new_timeout(timeout)1231 element = self.wait_for_element_visible(1232 selector, by=by, timeout=timeout)1233 self.__slow_scroll_to_element(element)1234 def scroll_click(self, selector, by=By.CSS_SELECTOR):1235 # DEPRECATED - self.click() now scrolls to the element before clicking1236 # self.scroll_to(selector, by=by)1237 self.click(selector, by=by)1238 def click_xpath(self, xpath):1239 self.click(xpath, by=By.XPATH)1240 def js_click(self, selector, by=By.CSS_SELECTOR):1241 """ Clicks an element using pure JS. Does not use jQuery. """1242 selector, by = self.__recalculate_selector(selector, by)1243 if by == By.LINK_TEXT:1244 message = (1245 "Pure JavaScript doesn't support clicking by Link Text. "1246 "You may want to use self.jquery_click() instead, which "1247 "allows this with :contains(), assuming jQuery isn't blocked. "1248 "For now, self.js_click() will use a regular WebDriver click.")1249 logging.debug(message)1250 self.click(selector, by=by)1251 return1252 element = self.wait_for_element_present(1253 selector, by=by, timeout=settings.SMALL_TIMEOUT)1254 if self.is_element_visible(selector, by=by):1255 self.__demo_mode_highlight_if_active(selector, by)1256 if not self.demo_mode:1257 self.__scroll_to_element(element)1258 css_selector = self.convert_to_css_selector(selector, by=by)1259 css_selector = re.escape(css_selector)1260 css_selector = self.__escape_quotes_if_needed(css_selector)1261 self.__js_click(selector, by=by) # The real "magic" happens here1262 self.__demo_mode_pause_if_active()1263 def jquery_click(self, selector, by=By.CSS_SELECTOR):1264 """ Clicks an element using jQuery. Different from using pure JS. """1265 selector, by = self.__recalculate_selector(selector, by)1266 self.wait_for_element_present(1267 selector, by=by, timeout=settings.SMALL_TIMEOUT)1268 if self.is_element_visible(selector, by=by):1269 self.__demo_mode_highlight_if_active(selector, by)1270 selector = self.convert_to_css_selector(selector, by=by)1271 selector = self.__make_css_match_first_element_only(selector)1272 click_script = """jQuery('%s')[0].click()""" % selector1273 self.safe_execute_script(click_script)1274 self.__demo_mode_pause_if_active()1275 def hide_element(self, selector, by=By.CSS_SELECTOR):1276 """ Hide the first element on the page that matches the selector. """1277 selector, by = self.__recalculate_selector(selector, by)1278 selector = self.convert_to_css_selector(selector, by=by)1279 selector = self.__make_css_match_first_element_only(selector)1280 hide_script = """jQuery('%s').hide()""" % selector1281 self.safe_execute_script(hide_script)1282 def hide_elements(self, selector, by=By.CSS_SELECTOR):1283 """ Hide all elements on the page that match the selector. """1284 selector, by = self.__recalculate_selector(selector, by)1285 selector = self.convert_to_css_selector(selector, by=by)1286 hide_script = """jQuery('%s').hide()""" % selector1287 self.safe_execute_script(hide_script)1288 def show_element(self, selector, by=By.CSS_SELECTOR):1289 """ Show the first element on the page that matches the selector. """1290 selector, by = self.__recalculate_selector(selector, by)1291 selector = self.convert_to_css_selector(selector, by=by)1292 selector = self.__make_css_match_first_element_only(selector)1293 show_script = """jQuery('%s').show(0)""" % selector1294 self.safe_execute_script(show_script)1295 def show_elements(self, selector, by=By.CSS_SELECTOR):1296 """ Show all elements on the page that match the selector. """1297 selector, by = self.__recalculate_selector(selector, by)1298 selector = self.convert_to_css_selector(selector, by=by)1299 show_script = """jQuery('%s').show(0)""" % selector1300 self.safe_execute_script(show_script)1301 def remove_element(self, selector, by=By.CSS_SELECTOR):1302 """ Remove the first element on the page that matches the selector. """1303 selector, by = self.__recalculate_selector(selector, by)1304 selector = self.convert_to_css_selector(selector, by=by)1305 selector = self.__make_css_match_first_element_only(selector)1306 remove_script = """jQuery('%s').remove()""" % selector1307 self.safe_execute_script(remove_script)1308 def remove_elements(self, selector, by=By.CSS_SELECTOR):1309 """ Remove all elements on the page that match the selector. """1310 selector, by = self.__recalculate_selector(selector, by)1311 selector = self.convert_to_css_selector(selector, by=by)1312 remove_script = """jQuery('%s').remove()""" % selector1313 self.safe_execute_script(remove_script)1314 def ad_block(self):1315 from seleniumbase.config import ad_block_list1316 for css_selector in ad_block_list.AD_BLOCK_LIST:1317 css_selector = re.escape(css_selector)1318 css_selector = self.__escape_quotes_if_needed(css_selector)1319 script = ("""var $elements = document.querySelectorAll('%s');1320 var index = 0, length = $elements.length;1321 for(; index < length; index++){1322 $elements[index].remove();}"""1323 % css_selector)1324 try:1325 self.execute_script(script)1326 except Exception:1327 pass # Don't fail test if ad_blocking fails1328 def jq_format(self, code):1329 # DEPRECATED - Use re.escape() instead, which does the action you want.1330 return js_utils._jq_format(code)1331 def get_domain_url(self, url):1332 return page_utils.get_domain_url(url)1333 def safe_execute_script(self, script):1334 """ When executing a script that contains a jQuery command,1335 it's important that the jQuery library has been loaded first.1336 This method will load jQuery if it wasn't already loaded. """1337 try:1338 self.execute_script(script)1339 except Exception:1340 # The likely reason this fails is because: "jQuery is not defined"1341 self.activate_jquery() # It's a good thing we can define it here1342 self.execute_script(script)1343 def download_file(self, file_url, destination_folder=None):1344 """ Downloads the file from the url to the destination folder.1345 If no destination folder is specified, the default one is used.1346 (The default downloads folder = "./downloaded_files") """1347 if not destination_folder:1348 destination_folder = constants.Files.DOWNLOADS_FOLDER1349 page_utils._download_file_to(file_url, destination_folder)1350 def save_file_as(self, file_url, new_file_name, destination_folder=None):1351 """ Similar to self.download_file(), except that you get to rename the1352 file being downloaded to whatever you want. """1353 if not destination_folder:1354 destination_folder = constants.Files.DOWNLOADS_FOLDER1355 page_utils._download_file_to(1356 file_url, destination_folder, new_file_name)1357 def save_data_as(self, data, file_name, destination_folder=None):1358 """ Saves the data specified to a file of the name specified.1359 If no destination folder is specified, the default one is used.1360 (The default downloads folder = "./downloaded_files") """1361 if not destination_folder:1362 destination_folder = constants.Files.DOWNLOADS_FOLDER1363 page_utils._save_data_as(data, destination_folder, file_name)1364 def get_downloads_folder(self):1365 """ Returns the OS path of the Downloads Folder.1366 (Works with Chrome and Firefox only, for now.) """1367 return download_helper.get_downloads_folder()1368 def get_path_of_downloaded_file(self, file):1369 """ Returns the OS path of the downloaded file. """1370 return os.path.join(self.get_downloads_folder(), file)1371 def is_downloaded_file_present(self, file):1372 """ Checks if the file exists in the Downloads Folder. """1373 return os.path.exists(self.get_path_of_downloaded_file(file))1374 def assert_downloaded_file(self, file):1375 """ Asserts that the file exists in the Downloads Folder. """1376 assert os.path.exists(self.get_path_of_downloaded_file(file))1377 def get_google_auth_password(self, totp_key=None):1378 """ Returns a time-based one-time password based on the1379 Google Authenticator password algorithm. Works with Authy.1380 If "totp_key" is not specified, defaults to using the one1381 provided in seleniumbase/config/settings.py1382 Google Auth passwords expire and change at 30-second intervals.1383 If the fetched password expires in the next 1.5 seconds, waits1384 for a new one before returning it (may take up to 1.5 seconds).1385 See https://pyotp.readthedocs.io/en/latest/ for details. """1386 import pyotp1387 if not totp_key:1388 totp_key = settings.TOTP_KEY1389 epoch_interval = time.time() / 30.01390 cycle_lifespan = float(epoch_interval) - int(epoch_interval)1391 if float(cycle_lifespan) > 0.95:1392 # Password expires in the next 1.5 seconds. Wait for a new one.1393 for i in range(30):1394 time.sleep(0.05)1395 epoch_interval = time.time() / 30.01396 cycle_lifespan = float(epoch_interval) - int(epoch_interval)1397 if not float(cycle_lifespan) > 0.95:1398 # The new password cycle has begun1399 break1400 totp = pyotp.TOTP(totp_key)1401 return str(totp.now())1402 def convert_xpath_to_css(self, xpath):1403 return xpath_to_css.convert_xpath_to_css(xpath)1404 def convert_to_css_selector(self, selector, by):1405 """ This method converts a selector to a CSS_SELECTOR.1406 jQuery commands require a CSS_SELECTOR for finding elements.1407 This method should only be used for jQuery/JavaScript actions.1408 Pure JavaScript doesn't support using a:contains("LINK_TEXT"). """1409 if by == By.CSS_SELECTOR:1410 return selector1411 elif by == By.ID:1412 return '#%s' % selector1413 elif by == By.CLASS_NAME:1414 return '.%s' % selector1415 elif by == By.NAME:1416 return '[name="%s"]' % selector1417 elif by == By.TAG_NAME:1418 return selector1419 elif by == By.XPATH:1420 return self.convert_xpath_to_css(selector)1421 elif by == By.LINK_TEXT:1422 return 'a:contains("%s")' % selector1423 elif by == By.PARTIAL_LINK_TEXT:1424 return 'a:contains("%s")' % selector1425 else:1426 raise Exception(1427 "Exception: Could not convert {%s}(by=%s) to CSS_SELECTOR!" % (1428 selector, by))1429 def set_value(self, selector, new_value, by=By.CSS_SELECTOR,1430 timeout=settings.LARGE_TIMEOUT):1431 """ This method uses JavaScript to update a text field. """1432 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1433 timeout = self.__get_new_timeout(timeout)1434 if page_utils.is_xpath_selector(selector):1435 by = By.XPATH1436 orginal_selector = selector1437 css_selector = self.convert_to_css_selector(selector, by=by)1438 self.__demo_mode_highlight_if_active(orginal_selector, by)1439 if not self.demo_mode:1440 self.scroll_to(orginal_selector, by=by, timeout=timeout)1441 value = re.escape(new_value)1442 value = self.__escape_quotes_if_needed(value)1443 css_selector = re.escape(css_selector)1444 css_selector = self.__escape_quotes_if_needed(css_selector)1445 script = ("""document.querySelector('%s').value='%s';"""1446 % (css_selector, value))1447 self.execute_script(script)1448 if new_value.endswith('\n'):1449 element = self.wait_for_element_present(1450 orginal_selector, by=by, timeout=timeout)1451 element.send_keys(Keys.RETURN)1452 if settings.WAIT_FOR_RSC_ON_PAGE_LOADS:1453 self.wait_for_ready_state_complete()1454 self.__demo_mode_pause_if_active()1455 def js_update_text(self, selector, new_value, by=By.CSS_SELECTOR,1456 timeout=settings.LARGE_TIMEOUT):1457 """ Same as self.set_value() """1458 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1459 timeout = self.__get_new_timeout(timeout)1460 self.set_value(1461 selector, new_value, by=by, timeout=timeout)1462 def jquery_update_text_value(self, selector, new_value, by=By.CSS_SELECTOR,1463 timeout=settings.LARGE_TIMEOUT):1464 """ This method uses jQuery to update a text field.1465 If the new_value string ends with the newline character,1466 WebDriver will finish the call, which simulates pressing1467 {Enter/Return} after the text is entered. """1468 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1469 timeout = self.__get_new_timeout(timeout)1470 if page_utils.is_xpath_selector(selector):1471 by = By.XPATH1472 element = self.wait_for_element_visible(1473 selector, by=by, timeout=timeout)1474 self.__demo_mode_highlight_if_active(selector, by)1475 self.scroll_to(selector, by=by)1476 selector = self.convert_to_css_selector(selector, by=by)1477 selector = self.__make_css_match_first_element_only(selector)1478 selector = self.__escape_quotes_if_needed(selector)1479 new_value = re.escape(new_value)1480 new_value = self.__escape_quotes_if_needed(new_value)1481 update_text_script = """jQuery('%s').val('%s')""" % (1482 selector, new_value)1483 self.safe_execute_script(update_text_script)1484 if new_value.endswith('\n'):1485 element.send_keys('\n')1486 self.__demo_mode_pause_if_active()1487 def jquery_update_text(self, selector, new_value, by=By.CSS_SELECTOR,1488 timeout=settings.LARGE_TIMEOUT):1489 """ The shorter version of self.jquery_update_text_value()1490 (The longer version remains for backwards compatibility.) """1491 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1492 timeout = self.__get_new_timeout(timeout)1493 self.jquery_update_text_value(1494 selector, new_value, by=by, timeout=timeout)1495 def hover_on_element(self, selector, by=By.CSS_SELECTOR):1496 if page_utils.is_xpath_selector(selector):1497 selector = self.convert_to_css_selector(selector, By.XPATH)1498 by = By.CSS_SELECTOR1499 if page_utils.is_link_text_selector(selector):1500 selector = page_utils.get_link_text_from_selector(selector)1501 by = By.LINK_TEXT1502 self.wait_for_element_visible(1503 selector, by=by, timeout=settings.SMALL_TIMEOUT)1504 self.__demo_mode_highlight_if_active(selector, by)1505 self.scroll_to(selector, by=by)1506 time.sleep(0.05) # Settle down from scrolling before hovering1507 return page_actions.hover_on_element(self.driver, selector)1508 def hover_and_click(self, hover_selector, click_selector,1509 hover_by=By.CSS_SELECTOR, click_by=By.CSS_SELECTOR,1510 timeout=settings.SMALL_TIMEOUT):1511 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1512 timeout = self.__get_new_timeout(timeout)1513 if page_utils.is_xpath_selector(hover_selector):1514 hover_selector = self.convert_to_css_selector(1515 hover_selector, By.XPATH)1516 hover_by = By.CSS_SELECTOR1517 if page_utils.is_xpath_selector(click_selector):1518 click_by = By.XPATH1519 if page_utils.is_link_text_selector(hover_selector):1520 hover_selector = page_utils.get_link_text_from_selector(1521 hover_selector)1522 hover_by = By.LINK_TEXT1523 if page_utils.is_link_text_selector(click_selector):1524 click_selector = page_utils.get_link_text_from_selector(1525 click_selector)1526 click_by = By.LINK_TEXT1527 self.wait_for_element_visible(1528 hover_selector, by=hover_by, timeout=timeout)1529 self.__demo_mode_highlight_if_active(hover_selector, hover_by)1530 self.scroll_to(hover_selector, by=hover_by)1531 pre_action_url = self.driver.current_url1532 element = page_actions.hover_and_click(1533 self.driver, hover_selector, click_selector,1534 hover_by, click_by, timeout)1535 if self.demo_mode:1536 if self.driver.current_url != pre_action_url:1537 self.__demo_mode_pause_if_active()1538 else:1539 self.__demo_mode_pause_if_active(tiny=True)1540 return element1541 def __select_option(self, dropdown_selector, option,1542 dropdown_by=By.CSS_SELECTOR, option_by="text",1543 timeout=settings.SMALL_TIMEOUT):1544 """ Selects an HTML <select> option by specification.1545 Option specifications are by "text", "index", or "value".1546 Defaults to "text" if option_by is unspecified or unknown. """1547 if page_utils.is_xpath_selector(dropdown_selector):1548 dropdown_by = By.XPATH1549 element = self.find_element(1550 dropdown_selector, by=dropdown_by, timeout=timeout)1551 self.__demo_mode_highlight_if_active(dropdown_selector, dropdown_by)1552 pre_action_url = self.driver.current_url1553 try:1554 if option_by == "index":1555 Select(element).select_by_index(option)1556 elif option_by == "value":1557 Select(element).select_by_value(option)1558 else:1559 Select(element).select_by_visible_text(option)1560 except (StaleElementReferenceException, ENI_Exception):1561 self.wait_for_ready_state_complete()1562 time.sleep(0.05)1563 element = self.find_element(1564 dropdown_selector, by=dropdown_by, timeout=timeout)1565 if option_by == "index":1566 Select(element).select_by_index(option)1567 elif option_by == "value":1568 Select(element).select_by_value(option)1569 else:1570 Select(element).select_by_visible_text(option)1571 if settings.WAIT_FOR_RSC_ON_CLICKS:1572 self.wait_for_ready_state_complete()1573 if self.demo_mode:1574 if self.driver.current_url != pre_action_url:1575 self.__demo_mode_pause_if_active()1576 else:1577 self.__demo_mode_pause_if_active(tiny=True)1578 def select_option_by_text(self, dropdown_selector, option,1579 dropdown_by=By.CSS_SELECTOR,1580 timeout=settings.SMALL_TIMEOUT):1581 """ Selects an HTML <select> option by option text.1582 @Params1583 dropdown_selector - the <select> selector1584 option - the text of the option """1585 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1586 timeout = self.__get_new_timeout(timeout)1587 self.__select_option(dropdown_selector, option,1588 dropdown_by=dropdown_by, option_by="text",1589 timeout=timeout)1590 def select_option_by_index(self, dropdown_selector, option,1591 dropdown_by=By.CSS_SELECTOR,1592 timeout=settings.SMALL_TIMEOUT):1593 """ Selects an HTML <select> option by option index.1594 @Params1595 dropdown_selector - the <select> selector1596 option - the index number of the option """1597 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1598 timeout = self.__get_new_timeout(timeout)1599 self.__select_option(dropdown_selector, option,1600 dropdown_by=dropdown_by, option_by="index",1601 timeout=timeout)1602 def select_option_by_value(self, dropdown_selector, option,1603 dropdown_by=By.CSS_SELECTOR,1604 timeout=settings.SMALL_TIMEOUT):1605 """ Selects an HTML <select> option by option value.1606 @Params1607 dropdown_selector - the <select> selector1608 option - the value property of the option """1609 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1610 timeout = self.__get_new_timeout(timeout)1611 self.__select_option(dropdown_selector, option,1612 dropdown_by=dropdown_by, option_by="value",1613 timeout=timeout)1614 @decorators.deprecated("Use self.select_option_by_text() instead!")1615 def pick_select_option_by_text(self, dropdown_selector, option,1616 dropdown_by=By.CSS_SELECTOR,1617 timeout=settings.SMALL_TIMEOUT):1618 """ Selects an HTML <select> option by option text. """1619 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1620 timeout = self.__get_new_timeout(timeout)1621 self.__select_option(dropdown_selector, option,1622 dropdown_by=dropdown_by, option_by="text",1623 timeout=timeout)1624 @decorators.deprecated("Use self.select_option_by_index() instead!")1625 def pick_select_option_by_index(self, dropdown_selector, option,1626 dropdown_by=By.CSS_SELECTOR,1627 timeout=settings.SMALL_TIMEOUT):1628 """ Selects an HTML <select> option by option index. """1629 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1630 timeout = self.__get_new_timeout(timeout)1631 self.__select_option(dropdown_selector, option,1632 dropdown_by=dropdown_by, option_by="index",1633 timeout=timeout)1634 @decorators.deprecated("Use self.select_option_by_value() instead!")1635 def pick_select_option_by_value(self, dropdown_selector, option,1636 dropdown_by=By.CSS_SELECTOR,1637 timeout=settings.SMALL_TIMEOUT):1638 """ Selects an HTML <select> option by option value. """1639 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1640 timeout = self.__get_new_timeout(timeout)1641 self.__select_option(dropdown_selector, option,1642 dropdown_by=dropdown_by, option_by="value",1643 timeout=timeout)1644 ############1645 def generate_referral(self, start_page, destination_page):1646 """ This method opens the start_page, creates a referral link there,1647 and clicks on that link, which goes to the destination_page.1648 (This generates real traffic for testing analytics software.) """1649 if not page_utils.is_valid_url(destination_page):1650 raise Exception(1651 "Exception: destination_page {%s} is not a valid URL!"1652 % destination_page)1653 if start_page:1654 if not page_utils.is_valid_url(start_page):1655 raise Exception(1656 "Exception: start_page {%s} is not a valid URL! "1657 "(Use an empty string or None to start from current page.)"1658 % start_page)1659 self.open(start_page)1660 time.sleep(0.08)1661 referral_link = ('''<a class='analytics referral test' href='%s' '''1662 '''style='font-family: Arial,sans-serif; '''1663 '''font-size: 30px; color: #18a2cd'>'''1664 '''Magic Link Button</a>''' % destination_page)1665 self.execute_script(1666 '''document.body.innerHTML = \"%s\"''' % referral_link)1667 time.sleep(0.1)1668 self.click("a.analytics.referral.test") # Clicks the generated button1669 time.sleep(0.15)1670 try:1671 self.click("html")1672 time.sleep(0.08)1673 except Exception:1674 pass1675 def generate_traffic(self, start_page, destination_page, loops=1):1676 """ Similar to generate_referral(), but can do multiple loops. """1677 for loop in range(loops):1678 self.generate_referral(start_page, destination_page)1679 time.sleep(0.05)1680 def generate_referral_chain(self, pages):1681 """ Use this method to chain the action of creating button links on1682 one website page that will take you to the next page.1683 (When you want to create a referral to a website for traffic1684 generation without increasing the bounce rate, you'll want to visit1685 at least one additional page on that site with a button click.) """1686 if not type(pages) is tuple and not type(pages) is list:1687 raise Exception(1688 "Exception: Expecting a list of website pages for chaining!")1689 if len(pages) < 2:1690 raise Exception(1691 "Exception: At least two website pages required for chaining!")1692 for page in pages:1693 # Find out if any of the web pages are invalid before continuing1694 if not page_utils.is_valid_url(page):1695 raise Exception(1696 "Exception: Website page {%s} is not a valid URL!" % page)1697 for page in pages:1698 self.generate_referral(None, page)1699 def generate_traffic_chain(self, pages, loops=1):1700 """ Similar to generate_referral_chain(), but for multiple loops. """1701 for loop in range(loops):1702 self.generate_referral_chain(pages)1703 time.sleep(0.05)1704 ############1705 def wait_for_element_present(self, selector, by=By.CSS_SELECTOR,1706 timeout=settings.LARGE_TIMEOUT):1707 """ Waits for an element to appear in the HTML of a page.1708 The element does not need be visible (it may be hidden). """1709 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1710 timeout = self.__get_new_timeout(timeout)1711 if page_utils.is_xpath_selector(selector):1712 by = By.XPATH1713 if page_utils.is_link_text_selector(selector):1714 selector = page_utils.get_link_text_from_selector(selector)1715 by = By.LINK_TEXT1716 return page_actions.wait_for_element_present(1717 self.driver, selector, by, timeout)1718 def assert_element_present(self, selector, by=By.CSS_SELECTOR,1719 timeout=settings.SMALL_TIMEOUT):1720 """ Similar to wait_for_element_present(), but returns nothing.1721 Waits for an element to appear in the HTML of a page.1722 The element does not need be visible (it may be hidden).1723 Returns True if successful. Default timeout = SMALL_TIMEOUT. """1724 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1725 timeout = self.__get_new_timeout(timeout)1726 self.wait_for_element_present(selector, by=by, timeout=timeout)1727 return True1728 # For backwards compatibility, earlier method names of the next1729 # four methods have remained even though they do the same thing,1730 # with the exception of assert_*, which won't return the element,1731 # but like the others, will raise an exception if the call fails.1732 def wait_for_element_visible(self, selector, by=By.CSS_SELECTOR,1733 timeout=settings.LARGE_TIMEOUT):1734 """ Waits for an element to appear in the HTML of a page.1735 The element must be visible (it cannot be hidden). """1736 if page_utils.is_xpath_selector(selector):1737 by = By.XPATH1738 if page_utils.is_link_text_selector(selector):1739 selector = page_utils.get_link_text_from_selector(selector)1740 by = By.LINK_TEXT1741 return page_actions.wait_for_element_visible(1742 self.driver, selector, by, timeout)1743 def wait_for_element(self, selector, by=By.CSS_SELECTOR,1744 timeout=settings.LARGE_TIMEOUT):1745 """ The shorter version of wait_for_element_visible() """1746 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1747 timeout = self.__get_new_timeout(timeout)1748 return self.wait_for_element_visible(selector, by=by, timeout=timeout)1749 def find_element(self, selector, by=By.CSS_SELECTOR,1750 timeout=settings.LARGE_TIMEOUT):1751 """ Same as wait_for_element_visible() - returns the element """1752 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1753 timeout = self.__get_new_timeout(timeout)1754 return self.wait_for_element_visible(selector, by=by, timeout=timeout)1755 def assert_element(self, selector, by=By.CSS_SELECTOR,1756 timeout=settings.SMALL_TIMEOUT):1757 """ Similar to wait_for_element_visible(), but returns nothing.1758 As above, will raise an exception if nothing can be found.1759 Returns True if successful. Default timeout = SMALL_TIMEOUT. """1760 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1761 timeout = self.__get_new_timeout(timeout)1762 self.wait_for_element_visible(selector, by=by, timeout=timeout)1763 if self.demo_mode:1764 if page_utils.is_xpath_selector(selector):1765 by = By.XPATH1766 if page_utils.is_link_text_selector(selector):1767 selector = page_utils.get_link_text_from_selector(selector)1768 by = By.LINK_TEXT1769 messenger_post = "ASSERT %s: %s" % (by, selector)1770 self.__highlight_with_assert_success(messenger_post, selector, by)1771 return True1772 # For backwards compatibility, earlier method names of the next1773 # four methods have remained even though they do the same thing,1774 # with the exception of assert_*, which won't return the element,1775 # but like the others, will raise an exception if the call fails.1776 def wait_for_text_visible(self, text, selector, by=By.CSS_SELECTOR,1777 timeout=settings.LARGE_TIMEOUT):1778 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1779 timeout = self.__get_new_timeout(timeout)1780 if page_utils.is_xpath_selector(selector):1781 by = By.XPATH1782 if page_utils.is_link_text_selector(selector):1783 selector = page_utils.get_link_text_from_selector(selector)1784 by = By.LINK_TEXT1785 return page_actions.wait_for_text_visible(1786 self.driver, text, selector, by, timeout)1787 def wait_for_text(self, text, selector, by=By.CSS_SELECTOR,1788 timeout=settings.LARGE_TIMEOUT):1789 """ The shorter version of wait_for_text_visible() """1790 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1791 timeout = self.__get_new_timeout(timeout)1792 return self.wait_for_text_visible(1793 text, selector, by=by, timeout=timeout)1794 def find_text(self, text, selector, by=By.CSS_SELECTOR,1795 timeout=settings.LARGE_TIMEOUT):1796 """ Same as wait_for_text_visible() - returns the element """1797 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1798 timeout = self.__get_new_timeout(timeout)1799 return self.wait_for_text_visible(1800 text, selector, by=by, timeout=timeout)1801 def assert_text_visible(self, text, selector, by=By.CSS_SELECTOR,1802 timeout=settings.SMALL_TIMEOUT):1803 """ Same as assert_text() """1804 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1805 timeout = self.__get_new_timeout(timeout)1806 return self.assert_text(text, selector, by=by, timeout=timeout)1807 def assert_text(self, text, selector, by=By.CSS_SELECTOR,1808 timeout=settings.SMALL_TIMEOUT):1809 """ Similar to wait_for_text_visible()1810 Raises an exception if the element or the text is not found.1811 Returns True if successful. Default timeout = SMALL_TIMEOUT. """1812 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1813 timeout = self.__get_new_timeout(timeout)1814 self.wait_for_text_visible(text, selector, by=by, timeout=timeout)1815 if self.demo_mode:1816 if page_utils.is_xpath_selector(selector):1817 by = By.XPATH1818 if page_utils.is_link_text_selector(selector):1819 selector = page_utils.get_link_text_from_selector(selector)1820 by = By.LINK_TEXT1821 messenger_post = ("ASSERT TEXT {%s} in %s: %s"1822 % (text, by, selector))1823 self.__highlight_with_assert_success(messenger_post, selector, by)1824 return True1825 # For backwards compatibility, earlier method names of the next1826 # four methods have remained even though they do the same thing,1827 # with the exception of assert_*, which won't return the element,1828 # but like the others, will raise an exception if the call fails.1829 def wait_for_link_text_visible(self, link_text,1830 timeout=settings.LARGE_TIMEOUT):1831 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1832 timeout = self.__get_new_timeout(timeout)1833 return self.wait_for_element_visible(1834 link_text, by=By.LINK_TEXT, timeout=timeout)1835 def wait_for_link_text(self, link_text, timeout=settings.LARGE_TIMEOUT):1836 """ The shorter version of wait_for_link_text_visible() """1837 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1838 timeout = self.__get_new_timeout(timeout)1839 return self.wait_for_link_text_visible(link_text, timeout=timeout)1840 def find_link_text(self, link_text, timeout=settings.LARGE_TIMEOUT):1841 """ Same as wait_for_link_text_visible() - returns the element """1842 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1843 timeout = self.__get_new_timeout(timeout)1844 return self.wait_for_link_text_visible(link_text, timeout=timeout)1845 def assert_link_text(self, link_text, timeout=settings.SMALL_TIMEOUT):1846 """ Similar to wait_for_link_text_visible(), but returns nothing.1847 As above, will raise an exception if nothing can be found.1848 Returns True if successful. Default timeout = SMALL_TIMEOUT. """1849 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1850 timeout = self.__get_new_timeout(timeout)1851 self.wait_for_link_text_visible(link_text, timeout=timeout)1852 if self.demo_mode:1853 messenger_post = ("ASSERT LINK TEXT {%s}." % link_text)1854 self.__highlight_with_assert_success(1855 messenger_post, link_text, by=By.LINK_TEXT)1856 return True1857 # For backwards compatibility, earlier method names of the next1858 # four methods have remained even though they do the same thing,1859 # with the exception of assert_*, which won't return the element,1860 # but like the others, will raise an exception if the call fails.1861 def wait_for_partial_link_text(self, partial_link_text,1862 timeout=settings.LARGE_TIMEOUT):1863 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1864 timeout = self.__get_new_timeout(timeout)1865 return self.wait_for_element_visible(1866 partial_link_text, by=By.PARTIAL_LINK_TEXT, timeout=timeout)1867 def find_partial_link_text(self, partial_link_text,1868 timeout=settings.LARGE_TIMEOUT):1869 """ Same as wait_for_partial_link_text() - returns the element """1870 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1871 timeout = self.__get_new_timeout(timeout)1872 return self.wait_for_partial_link_text(1873 partial_link_text, timeout=timeout)1874 def assert_partial_link_text(self, partial_link_text,1875 timeout=settings.SMALL_TIMEOUT):1876 """ Similar to wait_for_partial_link_text(), but returns nothing.1877 As above, will raise an exception if nothing can be found.1878 Returns True if successful. Default timeout = SMALL_TIMEOUT. """1879 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1880 timeout = self.__get_new_timeout(timeout)1881 self.wait_for_partial_link_text(partial_link_text, timeout=timeout)1882 return True1883 ############1884 def wait_for_element_absent(self, selector, by=By.CSS_SELECTOR,1885 timeout=settings.LARGE_TIMEOUT):1886 """ Waits for an element to no longer appear in the HTML of a page.1887 A hidden element still counts as appearing in the page HTML.1888 If an element with "hidden" status is acceptable,1889 use wait_for_element_not_visible() instead. """1890 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1891 timeout = self.__get_new_timeout(timeout)1892 if page_utils.is_xpath_selector(selector):1893 by = By.XPATH1894 return page_actions.wait_for_element_absent(1895 self.driver, selector, by, timeout)1896 def assert_element_absent(self, selector, by=By.CSS_SELECTOR,1897 timeout=settings.SMALL_TIMEOUT):1898 """ Similar to wait_for_element_absent() - returns nothing.1899 As above, will raise an exception if the element stays present.1900 Returns True if successful. Default timeout = SMALL_TIMEOUT. """1901 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1902 timeout = self.__get_new_timeout(timeout)1903 self.wait_for_element_absent(selector, by=by, timeout=timeout)1904 return True1905 ############1906 def wait_for_element_not_visible(self, selector, by=By.CSS_SELECTOR,1907 timeout=settings.LARGE_TIMEOUT):1908 """ Waits for an element to no longer be visible on a page.1909 The element can be non-existant in the HTML or hidden on the page1910 to qualify as not visible. """1911 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1912 timeout = self.__get_new_timeout(timeout)1913 if page_utils.is_xpath_selector(selector):1914 by = By.XPATH1915 if page_utils.is_link_text_selector(selector):1916 selector = page_utils.get_link_text_from_selector(selector)1917 by = By.LINK_TEXT1918 return page_actions.wait_for_element_not_visible(1919 self.driver, selector, by, timeout)1920 def assert_element_not_visible(self, selector, by=By.CSS_SELECTOR,1921 timeout=settings.SMALL_TIMEOUT):1922 """ Similar to wait_for_element_not_visible() - returns nothing.1923 As above, will raise an exception if the element stays visible.1924 Returns True if successful. Default timeout = SMALL_TIMEOUT. """1925 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1926 timeout = self.__get_new_timeout(timeout)1927 self.wait_for_element_not_visible(selector, by=by, timeout=timeout)1928 return True1929 ############1930 def wait_for_ready_state_complete(self, timeout=settings.EXTREME_TIMEOUT):1931 if self.timeout_multiplier and timeout == settings.EXTREME_TIMEOUT:1932 timeout = self.__get_new_timeout(timeout)1933 is_ready = js_utils.wait_for_ready_state_complete(self.driver, timeout)1934 self.wait_for_angularjs(timeout=settings.MINI_TIMEOUT)1935 if self.ad_block_on:1936 # If the ad_block feature is enabled, then block ads for new URLs1937 current_url = self.get_current_url()1938 if not current_url == self.__last_page_load_url:1939 time.sleep(0.02)1940 self.ad_block()1941 time.sleep(0.01)1942 if self.is_element_present("iframe"):1943 time.sleep(0.07) # iframe ads take slightly longer to load1944 self.ad_block() # Do ad_block on slower-loading iframes1945 self.__last_page_load_url = current_url1946 return is_ready1947 def wait_for_angularjs(self, timeout=settings.LARGE_TIMEOUT, **kwargs):1948 js_utils.wait_for_angularjs(self.driver, timeout, **kwargs)1949 def wait_for_and_accept_alert(self, timeout=settings.LARGE_TIMEOUT):1950 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1951 timeout = self.__get_new_timeout(timeout)1952 return page_actions.wait_for_and_accept_alert(self.driver, timeout)1953 def wait_for_and_dismiss_alert(self, timeout=settings.LARGE_TIMEOUT):1954 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1955 timeout = self.__get_new_timeout(timeout)1956 return page_actions.wait_for_and_dismiss_alert(self.driver, timeout)1957 def wait_for_and_switch_to_alert(self, timeout=settings.LARGE_TIMEOUT):1958 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:1959 timeout = self.__get_new_timeout(timeout)1960 return page_actions.wait_for_and_switch_to_alert(self.driver, timeout)1961 def switch_to_frame(self, frame, timeout=settings.SMALL_TIMEOUT):1962 """ Sets driver control to the specified browser frame. """1963 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1964 timeout = self.__get_new_timeout(timeout)1965 page_actions.switch_to_frame(self.driver, frame, timeout)1966 def switch_to_default_content(self):1967 """ Brings driver control outside the current iframe.1968 (If driver control is inside an iframe, the driver control1969 will be set to one level above the current frame. If the driver1970 control is not currenly in an iframe, nothing will happen.) """1971 self.driver.switch_to.default_content()1972 def open_new_window(self, switch_to=True):1973 """ Opens a new browser tab/window and switches to it by default. """1974 self.driver.execute_script("window.open('');")1975 time.sleep(0.01)1976 if switch_to:1977 self.switch_to_window(len(self.driver.window_handles) - 1)1978 def switch_to_window(self, window, timeout=settings.SMALL_TIMEOUT):1979 if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:1980 timeout = self.__get_new_timeout(timeout)1981 page_actions.switch_to_window(self.driver, window, timeout)1982 def switch_to_default_window(self):1983 self.switch_to_window(0)1984 def save_screenshot(self, name, folder=None):1985 """ The screenshot will be in PNG format. """1986 return page_actions.save_screenshot(self.driver, name, folder)1987 def get_new_driver(self, browser=None, headless=None,1988 servername=None, port=None, proxy=None, switch_to=True):1989 """ This method spins up an extra browser for tests that require1990 more than one. The first browser is already provided by tests1991 that import base_case.BaseCase from seleniumbase. If parameters1992 aren't specified, the method uses the same as the default driver.1993 @Params1994 browser - the browser to use. (Ex: "chrome", "firefox")1995 headless - the option to run webdriver in headless mode1996 servername - if using a Selenium Grid, set the host address here1997 port - if using a Selenium Grid, set the host port here1998 proxy - if using a proxy server, specify the "host:port" combo here1999 switch_to - the option to switch to the new driver (default = True)2000 """2001 if browser is None:2002 browser = self.browser2003 browser_name = browser2004 if headless is None:2005 headless = self.headless2006 if servername is None:2007 servername = self.servername2008 if port is None:2009 port = self.port2010 use_grid = False2011 if servername != "localhost":2012 # Use Selenium Grid (Use "127.0.0.1" for localhost Grid)2013 use_grid = True2014 proxy_string = proxy2015 if proxy_string is None:2016 proxy_string = self.proxy_string2017 valid_browsers = constants.ValidBrowsers.valid_browsers2018 if browser_name not in valid_browsers:2019 raise Exception("Browser: {%s} is not a valid browser option. "2020 "Valid options = {%s}" % (browser, valid_browsers))2021 # Launch a web browser2022 from seleniumbase.core import browser_launcher2023 new_driver = browser_launcher.get_driver(browser_name=browser_name,2024 headless=headless,2025 use_grid=use_grid,2026 servername=servername,2027 port=port,2028 proxy_string=proxy_string)2029 self._drivers_list.append(new_driver)2030 if switch_to:2031 self.driver = new_driver2032 if self.headless:2033 # Make sure the invisible browser window is big enough2034 try:2035 self.set_window_size(1440, 1080)2036 self.wait_for_ready_state_complete()2037 except Exception:2038 # This shouldn't fail, but in case it does,2039 # get safely through setUp() so that2040 # WebDrivers can get closed during tearDown().2041 pass2042 else:2043 if self.browser == 'chrome':2044 try:2045 if settings.START_CHROME_IN_FULL_SCREEN_MODE:2046 self.driver.maximize_window()2047 else:2048 self.driver.set_window_size(1250, 840)2049 self.wait_for_ready_state_complete()2050 except Exception:2051 pass # Keep existing browser resolution2052 return new_driver2053 def switch_to_driver(self, driver):2054 """ Sets self.driver to the specified driver.2055 You may need this if using self.get_new_driver() in your code. """2056 self.driver = driver2057 def switch_to_default_driver(self):2058 """ Sets self.driver to the default/original driver. """2059 self.driver = self._default_driver2060 ############2061 def __get_new_timeout(self, timeout):2062 """ When using --timeout_multiplier=#.# """2063 try:2064 timeout_multiplier = float(self.timeout_multiplier)2065 if timeout_multiplier <= 0.5:2066 timeout_multiplier = 0.52067 timeout = int(math.ceil(timeout_multiplier * timeout))2068 return timeout2069 except Exception:2070 # Wrong data type for timeout_multiplier (expecting int or float)2071 return timeout2072 ############2073 def __get_exception_message(self):2074 """ This method extracts the message from an exception if there2075 was an exception that occurred during the test, assuming2076 that the exception was in a try/except block and not thrown. """2077 exception_info = sys.exc_info()[1]2078 if hasattr(exception_info, 'msg'):2079 exc_message = exception_info.msg2080 elif hasattr(exception_info, 'message'):2081 exc_message = exception_info.message2082 else:2083 exc_message = '(Unknown Exception)'2084 return exc_message2085 def __get_improved_exception_message(self):2086 """2087 If Chromedriver is out-of-date, make it clear!2088 Given the high popularity of the following StackOverflow article:2089 https://stackoverflow.com/questions/49162667/unknown-error-2090 call-function-result-missing-value-for-selenium-send-keys-even2091 ... the original error message was not helpful. Tell people directly.2092 (Only expected when using driver.send_keys() with an old Chromedriver.)2093 """2094 exc_message = self.__get_exception_message()2095 maybe_using_old_chromedriver = False2096 if "unknown error: call function result missing" in exc_message:2097 maybe_using_old_chromedriver = True2098 if self.browser == 'chrome' and maybe_using_old_chromedriver:2099 update = ("Your version of ChromeDriver may be out-of-date! "2100 "Please go to "2101 "https://sites.google.com/a/chromium.org/chromedriver/ "2102 "and download the latest version to your system PATH! "2103 "Or use: ``seleniumbase install chromedriver`` . "2104 "Original Exception Message: %s" % exc_message)2105 exc_message = update2106 return exc_message2107 def __add_delayed_assert_failure(self):2108 """ Add a delayed_assert failure into a list for future processing. """2109 current_url = self.driver.current_url2110 message = self.__get_exception_message()2111 self.__page_check_failures.append(2112 "CHECK #%s: (%s)\n %s" % (2113 self.__page_check_count, current_url, message))2114 def delayed_assert_element(self, selector, by=By.CSS_SELECTOR,2115 timeout=settings.MINI_TIMEOUT):2116 """ A non-terminating assertion for an element on a page.2117 Failures will be saved until the process_delayed_asserts()2118 method is called from inside a test, likely at the end of it. """2119 self.__page_check_count += 12120 try:2121 url = self.get_current_url()2122 if url == self.__last_url_of_delayed_assert:2123 timeout = 12124 else:2125 self.__last_url_of_delayed_assert = url2126 except Exception:2127 pass2128 try:2129 self.wait_for_element_visible(selector, by=by, timeout=timeout)2130 return True2131 except Exception:2132 self.__add_delayed_assert_failure()2133 return False2134 @decorators.deprecated("Use self.delayed_assert_element() instead!")2135 def check_assert_element(self, selector, by=By.CSS_SELECTOR,2136 timeout=settings.MINI_TIMEOUT):2137 """ DEPRECATED - Use self.delayed_assert_element() instead. """2138 return self.delayed_assert_element(selector, by=by, timeout=timeout)2139 def delayed_assert_text(self, text, selector, by=By.CSS_SELECTOR,2140 timeout=settings.MINI_TIMEOUT):2141 """ A non-terminating assertion for text from an element on a page.2142 Failures will be saved until the process_delayed_asserts()2143 method is called from inside a test, likely at the end of it. """2144 self.__page_check_count += 12145 try:2146 url = self.get_current_url()2147 if url == self.__last_url_of_delayed_assert:2148 timeout = 12149 else:2150 self.__last_url_of_delayed_assert = url2151 except Exception:2152 pass2153 try:2154 self.wait_for_text_visible(text, selector, by=by, timeout=timeout)2155 return True2156 except Exception:2157 self.__add_delayed_assert_failure()2158 return False2159 @decorators.deprecated("Use self.delayed_assert_text() instead!")2160 def check_assert_text(self, text, selector, by=By.CSS_SELECTOR,2161 timeout=settings.MINI_TIMEOUT):2162 """ DEPRECATED - Use self.delayed_assert_text() instead. """2163 return self.delayed_assert_text(text, selector, by=by, timeout=timeout)2164 def process_delayed_asserts(self, print_only=False):2165 """ To be used with any test that uses delayed_asserts, which are2166 non-terminating verifications that only raise exceptions2167 after this method is called.2168 This is useful for pages with multiple elements to be checked when2169 you want to find as many bugs as possible in a single test run2170 before having all the exceptions get raised simultaneously.2171 Might be more useful if this method is called after processing all2172 the delayed asserts on a single html page so that the failure2173 screenshot matches the location of the delayed asserts.2174 If "print_only" is set to True, the exception won't get raised. """2175 if self.__page_check_failures:2176 exception_output = ''2177 exception_output += "\n*** DELAYED ASSERTION FAILURES FOR: "2178 exception_output += "%s\n" % self.id()2179 all_failing_checks = self.__page_check_failures2180 self.__page_check_failures = []2181 for tb in all_failing_checks:2182 exception_output += "%s\n" % tb2183 if print_only:2184 print(exception_output)2185 else:2186 raise Exception(exception_output)2187 @decorators.deprecated("Use self.process_delayed_asserts() instead!")2188 def process_checks(self, print_only=False):2189 """ DEPRECATED - Use self.process_delayed_asserts() instead. """2190 self.process_delayed_asserts(print_only=print_only)2191 ############2192 def __js_click(self, selector, by=By.CSS_SELECTOR):2193 """ Clicks an element using pure JS. Does not use jQuery. """2194 selector, by = self.__recalculate_selector(selector, by)2195 css_selector = self.convert_to_css_selector(selector, by=by)2196 css_selector = re.escape(css_selector)2197 css_selector = self.__escape_quotes_if_needed(css_selector)2198 script = ("""var simulateClick = function (elem) {2199 var evt = new MouseEvent('click', {2200 bubbles: true,2201 cancelable: true,2202 view: window2203 });2204 var canceled = !elem.dispatchEvent(evt);2205 };2206 var someLink = document.querySelector('%s');2207 simulateClick(someLink);"""2208 % css_selector)2209 self.execute_script(script)2210 def __jquery_click(self, selector, by=By.CSS_SELECTOR):2211 """ Clicks an element using jQuery. Different from using pure JS. """2212 selector, by = self.__recalculate_selector(selector, by)2213 self.wait_for_element_present(2214 selector, by=by, timeout=settings.SMALL_TIMEOUT)2215 if self.is_element_visible(selector, by=by):2216 self.__demo_mode_highlight_if_active(selector, by)2217 selector = self.convert_to_css_selector(selector, by=by)2218 selector = self.__make_css_match_first_element_only(selector)2219 click_script = """jQuery('%s')[0].click()""" % selector2220 self.safe_execute_script(click_script)2221 def __get_href_from_link_text(self, link_text, hard_fail=True):2222 href = self.get_link_attribute(link_text, "href", hard_fail)2223 if not href:2224 return None2225 if href.startswith('//'):2226 link = "http:" + href2227 elif href.startswith('/'):2228 url = self.driver.current_url2229 domain_url = self.get_domain_url(url)2230 link = domain_url + href2231 else:2232 link = href2233 return link2234 def __click_dropdown_link_text(self, link_text, link_css):2235 """ When a link may be hidden under a dropdown menu, use this. """2236 source = self.get_page_source()2237 soup = BeautifulSoup(source, "html.parser")2238 drop_down_list = soup.select('[class*=dropdown]')2239 csstype = link_css.split('[')[1].split('=')[0]2240 for item in drop_down_list:2241 if link_text in item.text.split('\n') and csstype in item.decode():2242 dropdown_css = ""2243 for css_class in item['class']:2244 dropdown_css += '.'2245 dropdown_css += css_class2246 dropdown_css = item.name + dropdown_css2247 matching_dropdowns = self.find_visible_elements(dropdown_css)2248 for dropdown in matching_dropdowns:2249 # The same class names might be used for multiple dropdowns2250 try:2251 page_actions.hover_element_and_click(2252 self.driver, dropdown, link_text,2253 click_by=By.LINK_TEXT, timeout=0.1)2254 return True2255 except Exception:2256 pass2257 return False2258 def __recalculate_selector(self, selector, by):2259 # Try to determine the type of selector automatically2260 if page_utils.is_xpath_selector(selector):2261 by = By.XPATH2262 if page_utils.is_link_text_selector(selector):2263 selector = page_utils.get_link_text_from_selector(selector)2264 by = By.LINK_TEXT2265 return (selector, by)2266 def __make_css_match_first_element_only(self, selector):2267 # Only get the first match2268 return page_utils.make_css_match_first_element_only(selector)2269 def __demo_mode_pause_if_active(self, tiny=False):2270 if self.demo_mode:2271 if self.demo_sleep:2272 wait_time = float(self.demo_sleep)2273 else:2274 wait_time = settings.DEFAULT_DEMO_MODE_TIMEOUT2275 if not tiny:2276 time.sleep(wait_time)2277 else:2278 time.sleep(wait_time / 3.4)2279 def __demo_mode_scroll_if_active(self, selector, by):2280 if self.demo_mode:2281 self.slow_scroll_to(selector, by=by)2282 def __demo_mode_highlight_if_active(self, selector, by):2283 if self.demo_mode:2284 # Includes self.slow_scroll_to(selector, by=by) by default2285 self.highlight(selector, by=by)2286 def __scroll_to_element(self, element):2287 js_utils.scroll_to_element(self.driver, element)2288 self.__demo_mode_pause_if_active(tiny=True)2289 def __slow_scroll_to_element(self, element):2290 js_utils.slow_scroll_to_element(self.driver, element, self.browser)2291 def __post_messenger_success_message(self, message, duration=None):2292 js_utils.post_messenger_success_message(2293 self.driver, message, self.message_duration, duration=duration)2294 def __post_messenger_error_message(self, message, duration=None):2295 js_utils.post_messenger_error_message(2296 self.driver, message, self.message_duration, duration=duration)2297 def __highlight_with_assert_success(2298 self, message, selector, by=By.CSS_SELECTOR):2299 selector, by = self.__recalculate_selector(selector, by)2300 element = self.find_element(2301 selector, by=by, timeout=settings.SMALL_TIMEOUT)2302 try:2303 selector = self.convert_to_css_selector(selector, by=by)2304 except Exception:2305 # Don't highlight if can't convert to CSS_SELECTOR2306 return2307 o_bs = '' # original_box_shadow2308 style = element.get_attribute('style')2309 if style:2310 if 'box-shadow: ' in style:2311 box_start = style.find('box-shadow: ')2312 box_end = style.find(';', box_start) + 12313 original_box_shadow = style[box_start:box_end]2314 o_bs = original_box_shadow2315 if ":contains" not in selector and ":first" not in selector:2316 selector = re.escape(selector)2317 selector = self.__escape_quotes_if_needed(selector)2318 self.__highlight_with_js_2(message, selector, o_bs)2319 else:2320 selector = self.__make_css_match_first_element_only(selector)2321 selector = re.escape(selector)2322 selector = self.__escape_quotes_if_needed(selector)2323 try:2324 self.__highlight_with_jquery_2(message, selector, o_bs)2325 except Exception:2326 pass # JQuery probably couldn't load. Skip highlighting.2327 time.sleep(0.065)2328 def __highlight_with_js_2(self, message, selector, o_bs):2329 js_utils.highlight_with_js_2(2330 self.driver, message, selector, o_bs, self.message_duration)2331 def __highlight_with_jquery_2(self, message, selector, o_bs):2332 js_utils.highlight_with_jquery_2(2333 self.driver, message, selector, o_bs, self.message_duration)2334 ############...

Full Screen

Full Screen

js_utils.py

Source:js_utils.py Github

copy

Full Screen

...257 test = getattr(config, "_webdriver_test")258 scroll_time_ms = 550 + time_offset259 sleep_time = 0.625 + (float(time_offset) / 1000.0)260 selector = self.convert_to_css_selector(selector, by=by)261 selector = self.__make_css_match_first_element_only(selector)262 scroll_script = (263 """jQuery([document.documentElement, document.body]).animate({"""264 """scrollTop: jQuery('%s').offset().top - 130}, %s);"""265 % (selector, scroll_time_ms)266 )267 if is_jquery_activated(driver):268 test.execute_script(scroll_script)269 else:270 slow_scroll_to_element(element)271 test.sleep(sleep_time)272@validate_arguments273def jquery_click(274 driver: WebDriver, how: SeleniumBy, selector: str = Field(default="", strict=True, min_length=1)275) -> None:276 """Clicks an element using jQuery. Different from using pure JS."""277 from .element_actions import wait_for_element_interactable278 wait_for_element_interactable(how, selector, timeout=constants.SMALL_TIMEOUT)279 selector = self.convert_to_css_selector(selector, by=by)280 selector = self.__make_css_match_first_element_only(selector)281 click_script = """jQuery('%s')[0].click();""" % selector282 safe_execute_script(driver, click_script)283def safe_execute_script(driver: WebDriver, script, *args) -> Any:284 """285 When executing a script that contains a jQuery command,286 it's important that the jQuery library has been loaded first.287 This method will load jQuery if it wasn't already loaded.288 """289 WebDriverValidator.validate(driver)290 if not is_jquery_activated(driver):291 activate_jquery()292 return driver.execute_script(script, *args)293def slow_scroll_to_element(element: WebElement) -> None:294 element = WebElementValidator.validate(element)...

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