From ca0206f696cce33e32c378b70922bc41002dc5db Mon Sep 17 00:00:00 2001 From: Atsushi Mori Date: Tue, 7 May 2019 21:14:48 +0900 Subject: [PATCH 1/6] Fix test failed: test_send_keys, test_screen_record --- test/functional/android/appium_tests.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/test/functional/android/appium_tests.py b/test/functional/android/appium_tests.py index e6843a39..b8d353eb 100644 --- a/test/functional/android/appium_tests.py +++ b/test/functional/android/appium_tests.py @@ -21,14 +21,16 @@ 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 import wait_element # the emulator is sometimes slow and needs time to think @@ -49,7 +51,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) @@ -216,9 +218,17 @@ def test_set_text(self): 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() + xpath = "//android.widget.TextView[@text='App']" + wait_element(self.driver, MobileBy.XPATH, xpath, SLEEPY_TIME) + self.driver.find_element_by_xpath(xpath).click() + + xpath = "//android.widget.TextView[@text='Activity']" + wait_element(self.driver, MobileBy.XPATH, xpath, SLEEPY_TIME) + self.driver.find_element_by_xpath(xpath).click() + + xpath = "//android.widget.TextView[@text='Custom Title']" + wait_element(self.driver, MobileBy.XPATH, xpath, SLEEPY_TIME) + self.driver.find_element_by_xpath(xpath).click() el = self.driver.find_element(By.ID, 'com.example.android.apis:id/left_text_edit') el.send_keys(' text') From 120d1ac0882e5f6a7b04bc6a720bab69c4bf5205 Mon Sep 17 00:00:00 2001 From: Atsushi Mori Date: Tue, 7 May 2019 21:27:54 +0900 Subject: [PATCH 2/6] Fix test failed: test_update_settings --- test/functional/android/appium_tests.py | 22 ++++++----------- test/functional/android/helper.py | 32 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 test/functional/android/helper.py diff --git a/test/functional/android/appium_tests.py b/test/functional/android/appium_tests.py index b8d353eb..6b2abff1 100644 --- a/test/functional/android/appium_tests.py +++ b/test/functional/android/appium_tests.py @@ -25,9 +25,6 @@ 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 import desired_capabilities from helper import wait_element @@ -198,19 +195,14 @@ 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')) - ) + + wait_element(self.driver, MobileBy.ACCESSIBILITY_ID, 'Controls', SLEEPY_TIME) 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')) - ) + wait_element(self.driver, MobileBy.ACCESSIBILITY_ID, '1. Light Theme', SLEEPY_TIME) 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')) - ) + wait_element(self.driver, MobileBy.CLASS_NAME, 'android.widget.EditText', SLEEPY_TIME) el = self.driver.find_element_by_class_name('android.widget.EditText') el.send_keys('original text') el.set_text('new text') @@ -230,7 +222,7 @@ def test_send_keys(self): wait_element(self.driver, MobileBy.XPATH, xpath, SLEEPY_TIME) self.driver.find_element_by_xpath(xpath).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) @@ -258,9 +250,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.py b/test/functional/android/helper.py new file mode 100644 index 00000000..0728670b --- /dev/null +++ b/test/functional/android/helper.py @@ -0,0 +1,32 @@ +#!/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_element(driver, locator, value, timeout=SLEEPY_TIME): + ''' + Wait until the element shows up + :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: None + ''' + WebDriverWait(driver, timeout).until( + EC.presence_of_element_located((locator, value)) + ) From 5489f11b0dcabfe8cad2628ccf0c8ff09e500c13 Mon Sep 17 00:00:00 2001 From: Atsushi Mori Date: Tue, 7 May 2019 22:11:43 +0900 Subject: [PATCH 3/6] Fix test failed: test_start_activity_other_app --- test/functional/android/appium_tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/functional/android/appium_tests.py b/test/functional/android/appium_tests.py index 6b2abff1..9a51000d 100644 --- a/test/functional/android/appium_tests.py +++ b/test/functional/android/appium_tests.py @@ -238,8 +238,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 From a9f410ce0c73f828109ae3758495a0f097117de0 Mon Sep 17 00:00:00 2001 From: Atsushi Mori Date: Tue, 7 May 2019 23:15:38 +0900 Subject: [PATCH 4/6] Move and rename helper package --- test/functional/android/appium_tests.py | 2 +- test/functional/android/helper/__init__.py | 0 test/functional/android/{helper.py => helper/test_helper.py} | 1 + 3 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 test/functional/android/helper/__init__.py rename test/functional/android/{helper.py => helper/test_helper.py} (99%) diff --git a/test/functional/android/appium_tests.py b/test/functional/android/appium_tests.py index 9a51000d..596c4214 100644 --- a/test/functional/android/appium_tests.py +++ b/test/functional/android/appium_tests.py @@ -27,7 +27,7 @@ from selenium.common.exceptions import NoSuchElementException import desired_capabilities -from helper import wait_element +from helper.test_helper import wait_element # the emulator is sometimes slow and needs time to think 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.py b/test/functional/android/helper/test_helper.py similarity index 99% rename from test/functional/android/helper.py rename to test/functional/android/helper/test_helper.py index 0728670b..db42a381 100644 --- a/test/functional/android/helper.py +++ b/test/functional/android/helper/test_helper.py @@ -21,6 +21,7 @@ def wait_element(driver, locator, value, timeout=SLEEPY_TIME): ''' Wait until the element shows up + :param driver: Webdriver :param locator: Locator like WebDriver, Mobile JSON Wire Protocol (e.g. MobileBy.ACCESSIBILITY_ID) :param value: Query value to locator From 903e68dd7c713bd246fa0d265301ae741df2d08b Mon Sep 17 00:00:00 2001 From: Atsushi Mori Date: Wed, 8 May 2019 23:02:42 +0900 Subject: [PATCH 5/6] Update along to review comments --- test/functional/android/appium_tests.py | 23 +++++++------------ test/functional/android/helper/test_helper.py | 4 ++-- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/test/functional/android/appium_tests.py b/test/functional/android/appium_tests.py index 596c4214..5ba3c91d 100644 --- a/test/functional/android/appium_tests.py +++ b/test/functional/android/appium_tests.py @@ -27,7 +27,7 @@ from selenium.common.exceptions import NoSuchElementException import desired_capabilities -from helper.test_helper import wait_element +from helper.test_helper import wait_for_element # the emulator is sometimes slow and needs time to think @@ -196,13 +196,13 @@ 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() - wait_element(self.driver, MobileBy.ACCESSIBILITY_ID, 'Controls', SLEEPY_TIME) + wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, 'Controls', SLEEPY_TIME) self.driver.find_element_by_accessibility_id('Controls').click() - wait_element(self.driver, MobileBy.ACCESSIBILITY_ID, '1. Light Theme', SLEEPY_TIME) + wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, '1. Light Theme', SLEEPY_TIME) self.driver.find_element_by_accessibility_id('1. Light Theme').click() - wait_element(self.driver, MobileBy.CLASS_NAME, 'android.widget.EditText', SLEEPY_TIME) + wait_for_element(self.driver, MobileBy.CLASS_NAME, 'android.widget.EditText', SLEEPY_TIME) el = self.driver.find_element_by_class_name('android.widget.EditText') el.send_keys('original text') el.set_text('new text') @@ -210,17 +210,10 @@ def test_set_text(self): self.assertEqual('new text', el.text) def test_send_keys(self): - xpath = "//android.widget.TextView[@text='App']" - wait_element(self.driver, MobileBy.XPATH, xpath, SLEEPY_TIME) - self.driver.find_element_by_xpath(xpath).click() - - xpath = "//android.widget.TextView[@text='Activity']" - wait_element(self.driver, MobileBy.XPATH, xpath, SLEEPY_TIME) - self.driver.find_element_by_xpath(xpath).click() - - xpath = "//android.widget.TextView[@text='Custom Title']" - wait_element(self.driver, MobileBy.XPATH, xpath, SLEEPY_TIME) - self.driver.find_element_by_xpath(xpath).click() + for text in ['App', 'Activity', 'Custom Title']: + xpath = "//android.widget.TextView[@text='{}']".format(text) + wait_for_element(self.driver, MobileBy.XPATH, xpath, SLEEPY_TIME) + self.driver.find_element_by_xpath(xpath).click() el = self.driver.find_element(MobileBy.ID, 'com.example.android.apis:id/left_text_edit') el.send_keys(' text') diff --git a/test/functional/android/helper/test_helper.py b/test/functional/android/helper/test_helper.py index db42a381..0921cfd4 100644 --- a/test/functional/android/helper/test_helper.py +++ b/test/functional/android/helper/test_helper.py @@ -18,9 +18,9 @@ SLEEPY_TIME = 3 -def wait_element(driver, locator, value, timeout=SLEEPY_TIME): +def wait_for_element(driver, locator, value, timeout=SLEEPY_TIME): ''' - Wait until the element shows up + Wait until the element located :param driver: Webdriver :param locator: Locator like WebDriver, Mobile JSON Wire Protocol (e.g. MobileBy.ACCESSIBILITY_ID) From 45025b7d48fe50b7b4d943a3fbb91be1c835fb64 Mon Sep 17 00:00:00 2001 From: Atsushi Mori Date: Thu, 9 May 2019 19:49:40 +0900 Subject: [PATCH 6/6] Add return value to wait_for_element --- test/functional/android/appium_tests.py | 15 +++++---------- test/functional/android/helper/test_helper.py | 4 ++-- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/test/functional/android/appium_tests.py b/test/functional/android/appium_tests.py index 5ba3c91d..ea4aa033 100644 --- a/test/functional/android/appium_tests.py +++ b/test/functional/android/appium_tests.py @@ -196,14 +196,10 @@ 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() - wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, 'Controls', SLEEPY_TIME) - self.driver.find_element_by_accessibility_id('Controls').click() + 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() - wait_for_element(self.driver, MobileBy.ACCESSIBILITY_ID, '1. Light Theme', SLEEPY_TIME) - self.driver.find_element_by_accessibility_id('1. Light Theme').click() - - wait_for_element(self.driver, MobileBy.CLASS_NAME, 'android.widget.EditText', SLEEPY_TIME) - el = self.driver.find_element_by_class_name('android.widget.EditText') + el = wait_for_element(self.driver, MobileBy.CLASS_NAME, 'android.widget.EditText', SLEEPY_TIME) el.send_keys('original text') el.set_text('new text') @@ -211,9 +207,8 @@ def test_set_text(self): def test_send_keys(self): for text in ['App', 'Activity', 'Custom Title']: - xpath = "//android.widget.TextView[@text='{}']".format(text) - wait_for_element(self.driver, MobileBy.XPATH, xpath, SLEEPY_TIME) - self.driver.find_element_by_xpath(xpath).click() + wait_for_element(self.driver, MobileBy.XPATH, + "//android.widget.TextView[@text='{}']".format(text), SLEEPY_TIME).click() el = self.driver.find_element(MobileBy.ID, 'com.example.android.apis:id/left_text_edit') el.send_keys(' text') diff --git a/test/functional/android/helper/test_helper.py b/test/functional/android/helper/test_helper.py index 0921cfd4..ef919cfa 100644 --- a/test/functional/android/helper/test_helper.py +++ b/test/functional/android/helper/test_helper.py @@ -26,8 +26,8 @@ def wait_for_element(driver, locator, value, timeout=SLEEPY_TIME): :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: None + :return: Webelement ''' - WebDriverWait(driver, timeout).until( + return WebDriverWait(driver, timeout).until( EC.presence_of_element_located((locator, value)) )