How to use wait_for_attribute method in SeleniumBase

Best Python code snippet using SeleniumBase

CommonSteps.py

Source:CommonSteps.py Github

copy

Full Screen

...161@step ("user waits [{element_name}] appear for '{timeout}' seconds")162def step_impl(context, element_name, timeout):163 element = world.find_element(element_name)164 element.wait_timeout = int(timeout)165 if not element.wait_for_attribute('visible', True):166 log.failed("Element '%s' not appear in %s seconds" % (element_name, timeout))167@step ("user waits [{element_name}] disappear for '{timeout}' seconds")168def step_impl(context, element_name, timeout):169 element = world.find_element(element_name)170 element.wait_timeout = int(timeout)171 if not element.wait_for_attribute('visible', False):172 log.failed("Element '%s' not disappear in %s seconds" % (element_name, timeout))173@step ("user scrolls to [{element_name}]")174def step_impl(context, element_name):175 element = world.find_element(element_name)176 element.send_keys(' ')177@step ("user saves current page to file '{value}'")178def step_impl(context, value):179 value = world.replace_variables(value)180 if not value[:-5].lower() == '.html':181 value = value + '.html'182 folder = os.path.dirname(value)183 if not folder == '':184 if not os.path.exists(folder):185 os.makedirs(folder)186 filename = open(value + '.html','w')187 page_source = world.get_current_page_source()188 filename.write(page_source.encode('utf8'))189 filename.close()190@step ("user saves screenshot to file '{value}'")191def step_impl(context, value):192 value = world.replace_variables(value)193 if not value[:-4].lower() == '.png':194 value = value + '.png'195 folder = os.path.dirname(value)196 if not folder == '':197 if not os.path.exists(folder):198 os.makedirs(folder)199 if not world.save_current_page_screenshot(value):200 log.failed("Could not save a screenshot.")201"""202Then step definitions203"""204@step ("the browser shows [{page_name}/{path_name}] page") ##->then205def step_impl(context, page_name, path_name):206 page = world.find_page(page_name)207 page.wait_for_loading(path_name)208@step ("the browser shows [{page_name}] page") ##->then209@step ("the system displays [{page_name}]")210def step_impl(context, page_name):211 page = world.find_page(page_name)212 page.wait_for_loading()213@step ("the page url is '{url}'") ##->then214@step ("the system url is '{url}'")215@step ("the system displays '{url}'")216def step_impl(context, url):217 url = world.replace_variables(url)218 page = Page(url=url)219 page.driver = world.driver220 page.wait_for_loading()221 if url == world.driver.current_url:222 return True223 else:224 log.failed("Verify url is?", world.driver.current_url, url)225@step ("the page url contains '{url}'") ##->then226@step ("the system url contains '{url}'")227def step_impl(context, url):228 url = world.replace_variables(url)229 page = Page(url=url)230 page.driver = world.driver231 page.wait_for_loading()232 if url in world.driver.current_url:233 return True234 else:235 log.failed("Verify url contains?", world.driver.current_url, url)236@step ("the [{element_name}] value is '{value}'") ##->then237@step ("the [{element_name}] shows '{value}'")238def step_impl(context, element_name, value):239 value = world.replace_variables(value)240 element = world.find_element(element_name)241 if element.value == value:242 return True243 else:244 log.failed("Verify value is?", element.value, value)245@step ("the [{element_name}] value is") ##->then246def step_impl(context, element_name):247 element = world.find_element(element_name)248 if element.value == context.text:249 return True250 else:251 log.failed("Verify value is? (multi-line)", element.value, context.text)252@step ("the [{element_name}] value is not '{value}'") ##->then253def step_impl(context, element_name, value):254 value = world.replace_variables(value)255 element = world.find_element(element_name)256 if element.value != value:257 return True258 else:259 log.failed("Verify value is not?", element.value, value)260@step ("the [{element_name}] value contains '{value}'") ##->then261@step ("the [{element_name}] contains '{value}'")262def step_impl(context, element_name, value):263 value = world.replace_variables(value)264 element = world.find_element(element_name)265 if value in element.value:266 return True267 else:268 log.failed("Verify value contains?", element.value, value)269@step ("the [{element_name}] value contains") ##->then270def step_impl(context, element_name):271 element = world.find_element(element_name)272 if context.text in element.value:273 return True274 else:275 log.failed("Verify value contains? (multi-line)", element.value, value)276@step ("the [{element_name}] value matches pattern '{pattern}'")277def step_impl(context, element_name, pattern):278 element = world.find_element(element_name)279 if re.match(pattern, element.value):280 return True281 else:282 log.failed("Verify value match with pattern?", element.value, pattern)283@step ("the [{element_name}] value is more than '{value}'") ##->then284@step ("the [{element_name}] more than '{value}'")285def step_impl(context, element_name, value):286 value = world.replace_variables(value)287 element = world.find_element(element_name)288 if element.value |more_than| value:289 return True290 else:291 log.failed(292 "Verify value is more than?",293 "value = %s" % element.value,294 "value > %s" % value295 )296@step ("the [{element_name}] value is more than or equal '{value}'") ##->then297@step ("the [{element_name}] more than or equal '{value}'")298def step_impl(context, element_name, value):299 value = world.replace_variables(value)300 element = world.find_element(element_name)301 if element.value |more_than_or_equal| value:302 return True303 else:304 log.failed(305 "Verify value is more than or equal?",306 "value = %s" % element.value,307 "value >= %s" % value308 )309@step ("the [{element_name}] value is less than '{value}'") ##->then310@step ("the [{element_name}] less than '{value}'")311def step_impl(context, element_name, value):312 value = world.replace_variables(value)313 element = world.find_element(element_name)314 if element.value |less_than| value:315 return True316 else:317 log.failed(318 "Verify value is less than?",319 "value = %s" % element.value,320 "value < %s" % value321 )322@step ("the [{element_name}] value is less than or equal '{value}'") ##->then323@step ("the [{element_name}] less than or equal '{value}'")324def step_impl(context, element_name, value):325 value = world.replace_variables(value)326 element = world.find_element(element_name)327 if element.value |less_than_or_equal| value:328 return True329 else:330 log.failed(331 "Verify value is less than or equal?",332 "value = %s" % element.value,333 "value <= %s" % value334 )335@step ("the [{element_name}] value is between '{value1}' and '{value2}'") ##->then336def step_impl(context, element_name, value1, value2):337 element = world.find_element(element_name)338 if (element.value |more_than_or_equal| value1) and (element.value |less_than_or_equal| value2):339 return True340 elif (element.value |less_than_or_equal| value1) and (element.value |more_than_or_equal| value2):341 return True342 else:343 log.failed(344 "Verify value is between?",345 "value = %s" % element.value,346 "value is between %s and %s" % (value1, value2)347 )348@step ("the [{element_name}] value is empty") ##->then349@step ("the [{element_name}] is blank")350def step_impl(context, element_name):351 element = world.find_element(element_name)352 if element.value == "":353 return True354 else:355 log.failed("Verify value is empty?", element.value, "<blank>")356@step ("the [{element_name}] value is not empty") ##->then357@step ("the [{element_name}] has a value")358def step_impl(context, element_name):359 element = world.find_element(element_name)360 if element.value != "":361 return True362 log.failed("Verify value is not empty?", element.value, "<any value>")363@step ("the [{element_name1}] value is equal to [{element_name2}] value") ##->then364def step_impl(context, element_name1, element_name2):365 element1 = world.find_element(element_name1)366 element2 = world.find_element(element_name2)367 if element1.value == element2.value:368 return True369 else:370 log.failed(371 "Verify element1 value is equal to element2 value?",372 "element1 value = %s , element2 value = %s" % (element1.value, element2.value),373 "element1 value = element2 value"374 )375@step ("the [{element_name1}] value is more than [{element_name2}] value") ##->then376@step ("the [{element_name1}] value is greater than [{element_name2}] value")377def step_impl(context, element_name1, element_name2):378 element1 = world.find_element(element_name1)379 element2 = world.find_element(element_name2)380 if element1.value |more_than| element2.value:381 return True382 else:383 log.failed(384 "Verify element1 value is greater than element2 value?",385 "element1 value = %s , element2 value = %s" % (element1.value, element2.value),386 "element1 value > element2 value"387 )388@step ("the [{element_name1}] value is more than or equal to [{element_name2}] value") ##->then389@step ("the [{element_name1}] value is greater than or equal to [{element_name2}] value")390def step_impl(context, element_name1, element_name2):391 element1 = world.find_element(element_name1)392 element2 = world.find_element(element_name2)393 if element1.value |more_than_or_equal| element2.value:394 return True395 else:396 log.failed(397 "Verify element1 value is greater than or equal to element2 value?",398 "element1 value = %s , element2 value = %s" % (element1.value, element2.value),399 "element1 value >= element2 value"400 )401@step ("the [{element_name1}] value is less than [{element_name2}] value") ##->then402def step_impl(context, element_name1, element_name2):403 element1 = world.find_element(element_name1)404 element2 = world.find_element(element_name2)405 if element1.value |less_than| element2.value:406 return True407 else:408 log.failed(409 "Verify element1 value is less than element2 value?",410 "element1 value = %s , element2 value = %s" % (element1.value, element2.value),411 "element1 value < element2 value"412 )413@step ("the [{element_name1}] value is less than or equal to [{element_name2}] value") ##->then414def step_impl(context, element_name1, element_name2):415 element1 = world.find_element(element_name1)416 element2 = world.find_element(element_name2)417 if element1.value |less_than_or_equal| element2.value:418 return True419 else:420 log.failed(421 "Verify element1 value is less than or equal to element2 value?",422 "element1 value = %s , element2 value = %s" % (element1.value, element2.value),423 "element1 value <= element2 value"424 )425@step ("the [{element_name}] exists") ##->then426def step_impl(context, element_name):427 element = world.find_element(element_name)428 if element.wait_for_exist():429 return True430 else:431 log.failed("Verify element '%s' exists?" % element.name, "not exists", "exists")432@step ("the [{element_name}] does not exist") ##->then433def step_impl(context, element_name):434 element = world.find_element(element_name)435 if element.wait_for_not_exist():436 return True437 else:438 log.failed("Verify element '%s' exists?" % element.name, "exists", "not exists")439@step ("the [{element_name}] is visible") ##->then440def step_impl(context, element_name):441 element = world.find_element(element_name)442 if element.wait_for_attribute('visible', True):443 return True444 else:445 log.failed("Verify element '%s' visible?" % element.name, "not visible", "visible")446@step ("the [{element_name}] is invisible") ##->then447def step_impl(context, element_name):448 element = world.find_element(element_name)449 if element.wait_for_not_exist():450 return True451 if element.wait_for_attribute('visible', False):452 return True453 else:454 log.failed("Verify element '%s' not visible?" % element.name, "visible", "not visible")455@step ("the [{element_name}] is enabled") ##->then456def step_impl(context, element_name):457 element = world.find_element(element_name)458 if element.wait_for_attribute('enabled', True):459 return True460 else:461 log.failed("Verify element '%s' enabled?" % element.name, "disabled", "enabled")462@step ("the [{element_name}] is disabled") ##->then463def step_impl(context, element_name):464 element = world.find_element(element_name)465 if element.wait_for_attribute('enabled', False):466 return True467 else:468 log.failed("Verify element '%s' disabled?" % element.name, "enabled", "disabled")469@step ("the [{element_name}] is selected") ##->then470@step ("the [{element_name}] is checked") ##->then471def step_impl(context, element_name):472 element = world.find_element(element_name)473 if element.wait_for_attribute('selected', True):474 return True475 else:476 log.failed("Verify element '%s' checked?" % element.name, "unchecked", "checked")477@step ("the [{element_name}] is not selected") ##->then478@step ("the [{element_name}] is unchecked") ##->then479def step_impl(context, element_name):480 element = world.find_element(element_name)481 if element.wait_for_attribute('selected', False):482 return True483 else:484 log.failed("Verify element '%s' unchecked?" % element.name, "checked", "unchecked")485@step ("the popup message shows '{value}'") ##->then486@step ("the popup shows '{value}'")487def step_impl(context, value):488 value = world.replace_variables(value)489 alert = world.get_alert_when_exist()490 if alert is None:491 log.failed("The popup alert not visible")492 if alert.text == value:493 return True494 else:495 log.failed("Verify popup message is?", alert.text, value)...

Full Screen

Full Screen

Element.py

Source:Element.py Github

copy

Full Screen

...50 element = self.get_instance()51 if element is None: return True52 time.sleep(0.5)53 return False54 def wait_for_attribute(self, attr, expected):55 for n in range(0, self.wait_timeout * 2):56 element = self.get_instance()57 if element is not None:58 if attr == 'enabled' and element.is_enabled() == expected:59 return True60 elif attr == 'visible' and element.is_displayed() == expected:61 return True62 elif attr == 'selected' and element.is_selected() == expected:63 return True64 elif element.get_attribute(attr) == expected:65 return True66 time.sleep(0.5)67 return False68 @property69 def count(self):70 return len(self.get_instances())71 @property72 def title(self):73 element = self.get_instance_on_exist()74 return element.get_attribute('title')75 @property76 def value(self):77 element = self.get_instance_on_exist()78 if element is None: return ""79 if element.tag_name in ['input', 'button', 'textarea']:80 return element.get_attribute('value')81 elif element.tag_name == 'select':82 return Select(element).first_selected_option.get_attribute('innerHTML')83 else:84 return element.get_attribute('innerHTML')85 @property86 def exists(self):87 element = self.get_instance_on_exist()88 if element is None:89 return False90 else:91 return True92 @property93 def enabled(self):94 element = self.get_instance_on_exist()95 if element is None:96 return False97 else:98 return element.is_enabled()99 @property100 def visible(self):101 element = self.get_instance_on_exist()102 if element is None:103 return False104 else:105 return element.is_displayed()106 @property107 def selected(self):108 element = self.get_instance_on_exist()109 if element is None:110 return False111 else:112 return element.is_selected()113 def get_attribute(self, attr):114 element = self.get_instance_on_exist()115 if element is None:116 return ""117 else:118 return element.get_attribute(attr)119 def send_keys(self, value):120 if not self.wait_for_exist():121 return False122 if not self.wait_for_attribute('visible', True):123 return False124 if not self.wait_for_attribute('enabled', True):125 return False126 element = self.get_instance()127 element.send_keys(value)128 return True129 def send_keys_by_script(self, value):130 element = self.get_instance_on_exist()131 if element is None:132 return False133 script = "arguments[0].value = '" + value + "'"134 self.driver.execute_script(script, element)135 return True136 def click(self):137 if not self.wait_for_exist():138 return False139 if not self.wait_for_attribute('visible', True):140 return False141 if not self.wait_for_attribute('enabled', True):142 return False143 element = self.get_instance()144 element.click()145 return True146 def click_by_script(self):147 if not self.wait_for_exist():148 return False149 if not self.wait_for_attribute('visible', True):150 return False151 if not self.wait_for_attribute('enabled', True):152 return False153 element = self.get_instance()154 script = "document.evaluate('%s', document, null, 9, null).singleNodeValue.click();" % self.identifier155 self.driver.execute_script(script, element)156 return True157 def select(self, value):158 if not self.wait_for_exist():159 return False160 if not self.wait_for_attribute('visible', True):161 return False162 if not self.wait_for_attribute('enabled', True):163 return False164 element = Select(self.get_instance())165 element.select_by_visible_text(value)166 return True167 def check(self):168 if not self.selected: self.click()169 return True170 def uncheck(self):171 if self.selected: self.click()172 return True173 def move_mouse_over(self):174 element = self.get_instance_on_exist()175 action = ActionChains(self.driver).move_to_element(element)176 action.perform()...

Full Screen

Full Screen

main.py

Source:main.py Github

copy

Full Screen

...43@wait_for()44def wait_for_elem(selector):45 return browser.find_element(selector[0], selector[1])46@wait_for()47def wait_for_attribute(elem, attr, allow_none = False):48 attrVal = elem.get_attribute(attr)49 if not allow_none and attrVal is None:50 raise Exception('val can\'t be None.')51 return attrVal52def switch_frame(frameSelector):53 elem = wait_for_elem(frameSelector)54 browser.switch_to.frame(elem)55def get_slide_distance(slide, target):56 # slide_img = slide_libs.url2image(slide.get_attribute("src"))57 # target_img = slide_libs.url2image(target.get_attribute("src"))58 slide_img = slide_libs.url2image(wait_for_attribute(slide, "src"))59 target_img = slide_libs.url2image(wait_for_attribute(target, "src"))60 61 position = slide_libs.check_position(slide_img, target_img)62 t_size = target.size63 t_loc = target.location64 s_loc = slide.location65 # slide_libs.show(slide_img, target_img, position)66 67 return t_loc['x'] +t_size['width'] * position['x'] - s_loc['x']68# 将一个滑动距离 拆为 多个逐渐减小的 滑动距离69def fuck_distance(rest_dis, dis_list = None):70 if dis_list is None:71 dis_list = []72 73 this_dis = rest_dis / 2 + random.uniform(0, 30.0)...

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