diff --git a/test/functional/android/appium_tests.py b/test/functional/android/appium_tests.py index e6843a39..ea4aa033 100644 --- a/test/functional/android/appium_tests.py +++ b/test/functional/android/appium_tests.py @@ -21,14 +21,13 @@ from time import sleep from dateutil.parser import parse +from appium import webdriver from appium.webdriver.applicationstate import ApplicationState +from appium.webdriver.common.mobileby import MobileBy from selenium.common.exceptions import NoSuchElementException -from selenium.webdriver.common.by import By -from selenium.webdriver.support import expected_conditions as EC -from selenium.webdriver.support.ui import WebDriverWait -from appium import webdriver import desired_capabilities +from helper.test_helper import wait_for_element # the emulator is sometimes slow and needs time to think @@ -49,7 +48,7 @@ def tearDown(self): def test_screen_record(self): self.driver.start_recording_screen(timeLimit=10, forcedRestart=True) - sleep(5) + sleep(10) result = self.driver.stop_recording_screen() self.assertTrue(len(result) > 0) @@ -196,31 +195,22 @@ def test_open_notifications(self): def test_set_text(self): self.driver.find_element_by_android_uiautomator( 'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("Views").instance(0));').click() - WebDriverWait(self.driver, SLEEPY_TIME).until( - EC.presence_of_element_located((By.ACCESSIBILITY_ID, 'Controls')) - ) - self.driver.find_element_by_accessibility_id('Controls').click() - - WebDriverWait(self.driver, SLEEPY_TIME).until( - EC.presence_of_element_located((By.ACCESSIBILITY_ID, '1. Light Theme')) - ) - self.driver.find_element_by_accessibility_id('1. Light Theme').click() - - WebDriverWait(self.driver, SLEEPY_TIME).until( - EC.presence_of_element_located((By.CLASS_NAME, 'android.widget.EditText')) - ) - el = self.driver.find_element_by_class_name('android.widget.EditText') + + wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, 'Controls', SLEEPY_TIME).click() + wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, '1. Light Theme', SLEEPY_TIME).click() + + el = wait_for_element(self.driver, MobileBy.CLASS_NAME, 'android.widget.EditText', SLEEPY_TIME) el.send_keys('original text') el.set_text('new text') self.assertEqual('new text', el.text) def test_send_keys(self): - self.driver.find_element_by_xpath("//android.widget.TextView[@text='App']").click() - self.driver.find_element_by_xpath("//android.widget.TextView[@text='Activity']").click() - self.driver.find_element_by_xpath("//android.widget.TextView[@text='Custom Title']").click() + for text in ['App', 'Activity', 'Custom Title']: + wait_for_element(self.driver, MobileBy.XPATH, + "//android.widget.TextView[@text='{}']".format(text), SLEEPY_TIME).click() - el = self.driver.find_element(By.ID, 'com.example.android.apis:id/left_text_edit') + el = self.driver.find_element(MobileBy.ID, 'com.example.android.apis:id/left_text_edit') el.send_keys(' text') self.assertEqual('Left is best text', el.text) @@ -236,8 +226,8 @@ def test_start_activity_other_app(self): self.driver.start_activity("com.example.android.apis", ".ApiDemos") self._assert_activity_contains('Demos') - self.driver.start_activity("com.android.contacts", ".ContactsListActivity") - self._assert_activity_contains('Contact') + self.driver.start_activity("com.android.calculator2", ".Calculator") + self._assert_activity_contains('Calculator') def _assert_activity_contains(self, activity): current = self.driver.current_activity @@ -248,9 +238,9 @@ def test_get_settings(self): self.assertIsNotNone(settings) def test_update_settings(self): - self.driver.update_settings({"cyberdelia": "open"}) + self.driver.update_settings({"waitForIdleTimeout": 10001}) settings = self.driver.get_settings() - self.assertEqual(settings["cyberdelia"], "open") + self.assertEqual(settings["waitForIdleTimeout"], 10001) def test_toggle_location_services(self): self.driver.toggle_location_services() diff --git a/test/functional/android/helper/__init__.py b/test/functional/android/helper/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test/functional/android/helper/test_helper.py b/test/functional/android/helper/test_helper.py new file mode 100644 index 00000000..ef919cfa --- /dev/null +++ b/test/functional/android/helper/test_helper.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from selenium.webdriver.support import expected_conditions as EC +from selenium.webdriver.support.ui import WebDriverWait + +SLEEPY_TIME = 3 + + +def wait_for_element(driver, locator, value, timeout=SLEEPY_TIME): + ''' + Wait until the element located + + :param driver: Webdriver + :param locator: Locator like WebDriver, Mobile JSON Wire Protocol (e.g. MobileBy.ACCESSIBILITY_ID) + :param value: Query value to locator + :param timeout: Maximum time to wait the element. If time is over, TimeoutException is thrown + :return: Webelement + ''' + return WebDriverWait(driver, timeout).until( + EC.presence_of_element_located((locator, value)) + )