|
13 | 13 | # See the License for the specific language governing permissions and |
14 | 14 | # limitations under the License. |
15 | 15 |
|
| 16 | +import base64 |
16 | 17 | import unittest |
17 | 18 | from zipfile import ZipFile |
18 | | -import json |
19 | 19 | import os |
20 | 20 | import random |
21 | 21 | from time import sleep |
|
24 | 24 | from appium.webdriver.applicationstate import ApplicationState |
25 | 25 | from selenium.common.exceptions import NoSuchElementException |
26 | 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 |
27 | 29 |
|
28 | 30 | from appium import webdriver |
29 | 31 | import desired_capabilities |
30 | 32 |
|
31 | 33 |
|
32 | 34 | # the emulator is sometimes slow and needs time to think |
33 | | -SLEEPY_TIME = 1 |
| 35 | +SLEEPY_TIME = 3 |
34 | 36 |
|
35 | 37 |
|
36 | 38 | class AppiumTests(unittest.TestCase): |
@@ -98,31 +100,29 @@ def test_current_package(self): |
98 | 100 | package = self.driver.current_package |
99 | 101 | self.assertEqual('com.example.android.apis', package) |
100 | 102 |
|
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)) |
105 | 109 |
|
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') |
111 | 110 | self.assertEqual(data, data_ret) |
112 | 111 |
|
113 | 112 | 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) |
115 | 114 | 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 | + |
118 | 119 | folder = self.driver.pull_folder(path) |
119 | 120 |
|
120 | 121 | # python doesn't have any functionality for unzipping streams |
121 | 122 | # save temporary file, which will be deleted in `tearDown` |
122 | 123 | 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)) |
126 | 126 |
|
127 | 127 | with ZipFile(self.zipfilename, 'r') as myzip: |
128 | 128 | # should find these. otherwise it will raise a `KeyError` |
@@ -196,9 +196,19 @@ def test_open_notifications(self): |
196 | 196 | def test_set_text(self): |
197 | 197 | self.driver.find_element_by_android_uiautomator( |
198 | 198 | '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 | + ) |
202 | 212 | el = self.driver.find_element_by_class_name('android.widget.EditText') |
203 | 213 | el.send_keys('original text') |
204 | 214 | el.set_text('new text') |
@@ -246,7 +256,7 @@ def test_toggle_location_services(self): |
246 | 256 | self.driver.toggle_location_services() |
247 | 257 |
|
248 | 258 | 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') |
250 | 260 | loc = el.location_in_view |
251 | 261 | self.assertIsNotNone(loc['x']) |
252 | 262 | self.assertIsNotNone(loc['y']) |
|
0 commit comments