Skip to content

Commit d63603c

Browse files
committed
Add find methods to WebElement
1 parent ae0dd8e commit d63603c

7 files changed

Lines changed: 138 additions & 1 deletion

File tree

‎appium/webdriver/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
"""
1818

1919
from webdriver import WebDriver as Remote
20+
from webelement import WebElement

‎appium/webdriver/webdriver.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .mobilecommand import MobileCommand as Command
1818
from .errorhandler import MobileErrorHandler
1919
from .switch_to import MobileSwitchTo
20+
from .webelement import WebElement as MobileWebElement
2021

2122
from appium.webdriver.common.mobileby import MobileBy
2223
from appium.webdriver.common.touch_action import TouchAction
@@ -133,6 +134,14 @@ def find_elements_by_accessibility_id(self, id):
133134
"""
134135
return self.find_elements(by=By.ACCESSIBILITY_ID, value=id)
135136

137+
def create_web_element(self, element_id):
138+
"""
139+
Creates a web element with the specified element_id.
140+
Overrides method in Selenium WebDriver in order to always give them
141+
Appium WebElement
142+
"""
143+
return MobileWebElement(self, element_id)
144+
136145
# convenience method added to Appium (NOT Selenium 3)
137146
def scroll(self, originEl, destinationEl):
138147
"""Scrolls from one element to another

‎appium/webdriver/webelement.py‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
from selenium.webdriver.common.by import By
16+
from selenium.webdriver.remote.webelement import WebElement as SeleniumWebElement
17+
18+
class WebElement(SeleniumWebElement):
19+
def find_element_by_ios_uiautomation(self, uia_string):
20+
"""Finds an element by uiautomation in iOS.
21+
22+
:Args:
23+
- uia_string - The element name in the iOS UIAutomation library
24+
25+
:Usage:
26+
driver.find_element_by_ios_uiautomation('.elements()[1].cells()[2]')
27+
"""
28+
return self.find_element(by=By.IOS_UIAUTOMATION, value=uia_string)
29+
30+
def find_elements_by_ios_uiautomation(self, uia_string):
31+
"""Finds elements by uiautomation in iOS.
32+
33+
:Args:
34+
- uia_string - The element name in the iOS UIAutomation library
35+
36+
:Usage:
37+
driver.find_elements_by_ios_uiautomation('.elements()[1].cells()[2]')
38+
"""
39+
return self.find_elements(by=By.IOS_UIAUTOMATION, value=uia_string)
40+
41+
def find_element_by_android_uiautomator(self, uia_string):
42+
"""Finds element by uiautomator in Android.
43+
44+
:Args:
45+
- uia_string - The element name in the Android UIAutomator library
46+
47+
:Usage:
48+
driver.find_element_by_android_uiautomator('.elements()[1].cells()[2]')
49+
"""
50+
return self.find_element(by=By.ANDROID_UIAUTOMATOR, value=uia_string)
51+
52+
def find_elements_by_android_uiautomator(self, uia_string):
53+
"""Finds elements by uiautomator in Android.
54+
55+
:Args:
56+
- uia_string - The element name in the Android UIAutomator library
57+
58+
:Usage:
59+
driver.find_elements_by_android_uiautomator('.elements()[1].cells()[2]')
60+
"""
61+
return self.find_elements(by=By.ANDROID_UIAUTOMATOR, value=uia_string)
62+
63+
def find_element_by_accessibility_id(self, id):
64+
"""Finds an element by accessibility id.
65+
66+
:Args:
67+
- id - a string corresponding to a recursive element search using the
68+
Id/Name that the native Accessibility options utilize
69+
70+
:Usage:
71+
driver.find_element_by_accessibility_id()
72+
"""
73+
return self.find_element(by=By.ACCESSIBILITY_ID, value=id)
74+
75+
def find_elements_by_accessibility_id(self, id):
76+
"""Finds elements by accessibility id.
77+
78+
:Args:
79+
- id - a string corresponding to a recursive element search using the
80+
Id/Name that the native Accessibility options utilize
81+
82+
:Usage:
83+
driver.find_elements_by_accessibility_id()
84+
"""
85+
return self.find_elements(by=By.ACCESSIBILITY_ID, value=id)

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
setup(
1919
name='Appium-Python-Client',
20-
version='0.2',
20+
version='0.3',
2121
description='Python client for Appium 1.0',
2222
keywords=[
2323
'appium',

‎test/functional/android/find_by_accessibility_id_tests.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,18 @@ def test_find_multiple_elements(self):
3434
els = self.driver.find_elements_by_accessibility_id('Animation')
3535
self.assertIsInstance(els, list)
3636

37+
def test_element_find_single_element(self):
38+
el = self.driver.find_element_by_class_name('android.widget.ListView')
39+
40+
sub_el = el.find_element_by_accessibility_id('Animation')
41+
self.assertIsNotNone(sub_el)
42+
43+
def test_element_find_multiple_elements(self):
44+
el = self.driver.find_element_by_class_name('android.widget.ListView')
45+
46+
sub_els = el.find_elements_by_accessibility_id('Animation')
47+
self.assertIsInstance(sub_els, list)
48+
49+
3750
if __name__ == "__main__":
3851
unittest.main()

‎test/functional/android/find_by_uiautomator_tests.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,17 @@ def test_find_multiple_elements(self):
3434
els = self.driver.find_elements_by_android_uiautomator('new UiSelector().clickable(true)')
3535
self.assertTrue(len(els) > 11)
3636

37+
def test_element_find_single_element(self):
38+
el = self.driver.find_element_by_class_name('android.widget.ListView')
39+
40+
sub_el = el.find_element_by_android_uiautomator('new UiSelector().description("Animation")')
41+
self.assertIsNotNone(sub_el)
42+
43+
def test_element_find_multiple_elements(self):
44+
el = self.driver.find_element_by_class_name('android.widget.ListView')
45+
46+
sub_els = self.driver.find_elements_by_android_uiautomator('new UiSelector().clickable(true)')
47+
self.assertTrue(len(sub_els) > 11)
48+
3749
if __name__ == "__main__":
3850
unittest.main()

‎test/functional/ios/find_by_uiautomation_tests.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import unittest
2020

21+
from time import sleep
2122
class FindByUIAutomationTests(unittest.TestCase):
2223
def setUp(self):
2324
desired_caps = desired_capabilities.get_desired_capabilities('UICatalog.app.zip')
@@ -34,5 +35,21 @@ def test_find_multiple_elements(self):
3435
els = self.driver.find_elements_by_ios_uiautomation('elements()')
3536
self.assertEqual(3, len(els))
3637

38+
def test_element_find_single_element(self):
39+
# get the list
40+
el = self.driver.find_element_by_ios_uiautomation('.elements()[1]')
41+
42+
# get the search bar button
43+
sub_el = el.find_element_by_ios_uiautomation('.elements()[3]')
44+
self.assertEqual('SearchBar, Use of UISearchBar', sub_el.get_attribute('name'))
45+
46+
def test_element_find_multiple_elements(self):
47+
# get the list
48+
el = self.driver.find_element_by_ios_uiautomation('.elements()[1]')
49+
50+
# get the buttons
51+
sub_el = el.find_elements_by_ios_uiautomation('.elements()')
52+
self.assertEqual(12, len(sub_el))
53+
3754
if __name__ == "__main__":
3855
unittest.main()

0 commit comments

Comments
 (0)