|
| 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 | +# pylint: disable=abstract-method |
| 16 | + |
| 17 | +import json |
| 18 | + |
| 19 | +from selenium import webdriver |
| 20 | +from selenium.webdriver.remote.webelement import WebElement as SeleniumWebElement |
| 21 | + |
| 22 | +from appium.webdriver.common.mobileby import MobileBy |
| 23 | + |
| 24 | + |
| 25 | +class BaseSearchContext(object): |
| 26 | + """Used by each search context. Dummy find_element/s are for preventing pylint error""" |
| 27 | + |
| 28 | + def find_element(self, by=None, value=None): |
| 29 | + raise NotImplementedError |
| 30 | + |
| 31 | + def find_elements(self, by=None, value=None): |
| 32 | + raise NotImplementedError |
| 33 | + |
| 34 | + |
| 35 | +class AndroidSearchContext(BaseSearchContext): |
| 36 | + """Define search context for Android""" |
| 37 | + |
| 38 | + def find_element_by_android_data_matcher(self, name=None, args=None, className=None): |
| 39 | + """Finds element by [onData](https://medium.com/androiddevelopers/adapterviews-and-espresso-f4172aa853cf) in Android |
| 40 | + It works with [Espresso Driver](https://github.com/appium/appium-espresso-driver). |
| 41 | +
|
| 42 | + :Args: |
| 43 | + - name - The name of a method to invoke. |
| 44 | + The method must return a Hamcrest |
| 45 | + [Matcher](http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matcher.html) |
| 46 | + - args - The args provided to the method |
| 47 | + - className - The class name that the method is part of (defaults to `org.hamcrest.Matchers`). |
| 48 | + Can be fully qualified, or simple, and simple defaults to `androidx.test.espresso.matcher` package |
| 49 | + (e.g.: `class=CursorMatchers` fully qualified is `class=androidx.test.espresso.matcher.CursorMatchers` |
| 50 | +
|
| 51 | + :Returns: |
| 52 | + An Element object |
| 53 | +
|
| 54 | + :Raises: |
| 55 | + - TypeError - Raises a TypeError if the arguments are not validated for JSON format |
| 56 | +
|
| 57 | + :Usage: |
| 58 | + driver.find_element_by_android_data_matcher(name='hasEntry', args=['title', 'Animation']) |
| 59 | + """ |
| 60 | + |
| 61 | + return self.find_element( |
| 62 | + by=MobileBy.ANDROID_DATA_MATCHER, |
| 63 | + value=self._build_data_matcher(name=name, args=args, className=className) |
| 64 | + ) |
| 65 | + |
| 66 | + def find_elements_by_android_data_matcher(self, name=None, args=None, className=None): |
| 67 | + """Finds elements by [onData](https://medium.com/androiddevelopers/adapterviews-and-espresso-f4172aa853cf) in Android |
| 68 | + It works with [Espresso Driver](https://github.com/appium/appium-espresso-driver). |
| 69 | +
|
| 70 | + :Args: |
| 71 | + - name - The name of a method to invoke. |
| 72 | + The method must return a Hamcrest |
| 73 | + [Matcher](http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matcher.html) |
| 74 | + - args - The args provided to the method |
| 75 | + - className - The class name that the method is part of (defaults to `org.hamcrest.Matchers`). |
| 76 | + Can be fully qualified, or simple, and simple defaults to `androidx.test.espresso.matcher` package |
| 77 | + (e.g.: `class=CursorMatchers` fully qualified is `class=androidx.test.espresso.matcher.CursorMatchers` |
| 78 | +
|
| 79 | + :Usage: |
| 80 | + driver.find_elements_by_android_data_matcher(name='hasEntry', args=['title', 'Animation']) |
| 81 | + """ |
| 82 | + |
| 83 | + return self.find_elements( |
| 84 | + by=MobileBy.ANDROID_DATA_MATCHER, |
| 85 | + value=self._build_data_matcher(name=name, args=args, className=className) |
| 86 | + ) |
| 87 | + |
| 88 | + def _build_data_matcher(self, name=None, args=None, className=None): |
| 89 | + result = {} |
| 90 | + |
| 91 | + for key, value in {'name': name, 'args': args, 'class': className}.items(): |
| 92 | + if value is not None: |
| 93 | + result[key] = value |
| 94 | + |
| 95 | + return json.dumps(result) |
| 96 | + |
| 97 | + |
| 98 | +class AppiumSearchContext(webdriver.Remote, |
| 99 | + AndroidSearchContext): |
| 100 | + """Returns appium driver search conext""" |
| 101 | + |
| 102 | + |
| 103 | +class AppiumWebElementSearchContext(SeleniumWebElement, |
| 104 | + AndroidSearchContext): |
| 105 | + """Returns appium web element search context""" |
0 commit comments