Best Python code snippet using robotframework-appiumlibrary_python
elementfinder.py
Source:elementfinder.py
...31 # Strategy routines, private32 def _find_by_identifier(self, browser, criteria, tag, constraints):33 elements = self._normalize_result(browser.find_elements_by_id(criteria))34 elements.extend(self._normalize_result(browser.find_elements_by_name(criteria)))35 return self._filter_elements(elements, tag, constraints)36 def _find_by_id(self, browser, criteria, tag, constraints):37 return self._filter_elements(38 browser.find_elements_by_id(criteria),39 tag, constraints)40 def _find_by_name(self, browser, criteria, tag, constraints):41 return self._filter_elements(42 browser.find_elements_by_name(criteria),43 tag, constraints)44 def _find_by_xpath(self, browser, criteria, tag, constraints):45 return self._filter_elements(46 browser.find_elements_by_xpath(criteria),47 tag, constraints)48 def _find_by_dom(self, browser, criteria, tag, constraints):49 result = browser.execute_script("return %s;" % criteria)50 if result is None:51 return []52 if not isinstance(result, list):53 result = [result]54 return self._filter_elements(result, tag, constraints)55 def _find_by_sizzle_selector(self, browser, criteria, tag, constraints):56 js = "return jQuery('%s').get();" % criteria.replace("'", "\\'")57 return self._filter_elements(58 browser.execute_script(js),59 tag, constraints)60 def _find_by_link_text(self, browser, criteria, tag, constraints):61 return self._filter_elements(62 browser.find_elements_by_link_text(criteria),63 tag, constraints)64 def _find_by_css_selector(self, browser, criteria, tag, constraints):65 return self._filter_elements(66 browser.find_elements_by_css_selector(criteria),67 tag, constraints)68 def _find_by_tag_name(self, browser, criteria, tag, constraints):69 return self._filter_elements(70 browser.find_elements_by_tag_name(criteria),71 tag, constraints)72 def _find_by_class_name(self, browser, criteria, tag, constraints):73 return self._filter_elements(74 browser.find_elements_by_class_name(criteria),75 tag, constraints)76 def _find_element_by_accessibility_id(self, browser, criteria, tag, constraints):77 elements = browser.find_elements_by_accessibility_id(criteria)78 return elements79 def _find_by_android(self, browser, criteria, tag, constraints):80 """Find element matches by UI Automator."""81 return self._filter_elements(82 browser.find_elements_by_android_uiautomator(criteria),83 tag, constraints)84 def _find_by_ios(self, browser, criteria, tag, constraints):85 """Find element matches by UI Automation."""86 return self._filter_elements(87 browser.find_elements_by_ios_uiautomation(criteria),88 tag, constraints)89 def _find_by_nsp(self, browser, criteria, tag, constraints):90 """Find element matches by iOSNsPredicateString."""91 return self._filter_elements(92 browser.find_elements_by_ios_predicate(criteria),93 tag, constraints)94 def _find_by_chain(self, browser, criteria, tag, constraints):95 """Find element matches by iOSChainString."""96 return self._filter_elements(97 browser.find_elements_by_ios_class_chain(criteria),98 tag, constraints)99 def _find_by_default(self, browser, criteria, tag, constraints):100 if criteria.startswith('//'):101 return self._find_by_xpath(browser, criteria, tag, constraints)102 # Used `id` instead of _find_by_key_attrs since iOS and Android internal `id` alternatives are103 # different and inside appium python client. Need to expose these and improve in order to make104 # _find_by_key_attrs useful.105 return self._find_by_id(browser, criteria, tag, constraints)106 # TODO: Not in use after conversion from Selenium2Library need to make more use of multiple auto selector strategy107 def _find_by_key_attrs(self, browser, criteria, tag, constraints):108 key_attrs = self._key_attrs.get(None)109 if tag is not None:110 key_attrs = self._key_attrs.get(tag, key_attrs)111 xpath_criteria = utils.escape_xpath_value(criteria)112 xpath_tag = tag if tag is not None else '*'113 xpath_constraints = ["@%s='%s'" % (name, constraints[name]) for name in constraints]114 xpath_searchers = ["%s=%s" % (attr, xpath_criteria) for attr in key_attrs]115 xpath_searchers.extend(116 self._get_attrs_with_url(key_attrs, criteria, browser))117 xpath = "//%s[%s(%s)]" % (118 xpath_tag,119 ' and '.join(xpath_constraints) + ' and ' if len(xpath_constraints) > 0 else '',120 ' or '.join(xpath_searchers))121 return self._normalize_result(browser.find_elements_by_xpath(xpath))122 # Private123 _key_attrs = {124 None: ['@id', '@name'],125 'a': ['@id', '@name', '@href', 'normalize-space(descendant-or-self::text())'],126 'img': ['@id', '@name', '@src', '@alt'],127 'input': ['@id', '@name', '@value', '@src'],128 'button': ['@id', '@name', '@value', 'normalize-space(descendant-or-self::text())']129 }130 def _get_tag_and_constraints(self, tag):131 if tag is None:132 return None, {}133 tag = tag.lower()134 constraints = {}135 if tag == 'link':136 tag = 'a'137 elif tag == 'image':138 tag = 'img'139 elif tag == 'list':140 tag = 'select'141 elif tag == 'radio button':142 tag = 'input'143 constraints['type'] = 'radio'144 elif tag == 'checkbox':145 tag = 'input'146 constraints['type'] = 'checkbox'147 elif tag == 'text field':148 tag = 'input'149 constraints['type'] = 'text'150 elif tag == 'file upload':151 tag = 'input'152 constraints['type'] = 'file'153 return tag, constraints154 def _element_matches(self, element, tag, constraints):155 if not element.tag_name.lower() == tag:156 return False157 for name in constraints:158 if not element.get_attribute(name) == constraints[name]:159 return False160 return True161 def _filter_elements(self, elements, tag, constraints):162 elements = self._normalize_result(elements)163 if tag is None:164 return elements165 return filter(166 lambda element: self._element_matches(element, tag, constraints),167 elements)168 def _get_attrs_with_url(self, key_attrs, criteria, browser):169 attrs = []170 url = None171 xpath_url = None172 for attr in ['@src', '@href']:173 if attr in key_attrs:174 if url is None or xpath_url is None:175 url = self._get_base_url(browser) + "/" + criteria...
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!!