|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import unittest |
| 17 | + |
| 18 | +import pytest |
| 19 | +from selenium.common.exceptions import WebDriverException |
| 20 | + |
| 21 | +from appium import webdriver |
| 22 | +from appium.webdriver.common.mobileby import MobileBy |
| 23 | +from appium.webdriver.extensions.search_context.android import ( |
| 24 | + AndroidSearchContext |
| 25 | +) |
| 26 | +from test.functional.android.helper.test_helper import ( |
| 27 | + desired_capabilities, |
| 28 | + is_ci |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +class FindByViewMatcherTests(unittest.TestCase): |
| 33 | + |
| 34 | + def setUp(self): |
| 35 | + desired_caps = desired_capabilities.get_desired_capabilities('ApiDemos-debug.apk.zip') |
| 36 | + desired_caps['automationName'] = 'Espresso' |
| 37 | + self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) |
| 38 | + |
| 39 | + def tearDown(self): |
| 40 | + if is_ci(): |
| 41 | + # Take the screenshot to investigate when tests failed only on CI |
| 42 | + img_path = os.path.join(os.getcwd(), self._testMethodName + '.png') |
| 43 | + self.driver.get_screenshot_as_file(img_path) |
| 44 | + self.driver.quit() |
| 45 | + |
| 46 | + def test_find_single_element(self): |
| 47 | + el = self.driver.find_element_by_android_view_matcher( |
| 48 | + name='withText', args=['Accessibility'], className='ViewMatchers') |
| 49 | + assert el.text == 'Accessibility' |
| 50 | + |
| 51 | + def test_find_single_element_ful_class_name(self): |
| 52 | + el = self.driver.find_element_by_android_view_matcher( |
| 53 | + name='withText', args=['Accessibility'], className='androidx.test.espresso.matcher.ViewMatchers') |
| 54 | + assert el.text == 'Accessibility' |
| 55 | + |
| 56 | + def test_find_single_element_using_hamcrest_matcher(self): |
| 57 | + el = self.driver.find_element_by_android_view_matcher( |
| 58 | + name='withText', |
| 59 | + args={ |
| 60 | + 'name': 'containsString', |
| 61 | + 'args': 'Animati', |
| 62 | + 'class': 'org.hamcrest.Matchers'}, |
| 63 | + className='ViewMatchers') |
| 64 | + assert el.text == 'Animation' |
| 65 | + |
| 66 | + # androidx.test.espresso.AmbiguousViewMatcherException: |
| 67 | + # 'with text: a string containing "Access"' matches multiple views in the hierarchy. |
| 68 | + def test_find_multiple_elements(self): |
| 69 | + value = AndroidSearchContext()._build_data_matcher( |
| 70 | + name='withSubstring', args=['Access'], className='ViewMatchers') |
| 71 | + with pytest.raises(WebDriverException): |
| 72 | + self.driver.find_elements(by=MobileBy.ANDROID_VIEW_MATCHER, value=value) |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == '__main__': |
| 76 | + suite = unittest.TestLoader().loadTestsFromTestCase(FindByViewMatcherTests) |
| 77 | + unittest.TextTestRunner(verbosity=2).run(suite) |
0 commit comments