Skip to content

Commit 868caa9

Browse files
authored
Fix functional tests failed (appium#364)
* Fix test failed: element_location_in_view, set_text * Fix test failed: test_push_file * Merge test_pull_test into test_push_test * Fix test failed: test_pull_folder * Enable running by both py2 and py3 * Removed unnecessary codes * Remove magic number
1 parent 9571962 commit 868caa9

1 file changed

Lines changed: 31 additions & 21 deletions

File tree

test/functional/android/appium_tests.py

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
import base64
1617
import unittest
1718
from zipfile import ZipFile
18-
import json
1919
import os
2020
import random
2121
from time import sleep
@@ -24,13 +24,15 @@
2424
from appium.webdriver.applicationstate import ApplicationState
2525
from selenium.common.exceptions import NoSuchElementException
2626
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
2729

2830
from appium import webdriver
2931
import desired_capabilities
3032

3133

3234
# the emulator is sometimes slow and needs time to think
33-
SLEEPY_TIME = 1
35+
SLEEPY_TIME = 3
3436

3537

3638
class AppiumTests(unittest.TestCase):
@@ -98,31 +100,29 @@ def test_current_package(self):
98100
package = self.driver.current_package
99101
self.assertEqual('com.example.android.apis', package)
100102

101-
def test_pull_file(self):
102-
data = self.driver.pull_file('data/local/tmp/strings.json')
103-
strings = json.loads(data.decode('base64', 'strict'))
104-
self.assertEqual('You can\'t wipe my data, you are a monkey!', strings[u'monkey_wipe_data'])
103+
def test_push_pull_file(self):
104+
path = '/data/local/tmp/test_push_file.txt'
105+
data = b'This is the contents of the file to push to the device.'
106+
107+
self.driver.push_file(path, base64.b64encode(data).decode('utf-8'))
108+
data_ret = base64.b64decode(self.driver.pull_file(path))
105109

106-
def test_push_file(self):
107-
path = 'data/local/tmp/test_push_file.txt'
108-
data = 'This is the contents of the file to push to the device.'
109-
self.driver.push_file(path, data.encode('base64'))
110-
data_ret = self.driver.pull_file('data/local/tmp/test_push_file.txt').decode('base64')
111110
self.assertEqual(data, data_ret)
112111

113112
def test_pull_folder(self):
114-
string_data = 'random string data %d' % random.randint(0, 1000)
113+
string_data = b'random string data %d' % random.randint(0, 1000)
115114
path = '/data/local/tmp'
116-
self.driver.push_file(path + '/1.txt', string_data.encode('base64'))
117-
self.driver.push_file(path + '/2.txt', string_data.encode('base64'))
115+
116+
self.driver.push_file(path + '/1.txt', base64.b64encode(string_data).decode('utf-8'))
117+
self.driver.push_file(path + '/2.txt', base64.b64encode(string_data).decode('utf-8'))
118+
118119
folder = self.driver.pull_folder(path)
119120

120121
# python doesn't have any functionality for unzipping streams
121122
# save temporary file, which will be deleted in `tearDown`
122123
self.zipfilename = 'folder_%d.zip' % random.randint(0, 1000000)
123-
file = open(self.zipfilename, "w")
124-
file.write(folder.decode('base64', 'strict'))
125-
file.close()
124+
with open(self.zipfilename, "wb") as fw:
125+
fw.write(base64.b64decode(folder))
126126

127127
with ZipFile(self.zipfilename, 'r') as myzip:
128128
# should find these. otherwise it will raise a `KeyError`
@@ -196,9 +196,19 @@ def test_open_notifications(self):
196196
def test_set_text(self):
197197
self.driver.find_element_by_android_uiautomator(
198198
'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("Views").instance(0));').click()
199-
self.driver.find_element_by_name('Controls').click()
200-
self.driver.find_element_by_name('1. Light Theme').click()
201-
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+
)
202212
el = self.driver.find_element_by_class_name('android.widget.EditText')
203213
el.send_keys('original text')
204214
el.set_text('new text')
@@ -246,7 +256,7 @@ def test_toggle_location_services(self):
246256
self.driver.toggle_location_services()
247257

248258
def test_element_location_in_view(self):
249-
el = self.driver.find_element_by_name('Content')
259+
el = self.driver.find_element_by_accessibility_id('Content')
250260
loc = el.location_in_view
251261
self.assertIsNotNone(loc['x'])
252262
self.assertIsNotNone(loc['y'])

0 commit comments

Comments
 (0)