Skip to content
This repository was archived by the owner on Mar 31, 2020. It is now read-only.

Commit e76c39e

Browse files
committed
Merge pull request appium#3 from appium/isaac-android-uiautomator
Add Android UIAutomator locator strategy
2 parents f3751e0 + 0603e33 commit e76c39e

8 files changed

Lines changed: 101 additions & 2 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,10 @@ driver.switch_to.context(context_name)
8080
This allows elements in iOS applications to be found using recursive element
8181
search using the UIAutomation library. Adds the methods `driver.find_element_by_ios_uiautomation`
8282
and `driver.find_elements_by_ios_uiautomation`.
83+
84+
85+
### Finding elements by Android UIAutomator search
86+
87+
This allows elements in an Android application to be found using recursive element
88+
search using the UIAutomator library. Adds the methods `driver.find_element_by_android_uiautomator`
89+
and `driver.find_elements_by_android_uiautomator`.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616

1717
class MobileBy(By):
1818
IOS_UIAUTOMATION = '-ios uiautomation'
19+
ANDROID_UIAUTOMATOR = '-android uiautomator'

appium/webdriver/webdriver.py

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

3636
# add new method to the `find_by_*` pantheon
3737
By.IOS_UIAUTOMATION = MobileBy.IOS_UIAUTOMATION
38+
By.ANDROID_UIAUTOMATOR = MobileBy.ANDROID_UIAUTOMATOR
3839

3940
@property
4041
def contexts(self):
@@ -78,6 +79,28 @@ def find_elements_by_ios_uiautomation(self, uia_string):
7879
"""
7980
return self.find_elements(by=By.IOS_UIAUTOMATION, value=uia_string)
8081

82+
def find_element_by_android_uiautomator(self, uia_string):
83+
"""Finds element by uiautomator in Android.
84+
85+
:Args:
86+
- uia_string - The element name in the Android UIAutomator library
87+
88+
:Usage:
89+
driver.find_element_by_android_uiautomator('.elements()[1].cells()[2]')
90+
"""
91+
return self.find_element(by=By.ANDROID_UIAUTOMATOR, value=uia_string)
92+
93+
def find_elements_by_android_uiautomator(self, uia_string):
94+
"""Finds elements by uiautomator in Android.
95+
96+
:Args:
97+
- uia_string - The element name in the Android UIAutomator library
98+
99+
:Usage:
100+
driver.find_elements_by_android_uiautomator('.elements()[1].cells()[2]')
101+
"""
102+
return self.find_elements(by=By.ANDROID_UIAUTOMATOR, value=uia_string)
103+
81104
def _addCommands(self):
82105
self.command_executor._commands[Command.CONTEXTS] = \
83106
('GET', '/session/$sessionId/contexts')

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
author='Isaac Murchie',
2121
author_email='[email protected]',
2222
url='http://appium.io/',
23-
packages=['appium', 'appium.webdriver', 'appium.common'],
23+
packages=['appium',
24+
'appium.common',
25+
'appium.webdriver',
26+
'appium.webdriver.common'],
2427
license='Apache 2.0'
2528
)

test/apps/ApiDemos-debug.apk

2.94 MB
Binary file not shown.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
from time import sleep
17+
18+
from appium import webdriver
19+
from appium.common.exceptions import NoSuchContextException
20+
21+
import unittest
22+
23+
# Returns abs path relative to this file and not cwd
24+
PATH = lambda p: os.path.abspath(
25+
os.path.join(os.path.dirname(__file__), p)
26+
)
27+
28+
class FindByUIAutomatorTests(unittest.TestCase):
29+
def setUp(self):
30+
desired_caps = {}
31+
desired_caps['device'] = 'Android'
32+
desired_caps['browserName'] = ''
33+
desired_caps['version'] = '4.2'
34+
desired_caps['app'] = PATH('../../apps/ApiDemos-debug.apk')
35+
desired_caps['app-package'] = 'com.example.android.apis'
36+
desired_caps['app-activity'] = '.ApiDemos'
37+
38+
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
39+
40+
def tearDown(self):
41+
self.driver.quit()
42+
43+
def test_find_single_element(self):
44+
el = self.driver.find_element_by_android_uiautomator('new UiSelector().description("Animation")')
45+
self.assertIsNotNone(el)
46+
47+
def test_find_multiple_elements(self):
48+
els = self.driver.find_elements_by_android_uiautomator('new UiSelector().clickable(true)')
49+
self.assertTrue(len(els) > 11)
50+
51+
if __name__ == "__main__":
52+
unittest.main()

test/functional/ios/find_by_uiautomation_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_find_single_element(self):
3636
el = self.driver.find_element_by_ios_uiautomation('.elements()[0]')
3737
self.assertEqual('UICatalog', el.get_attribute('name'))
3838

39-
def test_find_multiple_element(self):
39+
def test_find_multiple_elements(self):
4040
els = self.driver.find_elements_by_ios_uiautomation('elements()')
4141
self.assertEqual(3, len(els))
4242

0 commit comments

Comments
 (0)