Best Python code snippet using robotframework-appiumlibrary_python
elementfinder.py
Source:elementfinder.py
...3132 # Strategy routines, private3334 def _find_by_identifier(self, browser, criteria, tag, constraints):35 elements = self._normalize_result(browser.find_elements_by_id(criteria))36 elements.extend(self._normalize_result(browser.find_elements_by_name(criteria)))37 return self._filter_elements(elements, tag, constraints)3839 def _find_by_id(self, browser, criteria, tag, constraints):40 return self._filter_elements(41 browser.find_elements_by_id(criteria),42 tag, constraints)4344 def _find_by_name(self, browser, criteria, tag, constraints):45 return self._filter_elements(46 browser.find_elements_by_name(criteria),47 tag, constraints)4849 def _find_by_xpath(self, browser, criteria, tag, constraints):50 return self._filter_elements(51 browser.find_elements_by_xpath(criteria),52 tag, constraints)5354 def _find_by_dom(self, browser, criteria, tag, constraints):55 result = browser.execute_script("return %s;" % criteria)56 if result is None:57 return []58 if not isinstance(result, list):59 result = [result]60 return self._filter_elements(result, tag, constraints)6162 def _find_by_sizzle_selector(self, browser, criteria, tag, constraints):63 js = "return jQuery('%s').get();" % criteria.replace("'", "\\'")64 return self._filter_elements(65 browser.execute_script(js),66 tag, constraints)6768 def _find_by_link_text(self, browser, criteria, tag, constraints):69 return self._filter_elements(70 browser.find_elements_by_link_text(criteria),71 tag, constraints)7273 def _find_by_css_selector(self, browser, criteria, tag, constraints):74 return self._filter_elements(75 browser.find_elements_by_css_selector(criteria),76 tag, constraints)7778 def _find_by_tag_name(self, browser, criteria, tag, constraints):79 return self._filter_elements(80 browser.find_elements_by_tag_name(criteria),81 tag, constraints)8283 def _find_by_default(self, browser, criteria, tag, constraints):84 if criteria.startswith('//'):85 return self._find_by_xpath(browser, criteria, tag, constraints)86 return self._find_by_key_attrs(browser, criteria, tag, constraints)8788 def _find_by_key_attrs(self, browser, criteria, tag, constraints):89 key_attrs = self._key_attrs.get(None)90 if tag is not None:91 key_attrs = self._key_attrs.get(tag, key_attrs)9293 xpath_criteria = utils.escape_xpath_value(criteria)94 xpath_tag = tag if tag is not None else '*'95 xpath_constraints = ["@%s='%s'" % (name, constraints[name]) for name in constraints]96 xpath_searchers = ["%s=%s" % (attr, xpath_criteria) for attr in key_attrs]97 xpath_searchers.extend(98 self._get_attrs_with_url(key_attrs, criteria, browser))99 xpath = "//%s[%s(%s)]" % (100 xpath_tag,101 ' and '.join(xpath_constraints) + ' and ' if len(xpath_constraints) > 0 else '',102 ' or '.join(xpath_searchers))103104 return self._normalize_result(browser.find_elements_by_xpath(xpath))105106 # Private107108 _key_attrs = {109 None: ['@id', '@name'],110 'a': ['@id', '@name', '@href', 'normalize-space(descendant-or-self::text())'],111 'img': ['@id', '@name', '@src', '@alt'],112 'input': ['@id', '@name', '@value', '@src'],113 'button': ['@id', '@name', '@value', 'normalize-space(descendant-or-self::text())']114 }115116 def _get_tag_and_constraints(self, tag):117 if tag is None: return None, {}118119 tag = tag.lower()120 constraints = {}121 if tag == 'link':122 tag = 'a'123 elif tag == 'image':124 tag = 'img'125 elif tag == 'list':126 tag = 'select'127 elif tag == 'radio button':128 tag = 'input'129 constraints['type'] = 'radio'130 elif tag == 'checkbox':131 tag = 'input'132 constraints['type'] = 'checkbox'133 elif tag == 'text field':134 tag = 'input'135 constraints['type'] = 'text'136 elif tag == 'file upload':137 tag = 'input'138 constraints['type'] = 'file'139 elif tag == 'text area':140 tag = 'textarea'141 return tag, constraints142143 def _element_matches(self, element, tag, constraints):144 if not element.tag_name.lower() == tag:145 return False146 for name in constraints:147 if not element.get_attribute(name) == constraints[name]:148 return False149 return True150151 def _filter_elements(self, elements, tag, constraints):152 elements = self._normalize_result(elements)153 if tag is None: return elements154 return filter(155 lambda element: self._element_matches(element, tag, constraints),156 elements)157158 def _get_attrs_with_url(self, key_attrs, criteria, browser):159 attrs = []160 url = None161 xpath_url = None162 for attr in ['@src', '@href']:163 if attr in key_attrs:164 if url is None or xpath_url is None:165 url = self._get_base_url(browser) + "/" + criteria166 xpath_url = utils.escape_xpath_value(url)167 attrs.append("%s=%s" % (attr, xpath_url))168 return attrs169170 def _get_base_url(self, browser):171 url = browser.get_current_url()172 if '/' in url:173 url = '/'.join(url.split('/')[:-1])174 return url175176 def _parse_locator(self, locator):177 prefix = None178 criteria = locator179 if not locator.startswith('//'):180 locator_parts = locator.partition('=')181 if len(locator_parts[1]) > 0:182 prefix = locator_parts[0].strip().lower()183 criteria = locator_parts[2].strip()184 return (prefix, criteria)185186 def _normalize_result(self, elements):187 if not isinstance(elements, list):188 logger.debug("WebDriver find returned %s" % elements)189 return []
...
test.py
Source:test.py
...8 t = np.linspace(0, 1.8 * np.pi, n_points)9 x = (center[0] + width * np.cos(t) * np.cos(phi) - height * np.sin(t) * np.sin(phi))10 y = (center[1] + width * np.cos(t) * np.sin(phi) + height * np.sin(t) * np.cos(phi))11 return np.c_[x, y]12def _normalize_result(major, minor, phi):13 """Map parameters back to original orientation14 If phi > np.pi/2 then we are actually measuring a tall ellipse15 chaneg this to measure angle of the waist16 """17 if phi >= np.pi/2:18 width, height = minor, major19 phi -= np.pi/220 else:21 width, height = major, minor22 return width, height, phi23# phi needs to be < (1/4 * pi) and width != height or test is degenerate24@pytest.mark.parametrize('center', [[1, 1], [0, 1]])25@pytest.mark.parametrize('width', [.4, 10])26@pytest.mark.parametrize('height', [.2, 3])27@pytest.mark.parametrize('phi', [np.pi/5, np.pi/13])28def test_ellipse_fit(center, width, height, phi):29 X = make_dataset(30 center=center,31 width=width,32 height=height,33 phi=phi,34 n_points=1035 )36 elp = LsqEllipse()37 elp.fit(X)38 _center, _major, _minor, _phi = elp.as_parameters()39 _width, _height, _phi = _normalize_result(_major, _minor, _phi)40 nptest.assert_array_almost_equal(_center, center)41 nptest.assert_almost_equal(_width, width)42 nptest.assert_almost_equal(_height, height)43 nptest.assert_almost_equal(_phi, phi)44def test_minimum_data_points():45 X = make_dataset(46 center=[0, 0],47 width=1,48 height=.5,49 phi=np.pi/20,50 n_points=551 )52 elp = LsqEllipse()53 elp.fit(X)54 _center, _major, _minor, _phi = elp.as_parameters()55 _width, _height, _phi = _normalize_result(_major, _minor, _phi)56 nptest.assert_array_almost_equal(_center, [0, 0])57 nptest.assert_almost_equal(_width, 1)58 nptest.assert_almost_equal(_height, .5)59 nptest.assert_almost_equal(_phi, np.pi/20)60def test_less_than_minimum_data_points_raises_err():61 X = make_dataset(62 center=[0, 0],63 width=1,64 height=.5,65 phi=0,66 n_points=467 )68 elp = LsqEllipse()69 with pytest.raises(ValueError):70 elp.fit(X)71def test_cannot_get_coef_without_fitting():72 elp = LsqEllipse()73 with pytest.raises(ValueError):74 elp.coefficients75@pytest.mark.parametrize('n_points', [6, 100])76def test_return_fit_returns_correct_ellipse(n_points):77 X = make_dataset(78 center=[0, 0],79 width=1,80 height=.5,81 phi=0,82 n_points=n_points83 )84 elp = LsqEllipse().fit(X)85 t = np.linspace(0, 1.8 * np.pi, n_points)86 center, major, minor, phi = elp.as_parameters()87 with mock.patch.object(LsqEllipse, 'as_parameters') as as_parameters:88 as_parameters.return_value = (center, *_normalize_result(major, minor, phi))89 x = elp.return_fit(n_points, t=t)90 nptest.assert_array_almost_equal(x, X)91def test_if_perfect_circle():92 X = make_dataset(93 center=[0, 0],94 width=1,95 height=1,96 phi=0,97 n_points=5098 )99 elp = LsqEllipse().fit(X)100 _center, _width, _height, _phi = elp.as_parameters()101 nptest.assert_array_almost_equal(_center, [0, 0])102 nptest.assert_almost_equal(_width, 1)...
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!!