Skip to content

Commit 7dbf4f2

Browse files
authored
test: update tests to use find_element(by...) (#674)
* test: update find element/s methods * fix arguments * fix default value
1 parent e3b55e1 commit 7dbf4f2

19 files changed

+139
-140
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ environment:
106106
# Android environment
107107
import unittest
108108
from appium import webdriver
109+
from appium.webdriver.common.appiumby import AppiumBy
109110
110111
desired_caps = dict(
111112
platformName='Android',
@@ -115,14 +116,15 @@ desired_caps = dict(
115116
app=PATH('../../../apps/selendroid-test-app.apk')
116117
)
117118
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
118-
el = self.driver.find_element_by_accessibility_id('item')
119+
el = self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value='item')
119120
el.click()
120121
```
121122

122123
```python
123124
# iOS environment
124125
import unittest
125126
from appium import webdriver
127+
from appium.webdriver.common.appiumby import AppiumBy
126128
127129
desired_caps = dict(
128130
platformName='iOS',
@@ -133,7 +135,7 @@ desired_caps = dict(
133135
)
134136
135137
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
136-
el = self.driver.find_element_by_accessibility_id('item')
138+
el = self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value='item')
137139
el.click()
138140
```
139141

appium/webdriver/webdriver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def add_command(self):
189189
190190
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps,
191191
extensions=[YourCustomCommand])
192-
element = driver.find_element_by_accessibility_id('id')
192+
element = driver.find_elemeent(by=AppiumBy.ACCESSIBILITY_ID, value='id')
193193
194194
# Then, the driver calls a get request to `session/$sessionId/path/to/your/custom/$id/url`
195195
# with replacing the `$id` with the given `element.id`

docs/roadmap.md

Lines changed: 0 additions & 36 deletions
This file was deleted.

test/functional/android/chrome_tests.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
from appium import webdriver
16+
from appium.webdriver.common.appiumby import AppiumBy
1617

1718
from .helper.desired_capabilities import get_desired_capabilities
1819

@@ -28,6 +29,6 @@ def teardown_method(self) -> None:
2829

2930
def test_find_single_element(self) -> None:
3031
self.driver.get('http://10.0.2.2:4723/test/guinea-pig')
31-
self.driver.find_element_by_link_text('i am a link').click()
32+
self.driver.find_element(by=AppiumBy.LINK_TEXT, value='i am a link').click()
3233

3334
assert 'I am some other page content' in self.driver.page_source

test/functional/android/common_tests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ def test_open_notifications(self) -> None:
4343
self.driver.open_notifications()
4444
sleep(1)
4545
with pytest.raises(NoSuchElementException):
46-
self.driver.find_element_by_android_uiautomator, 'new UiSelector().text(":-|")'
46+
self.driver.find_element(by=AppiumBy.ANDROID_UIAUTOMATOR, value='new UiSelector().text(":-|")')
4747

48-
els = self.driver.find_elements_by_class_name('android.widget.TextView')
48+
els = self.driver.find_element(by=AppiumBy.CLASS_NAME, value='android.widget.TextView')
4949
# sometimes numbers shift
5050
title = False
5151
body = False
@@ -60,4 +60,4 @@ def test_open_notifications(self) -> None:
6060

6161
self.driver.keyevent(4)
6262
sleep(1)
63-
self.driver.find_element_by_android_uiautomator('new UiSelector().text(":-|")')
63+
self.driver.find_element(by=AppiumBy.ANDROID_UIAUTOMATOR, value='new UiSelector().text(":-|")')

test/functional/android/context_switching_tests.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from appium import webdriver
1818
from appium.common.exceptions import NoSuchContextException
19+
from appium.webdriver.common.appiumby import AppiumBy
1920

2021
from .helper import desired_capabilities
2122

@@ -40,8 +41,8 @@ def test_move_to_correct_context(self) -> None:
4041

4142
def test_actually_in_webview(self) -> None:
4243
self._enter_webview()
43-
self.driver.find_element_by_css_selector('input[type=submit]').click()
44-
el = self.driver.find_element_by_xpath("//h1[contains(., 'This is my way')]")
44+
self.driver.find_element(by=AppiumBy.CSS_SELECTOR, value='input[type=submit]').click()
45+
el = self.driver.find_element(by=AppiumBy.XPATH, value="//h1[contains(., 'This is my way')]")
4546
assert el is not None
4647

4748
def test_move_back_to_native_context(self) -> None:
@@ -54,6 +55,6 @@ def test_set_invalid_context(self) -> None:
5455
self.driver.switch_to.context('invalid name')
5556

5657
def _enter_webview(self) -> None:
57-
btn = self.driver.find_element_by_name('buttonStartWebviewCD')
58+
btn = self.driver.find_element(by=AppiumBy.NAME, value='buttonStartWebviewCD')
5859
btn.click()
5960
self.driver.switch_to.context('WEBVIEW')

test/functional/android/multi_action_tests.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class TestMultiAction(BaseTestCase):
2828
def test_parallel_actions(self) -> None:
2929
self._move_to_splitting_touches_accros_views()
3030

31-
els = self.driver.find_elements_by_class_name('android.widget.ListView')
31+
els = self.driver.find_elements(by=AppiumBy.CLASS_NAME, value='android.widget.ListView')
3232
a1 = TouchAction()
3333
a1.press(els[0]).move_to(x=10, y=0).move_to(x=10, y=-75).move_to(x=10, y=-600).release()
3434

@@ -42,7 +42,7 @@ def test_parallel_actions(self) -> None:
4242
def test_actions_with_waits(self) -> None:
4343
self._move_to_splitting_touches_accros_views()
4444

45-
els = self.driver.find_elements_by_class_name('android.widget.ListView')
45+
els = self.driver.find_elements(by=AppiumBy.CLASS_NAME, value='android.widget.ListView')
4646
a1 = TouchAction()
4747
a1.press(els[0]).move_to(x=10, y=0).move_to(x=10, y=-75).wait(1000).move_to(x=10, y=-600).release()
4848

@@ -54,40 +54,40 @@ def test_actions_with_waits(self) -> None:
5454
ma.perform()
5555

5656
def _move_to_splitting_touches_accros_views(self) -> None:
57-
el1 = self.driver.find_element_by_accessibility_id('Content')
58-
el2 = self.driver.find_element_by_accessibility_id('Animation')
57+
el1 = self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value='Content')
58+
el2 = self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value='Animation')
5959
self.driver.scroll(el1, el2)
6060

61-
el = self.driver.find_element_by_accessibility_id('Views')
61+
el = self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value='Views')
6262
action = TouchAction(self.driver)
6363
action.tap(el).perform()
6464

6565
# simulate a swipe/scroll
6666
el = wait_for_element(self.driver, AppiumBy.ACCESSIBILITY_ID, 'Expandable Lists')
6767
action.press(el).move_to(x=100, y=-1000).release().perform()
68-
el = self.driver.find_element_by_accessibility_id('Layouts')
68+
el = self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value='Layouts')
6969
action.press(el).move_to(x=100, y=-1000).release().perform()
7070

71-
el = self.driver.find_element_by_accessibility_id('Splitting Touches across Views')
71+
el = self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value='Splitting Touches across Views')
7272
action.tap(el).perform()
7373

7474
wait_for_element(self.driver, AppiumBy.ID, 'io.appium.android.apis:id/list1')
7575

7676
@pytest.mark.skipif(condition=is_ci(), reason='Skip since the test must be watched to check if it works')
7777
def test_driver_multi_tap(self) -> None:
78-
el = self.driver.find_element_by_accessibility_id('Graphics')
78+
el = self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value='Graphics')
7979
action = TouchAction(self.driver)
8080
action.tap(el).perform()
8181

8282
wait_for_element(self.driver, AppiumBy.CLASS_NAME, 'android.widget.TextView')
83-
els = self.driver.find_elements_by_class_name('android.widget.TextView')
83+
els = self.driver.find_elements(by=AppiumBy.CLASS_NAME, value='android.widget.TextView')
8484
self.driver.scroll(els[len(els) - 1], els[0])
8585

86-
els = self.driver.find_elements_by_class_name('android.widget.TextView')
86+
els = self.driver.find_elements(by=AppiumBy.CLASS_NAME, value='android.widget.TextView')
8787
if els[len(els) - 1].get_attribute('name') != 'Xfermodes':
8888
self.driver.scroll(els[len(els) - 1], els[0])
8989

90-
el = self.driver.find_element_by_accessibility_id('Touch Paint')
90+
el = self.driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value='Touch Paint')
9191
action.tap(el).perform()
9292

9393
positions = [(100, 200), (100, 400)]

test/functional/android/search_context/find_by_accessibility_id_tests.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import pytest
1616

1717
from appium.webdriver.common.appiumby import AppiumBy
18+
from appium.webdriver.webelement import WebElement
1819
from test.functional.android.helper.test_helper import BaseTestCase, is_ci
1920
from test.functional.test_helper import wait_for_element
2021

@@ -29,7 +30,7 @@ def test_find_single_element(self) -> None:
2930
assert el is not None
3031

3132
def test_find_multiple_elements(self) -> None:
32-
els = self.driver.find_elements_by_accessibility_id('Accessibility')
33+
els = self.driver.find_elements(by=AppiumBy.ACCESSIBILITY_ID, value='Accessibility')
3334
assert isinstance(els, list)
3435

3536
@pytest.mark.skipif(condition=is_ci(), reason='Need to fix flaky test during running on CI')
@@ -40,12 +41,11 @@ def test_element_find_single_element(self) -> None:
4041
).click()
4142
el = wait_for_element(self.driver, AppiumBy.CLASS_NAME, 'android.widget.ListView')
4243

43-
sub_el = el.find_element_by_accessibility_id('Task Take out Trash')
44+
sub_el = el.find_element(by=AppiumBy.ACCESSIBILITY_ID, value='Task Take out Trash') # type: WebElement
4445
assert sub_el is not None
4546

4647
def test_element_find_multiple_elements(self) -> None:
4748
wait_for_element(self.driver, AppiumBy.CLASS_NAME, 'android.widget.ListView')
48-
el = self.driver.find_element_by_class_name('android.widget.ListView')
49-
50-
sub_els = el.find_elements_by_accessibility_id('Animation')
49+
el = self.driver.find_element(by=AppiumBy.CLASS_NAME, value='android.widget.ListView')
50+
sub_els = el.find_elements(by=AppiumBy.ACCESSIBILITY_ID, value='Animation') # type: list
5151
assert isinstance(sub_els, list)

test/functional/android/search_context/find_by_uiautomator_tests.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,41 @@
1414

1515
import pytest
1616

17+
from appium.webdriver.common.appiumby import AppiumBy
18+
from appium.webdriver.webelement import WebElement
1719
from test.functional.android.helper.test_helper import BaseTestCase
1820

1921

2022
@pytest.mark.skip(reason="Need to fix flaky test")
2123
class TestFindByUIAutomator(BaseTestCase):
2224
def test_find_single_element(self) -> None:
23-
el = self.driver.find_element_by_android_uiautomator('new UiSelector().text("Animation")')
25+
el = self.driver.find_element(by=AppiumBy.ANDROID_UIAUTOMATOR, value='new UiSelector().text("Animation")')
2426
assert el is not None
2527

2628
def test_find_multiple_elements(self) -> None:
27-
els = self.driver.find_elements_by_android_uiautomator('new UiSelector().clickable(true)')
29+
els = self.driver.find_elements(by=AppiumBy.ANDROID_UIAUTOMATOR, value='new UiSelector().clickable(true)')
2830
assert isinstance(els, list)
2931

3032
def test_element_find_single_element(self) -> None:
31-
el = self.driver.find_element_by_class_name('android.widget.ListView')
33+
el = self.driver.find_element(by=AppiumBy.CLASS_NAME, value='android.widget.ListView')
3234

33-
sub_el = el.find_element_by_android_uiautomator('new UiSelector().description("Animation")')
35+
sub_el = el.find_element(
36+
by=AppiumBy.ANDROID_UIAUTOMATOR, value='new UiSelector().description("Animation")'
37+
) # type: WebElement
3438
assert sub_el is not None
3539

3640
def test_element_find_multiple_elements(self) -> None:
37-
el = self.driver.find_element_by_class_name('android.widget.ListView')
41+
el = self.driver.find_element(by=AppiumBy.CLASS_NAME, value='android.widget.ListView')
3842

39-
sub_els = el.find_elements_by_android_uiautomator('new UiSelector().clickable(true)')
43+
sub_els = el.find_elements(
44+
by=AppiumBy.ANDROID_UIAUTOMATOR, value='new UiSelector().clickable(true)'
45+
) # type: list
4046
assert isinstance(sub_els, list)
4147

4248
def test_scroll_into_view(self) -> None:
43-
el = self.driver.find_element_by_android_uiautomator(
44-
'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("Views").instance(0));'
49+
el = self.driver.find_element(
50+
by=AppiumBy.ANDROID_UIAUTOMATOR,
51+
value='new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("Views").instance(0));',
4552
)
4653
el.click()
4754
# TODO Add assert

test/functional/android/search_context/find_by_view_matcher_tests.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
16-
import unittest
15+
import json
1716

1817
import pytest
1918
from selenium.common.exceptions import WebDriverException
@@ -25,7 +24,6 @@
2524

2625

2726
class TestFindByViewMatcher(BaseTestCase):
28-
2927
# Override
3028
def setup_method(self, method) -> None: # type: ignore
3129
desired_caps = desired_capabilities.get_desired_capabilities('ApiDemos-debug.apk.zip')
@@ -35,30 +33,39 @@ def setup_method(self, method) -> None: # type: ignore
3533
self.driver.start_recording_screen()
3634

3735
def test_find_single_element(self) -> None:
38-
el = self.driver.find_element_by_android_view_matcher(
39-
name='withText', args=['Accessibility'], className='ViewMatchers'
36+
el = self.driver.find_element(
37+
by=AppiumBy.ANDROID_VIEW_MATCHER,
38+
value=json.dumps({'name': 'withText', 'args': ['Accessibility'], 'class': 'ViewMatchers'}),
4039
)
4140
assert el.text == 'Accessibility'
4241

4342
def test_find_single_element_ful_class_name(self) -> None:
44-
el = self.driver.find_element_by_android_view_matcher(
45-
name='withText', args=['Accessibility'], className='androidx.test.espresso.matcher.ViewMatchers'
43+
el = self.driver.find_element(
44+
by=AppiumBy.ANDROID_VIEW_MATCHER,
45+
value=json.dumps(
46+
{'name': 'withText', 'args': ['Accessibility'], 'class': 'androidx.test.espresso.matcher.ViewMatchers'}
47+
),
4648
)
4749
assert el.text == 'Accessibility'
4850

4951
def test_find_single_element_using_hamcrest_matcher(self) -> None:
50-
el = self.driver.find_element_by_android_view_matcher(
51-
name='withText',
52-
args={'name': 'containsString', 'args': 'Animati', 'class': 'org.hamcrest.Matchers'},
53-
className='ViewMatchers',
52+
el = self.driver.find_element(
53+
by=AppiumBy.ANDROID_VIEW_MATCHER,
54+
value=json.dumps(
55+
{
56+
'name': 'withText',
57+
'args': {'name': 'containsString', 'args': 'Animati', 'class': 'org.hamcrest.Matchers'},
58+
'class': 'ViewMatchers',
59+
}
60+
),
5461
)
5562
assert el.text == 'Animation'
5663

5764
# androidx.test.espresso.AmbiguousViewMatcherException:
5865
# 'with text: a string containing "Access"' matches multiple views in the hierarchy.
5966
def test_find_multiple_elements(self) -> None:
60-
value = AndroidSearchContext()._build_data_matcher(
61-
name='withSubstring', args=['Access'], className='ViewMatchers'
62-
)
6367
with pytest.raises(WebDriverException):
64-
self.driver.find_elements(by=AppiumBy.ANDROID_VIEW_MATCHER, value=value)
68+
self.driver.find_elements(
69+
by=AppiumBy.ANDROID_VIEW_MATCHER,
70+
value=json.dumps({'name': 'withSubstring', 'args': ['Access'], 'class': 'ViewMatchers'}),
71+
)

0 commit comments

Comments
 (0)