-
Notifications
You must be signed in to change notification settings - Fork 572
add datamatcher #335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add datamatcher #335
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7143e8a
add datamatcher
KazuCocoa 1e9b714
add zero case
KazuCocoa 7c157dd
add docstring for find elements
KazuCocoa 8713e1b
tweak test cases, append docstring
KazuCocoa ac8b935
define _build_data_matcher
KazuCocoa c334157
define search_context, add them into webelement
KazuCocoa 9399635
add tests for webelements
KazuCocoa a1ef34c
defines seach context for driver and element
KazuCocoa 38d3f1b
use items in for
KazuCocoa 21ff87c
define android, ios, windows and tizen search context as parents
KazuCocoa cd3df92
change message option for pylint
KazuCocoa 139c9a6
rename class name
KazuCocoa 6f23f89
tweak search context
KazuCocoa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#!/usr/bin/env python | ||
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# pylint: disable=abstract-method | ||
|
||
import json | ||
|
||
from selenium import webdriver | ||
from selenium.webdriver.remote.webelement import WebElement as SeleniumWebElement | ||
|
||
from appium.webdriver.common.mobileby import MobileBy | ||
|
||
|
||
class BaseSearchContext(object): | ||
"""Used by each search context. Dummy find_element/s are for preventing pylint error""" | ||
|
||
def find_element(self, by=None, value=None): | ||
raise NotImplementedError | ||
|
||
def find_elements(self, by=None, value=None): | ||
raise NotImplementedError | ||
|
||
|
||
class AndroidSearchContext(BaseSearchContext): | ||
"""Define search context for Android""" | ||
|
||
def find_element_by_android_data_matcher(self, name=None, args=None, className=None): | ||
"""Finds element by [onData](https://medium.com/androiddevelopers/adapterviews-and-espresso-f4172aa853cf) in Android | ||
It works with [Espresso Driver](https://github.com/appium/appium-espresso-driver). | ||
|
||
:Args: | ||
- name - The name of a method to invoke. | ||
The method must return a Hamcrest | ||
[Matcher](http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matcher.html) | ||
- args - The args provided to the method | ||
- className - The class name that the method is part of (defaults to `org.hamcrest.Matchers`). | ||
Can be fully qualified, or simple, and simple defaults to `androidx.test.espresso.matcher` package | ||
(e.g.: `class=CursorMatchers` fully qualified is `class=androidx.test.espresso.matcher.CursorMatchers` | ||
|
||
:Returns: | ||
An Element object | ||
|
||
:Raises: | ||
- TypeError - Raises a TypeError if the arguments are not validated for JSON format | ||
|
||
:Usage: | ||
driver.find_element_by_android_data_matcher(name='hasEntry', args=['title', 'Animation']) | ||
""" | ||
|
||
return self.find_element( | ||
by=MobileBy.ANDROID_DATA_MATCHER, | ||
value=self._build_data_matcher(name=name, args=args, className=className) | ||
) | ||
|
||
def find_elements_by_android_data_matcher(self, name=None, args=None, className=None): | ||
"""Finds elements by [onData](https://medium.com/androiddevelopers/adapterviews-and-espresso-f4172aa853cf) in Android | ||
It works with [Espresso Driver](https://github.com/appium/appium-espresso-driver). | ||
|
||
:Args: | ||
- name - The name of a method to invoke. | ||
The method must return a Hamcrest | ||
[Matcher](http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matcher.html) | ||
- args - The args provided to the method | ||
- className - The class name that the method is part of (defaults to `org.hamcrest.Matchers`). | ||
Can be fully qualified, or simple, and simple defaults to `androidx.test.espresso.matcher` package | ||
(e.g.: `class=CursorMatchers` fully qualified is `class=androidx.test.espresso.matcher.CursorMatchers` | ||
|
||
:Usage: | ||
driver.find_elements_by_android_data_matcher(name='hasEntry', args=['title', 'Animation']) | ||
""" | ||
|
||
return self.find_elements( | ||
by=MobileBy.ANDROID_DATA_MATCHER, | ||
value=self._build_data_matcher(name=name, args=args, className=className) | ||
) | ||
|
||
def _build_data_matcher(self, name=None, args=None, className=None): | ||
result = {} | ||
|
||
for key, value in {'name': name, 'args': args, 'class': className}.items(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Python 3 has only |
||
if value is not None: | ||
result[key] = value | ||
|
||
return json.dumps(result) | ||
|
||
|
||
class AppiumSearchContext(webdriver.Remote, | ||
AndroidSearchContext): | ||
"""Returns appium driver search conext""" | ||
|
||
|
||
class AppiumWebElementSearchContext(SeleniumWebElement, | ||
AndroidSearchContext): | ||
"""Returns appium web element search context""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/usr/bin/env python | ||
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import json | ||
import httpretty | ||
|
||
from appium import webdriver | ||
from appium.webdriver.webelement import WebElement as MobileWebElement | ||
|
||
from appium import version as appium_version | ||
|
||
from test.unit.helper.test_helper import ( | ||
appium_command, | ||
android_w3c_driver, | ||
get_httpretty_request_body | ||
) | ||
|
||
|
||
class TestWebElement(object): | ||
|
||
@httpretty.activate | ||
def test_find_element_by_android_data_matcher(self): | ||
driver = android_w3c_driver() | ||
element = MobileWebElement(driver, 'element_id', w3c=True) | ||
httpretty.register_uri( | ||
httpretty.POST, | ||
appium_command('/session/1234567890/element/element_id/element'), | ||
body='{"value": {"element-6066-11e4-a52e-4f735466cecf": "child-element-id"}}' | ||
) | ||
el = element.find_element_by_android_data_matcher( | ||
name='title', args=['title', 'Animation'], className='class name') | ||
|
||
d = get_httpretty_request_body(httpretty.last_request()) | ||
assert d['using'] == '-android datamatcher' | ||
value_dict = json.loads(d['value']) | ||
assert value_dict['args'] == ['title', 'Animation'] | ||
assert value_dict['name'] == 'title' | ||
assert value_dict['class'] == 'class name' | ||
assert el.id == 'child-element-id' | ||
|
||
@httpretty.activate | ||
def test_find_elements_by_android_data_matcher(self): | ||
driver = android_w3c_driver() | ||
element = MobileWebElement(driver, 'element_id', w3c=True) | ||
httpretty.register_uri( | ||
httpretty.POST, | ||
appium_command('/session/1234567890/element/element_id/elements'), | ||
body='{"value": [{"element-6066-11e4-a52e-4f735466cecf": "child-element-id1"}, {"element-6066-11e4-a52e-4f735466cecf": "child-element-id2"}]}' | ||
) | ||
els = element.find_elements_by_android_data_matcher(name='title', args=['title', 'Animation']) | ||
|
||
d = get_httpretty_request_body(httpretty.last_request()) | ||
assert d['using'] == '-android datamatcher' | ||
value_dict = json.loads(d['value']) | ||
assert value_dict['args'] == ['title', 'Animation'] | ||
assert value_dict['name'] == 'title' | ||
assert els[0].id == 'child-element-id1' | ||
assert els[1].id == 'child-element-id2' | ||
|
||
@httpretty.activate | ||
def test_find_elements_by_android_data_matcher_no_value(self): | ||
driver = android_w3c_driver() | ||
element = MobileWebElement(driver, 'element_id', w3c=True) | ||
httpretty.register_uri( | ||
httpretty.POST, | ||
appium_command('/session/1234567890/element/element_id/elements'), | ||
body='{"value": []}' | ||
) | ||
els = element.find_elements_by_android_data_matcher() | ||
|
||
d = get_httpretty_request_body(httpretty.last_request()) | ||
assert d['using'] == '-android datamatcher' | ||
assert d['value'] == '{}' | ||
assert len(els) == 0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.