Skip to content

Commit 3d22346

Browse files
authored
Merge pull request appium#145 from benzhou29/master
Adding clear and find elements by ios predicate string.
2 parents 399f800 + db79041 commit 3d22346

File tree

6 files changed

+142
-0
lines changed

6 files changed

+142
-0
lines changed

appium/webdriver/common/mobileby.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717

1818
class MobileBy(By):
19+
IOS_PREDICATE = '-ios predicate string'
1920
IOS_UIAUTOMATION = '-ios uiautomation'
2021
ANDROID_UIAUTOMATOR = '-android uiautomator'
2122
ACCESSIBILITY_ID = 'accessibility id'

appium/webdriver/mobilecommand.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ class MobileCommand(object):
5858
UPDATE_SETTINGS = 'updateSettings'
5959
SET_LOCATION = 'setLocation'
6060
GET_DEVICE_TIME = 'getDeviceTime'
61+
CLEAR = 'clear'

appium/webdriver/webdriver.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def __init__(self, command_executor='http://127.0.0.1:4444/wd/hub',
4343

4444
# add new method to the `find_by_*` pantheon
4545
By.IOS_UIAUTOMATION = MobileBy.IOS_UIAUTOMATION
46+
By.IOS_PREDICATE = MobileBy.IOS_PREDICATE
4647
By.ANDROID_UIAUTOMATOR = MobileBy.ANDROID_UIAUTOMATOR
4748
By.ACCESSIBILITY_ID = MobileBy.ACCESSIBILITY_ID
4849

@@ -98,6 +99,28 @@ def find_elements_by_ios_uiautomation(self, uia_string):
9899
"""
99100
return self.find_elements(by=By.IOS_UIAUTOMATION, value=uia_string)
100101

102+
def find_element_by_ios_predicate(self, predicate_string):
103+
"""Find an element by ios predicate string.
104+
105+
:Args:
106+
- predicate_string - The predicate string
107+
108+
:Usage:
109+
driver.find_element_by_ios_predicate('label == "myLabel"')
110+
"""
111+
return self.find_element(by=By.IOS_PREDICATE, value=predicate_string)
112+
113+
def find_elements_by_ios_predicate(self, predicate_string):
114+
"""Finds elements by ios predicate string.
115+
116+
:Args:
117+
- predicate_string - The predicate string
118+
119+
:Usage:
120+
driver.find_elements_by_ios_predicate('label == "myLabel"')
121+
"""
122+
return self.find_elements(by=By.IOS_PREDICATE, value=predicate_string)
123+
101124
def find_element_by_android_uiautomator(self, uia_string):
102125
"""Finds element by uiautomator in Android.
103126
@@ -829,3 +852,5 @@ def _addCommands(self):
829852
('GET', '/session/$sessionId/element/$id/location_in_view')
830853
self.command_executor._commands[Command.GET_DEVICE_TIME] = \
831854
('GET', '/session/$sessionId/appium/device/system_time')
855+
self.command_executor._commands[Command.CLEAR] = \
856+
('POST', '/session/$sessionId/element/$id/clear')

appium/webdriver/webelement.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ def find_elements_by_ios_uiautomation(self, uia_string):
4141
"""
4242
return self.find_elements(by=By.IOS_UIAUTOMATION, value=uia_string)
4343

44+
def find_element_by_ios_predicate(self, predicate_string):
45+
"""Find an element by ios predicate string.
46+
47+
:Args:
48+
- predicate_string - The predicate string
49+
50+
:Usage:
51+
driver.find_element_by_ios_predicate('label == "myLabel"')
52+
"""
53+
return self.find_element(by=By.IOS_PREDICATE, value=predicate_string)
54+
55+
def find_elements_by_ios_predicate(self, predicate_string):
56+
"""Finds elements by ios predicate string.
57+
58+
:Args:
59+
- predicate_string - The predicate string
60+
61+
:Usage:
62+
driver.find_elements_by_ios_predicate('label == "myLabel"')
63+
"""
64+
return self.find_elements(by=By.IOS_PREDICATE, value=predicate_string)
65+
4466
def find_element_by_android_uiautomator(self, uia_string):
4567
"""Finds element by uiautomator in Android.
4668
@@ -122,3 +144,10 @@ def set_value(self, value):
122144
}
123145
self._execute(Command.SET_IMMEDIATE_VALUE, data)
124146
return self
147+
148+
def clear(self):
149+
"""Clears text.
150+
"""
151+
data = {'id': self.id}
152+
self._execute(Command.CLEAR, data)
153+
return self

test/functional/ios/appium_tests.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@ def test_hide_keyboard_no_key_name(self):
9696
# currently fails.
9797
self.assertFalse(el.is_displayed())
9898

99+
def test_clear(self):
100+
# Click text fields
101+
self.driver.find_element_by_accessibility_id('TextFields').click()
102+
103+
# Verify default text
104+
def_text = '<enter text>'
105+
text = self.driver.find_element_by_accessibility_id('Normal').get_attribute('value')
106+
self.assertEqual(text, def_text)
107+
108+
# Input some text, verify
109+
input_text = 'blah'
110+
self.driver.find_element_by_accessibility_id('Normal').send_keys(input_text)
111+
text = self.driver.find_element_by_accessibility_id('Normal').get_attribute('value')
112+
self.assertEqual(text, input_text)
113+
114+
# Clear text, verify
115+
self.driver.find_element_by_accessibility_id('Normal').clear()
116+
text = self.driver.find_element_by_accessibility_id('Normal').get_attribute('value')
117+
self.assertEqual(text, def_text)
118+
99119

100120
if __name__ == "__main__":
101121
suite = unittest.TestLoader().loadTestsFromTestCase(AppiumTests)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 unittest
16+
17+
from appium import webdriver
18+
import desired_capabilities
19+
20+
21+
class FindByIOSPredicateTests(unittest.TestCase):
22+
@classmethod
23+
def setUpClass(self):
24+
desired_caps = desired_capabilities.get_desired_capabilities('UICatalog.app.zip')
25+
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
26+
27+
@classmethod
28+
def tearDownClass(self):
29+
self.driver.quit()
30+
31+
def test_find_element_by_name(self):
32+
# Will throw exception if element is not found
33+
self.driver.find_element_by_ios_predicate('wdName == "Buttons"')
34+
35+
def test_find_multiple_element_by_type(self):
36+
e = self.driver.find_elements_by_ios_predicate('wdType == "XCUIElementTypeStaticText"')
37+
self.assertNotEqual(len(e), 0)
38+
39+
def test_find_element_by_label(self):
40+
# Will throw exception if element is not found
41+
self.driver.find_element_by_ios_predicate('label == "TextFields"')
42+
43+
def test_find_element_by_value(self):
44+
# Will throw exception if element is not found
45+
self.driver.find_element_by_ios_predicate('wdValue == "Controls"')
46+
47+
def test_find_element_by_isvisible(self):
48+
# Will throw exception if element is not found
49+
self.driver.find_element_by_ios_predicate('wdValue == "SearchBar" AND isWDVisible == 1')
50+
51+
# Should not find any elements
52+
e = self.driver.find_elements_by_ios_predicate('wdValue == "SearchBar" AND isWDVisible == 0')
53+
self.assertEqual(len(e), 0)
54+
55+
def test_find_element_by_isenabled(self):
56+
# Will throw exception if element is not found
57+
self.driver.find_element_by_ios_predicate('wdValue == "SearchBar" AND isWDEnabled == 1')
58+
59+
# Should not find any elements
60+
e = self.driver.find_elements_by_ios_predicate('wdValue == "SearchBar" AND isWDEnabled == 0')
61+
self.assertEqual(len(e), 0)
62+
63+
64+
if __name__ == "__main__":
65+
suite = unittest.TestLoader().loadTestsFromTestCase(FindByIOSPredicateTests)
66+
unittest.TextTestRunner(verbosity=2).run(suite)

0 commit comments

Comments
 (0)