Skip to content

Commit 6cc1e14

Browse files
authored
Fix functional tests failed (android, appium_tests) (appium#366)
* Fix test failed: test_send_keys, test_screen_record * Fix test failed: test_update_settings * Fix test failed: test_start_activity_other_app * Move and rename helper package * Update along to review comments * Add return value to wait_for_element
1 parent 807d424 commit 6cc1e14

3 files changed

Lines changed: 50 additions & 27 deletions

File tree

test/functional/android/appium_tests.py

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@
2121
from time import sleep
2222
from dateutil.parser import parse
2323

24+
from appium import webdriver
2425
from appium.webdriver.applicationstate import ApplicationState
26+
from appium.webdriver.common.mobileby import MobileBy
2527
from selenium.common.exceptions import NoSuchElementException
26-
from selenium.webdriver.common.by import By
27-
from selenium.webdriver.support import expected_conditions as EC
28-
from selenium.webdriver.support.ui import WebDriverWait
2928

30-
from appium import webdriver
3129
import desired_capabilities
30+
from helper.test_helper import wait_for_element
3231

3332

3433
# the emulator is sometimes slow and needs time to think
@@ -49,7 +48,7 @@ def tearDown(self):
4948

5049
def test_screen_record(self):
5150
self.driver.start_recording_screen(timeLimit=10, forcedRestart=True)
52-
sleep(5)
51+
sleep(10)
5352
result = self.driver.stop_recording_screen()
5453
self.assertTrue(len(result) > 0)
5554

@@ -196,31 +195,22 @@ def test_open_notifications(self):
196195
def test_set_text(self):
197196
self.driver.find_element_by_android_uiautomator(
198197
'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("Views").instance(0));').click()
199-
WebDriverWait(self.driver, SLEEPY_TIME).until(
200-
EC.presence_of_element_located((By.ACCESSIBILITY_ID, 'Controls'))
201-
)
202-
self.driver.find_element_by_accessibility_id('Controls').click()
203-
204-
WebDriverWait(self.driver, SLEEPY_TIME).until(
205-
EC.presence_of_element_located((By.ACCESSIBILITY_ID, '1. Light Theme'))
206-
)
207-
self.driver.find_element_by_accessibility_id('1. Light Theme').click()
208-
209-
WebDriverWait(self.driver, SLEEPY_TIME).until(
210-
EC.presence_of_element_located((By.CLASS_NAME, 'android.widget.EditText'))
211-
)
212-
el = self.driver.find_element_by_class_name('android.widget.EditText')
198+
199+
wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, 'Controls', SLEEPY_TIME).click()
200+
wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, '1. Light Theme', SLEEPY_TIME).click()
201+
202+
el = wait_for_element(self.driver, MobileBy.CLASS_NAME, 'android.widget.EditText', SLEEPY_TIME)
213203
el.send_keys('original text')
214204
el.set_text('new text')
215205

216206
self.assertEqual('new text', el.text)
217207

218208
def test_send_keys(self):
219-
self.driver.find_element_by_xpath("//android.widget.TextView[@text='App']").click()
220-
self.driver.find_element_by_xpath("//android.widget.TextView[@text='Activity']").click()
221-
self.driver.find_element_by_xpath("//android.widget.TextView[@text='Custom Title']").click()
209+
for text in ['App', 'Activity', 'Custom Title']:
210+
wait_for_element(self.driver, MobileBy.XPATH,
211+
"//android.widget.TextView[@text='{}']".format(text), SLEEPY_TIME).click()
222212

223-
el = self.driver.find_element(By.ID, 'com.example.android.apis:id/left_text_edit')
213+
el = self.driver.find_element(MobileBy.ID, 'com.example.android.apis:id/left_text_edit')
224214
el.send_keys(' text')
225215

226216
self.assertEqual('Left is best text', el.text)
@@ -236,8 +226,8 @@ def test_start_activity_other_app(self):
236226
self.driver.start_activity("com.example.android.apis", ".ApiDemos")
237227
self._assert_activity_contains('Demos')
238228

239-
self.driver.start_activity("com.android.contacts", ".ContactsListActivity")
240-
self._assert_activity_contains('Contact')
229+
self.driver.start_activity("com.android.calculator2", ".Calculator")
230+
self._assert_activity_contains('Calculator')
241231

242232
def _assert_activity_contains(self, activity):
243233
current = self.driver.current_activity
@@ -248,9 +238,9 @@ def test_get_settings(self):
248238
self.assertIsNotNone(settings)
249239

250240
def test_update_settings(self):
251-
self.driver.update_settings({"cyberdelia": "open"})
241+
self.driver.update_settings({"waitForIdleTimeout": 10001})
252242
settings = self.driver.get_settings()
253-
self.assertEqual(settings["cyberdelia"], "open")
243+
self.assertEqual(settings["waitForIdleTimeout"], 10001)
254244

255245
def test_toggle_location_services(self):
256246
self.driver.toggle_location_services()

test/functional/android/helper/__init__.py

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.support import expected_conditions as EC
16+
from selenium.webdriver.support.ui import WebDriverWait
17+
18+
SLEEPY_TIME = 3
19+
20+
21+
def wait_for_element(driver, locator, value, timeout=SLEEPY_TIME):
22+
'''
23+
Wait until the element located
24+
25+
:param driver: Webdriver
26+
:param locator: Locator like WebDriver, Mobile JSON Wire Protocol (e.g. MobileBy.ACCESSIBILITY_ID)
27+
:param value: Query value to locator
28+
:param timeout: Maximum time to wait the element. If time is over, TimeoutException is thrown
29+
:return: Webelement
30+
'''
31+
return WebDriverWait(driver, timeout).until(
32+
EC.presence_of_element_located((locator, value))
33+
)

0 commit comments

Comments
 (0)