forked from appium-boneyard/sample-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathios_complex.py
More file actions
185 lines (145 loc) · 6.61 KB
/
ios_complex.py
File metadata and controls
185 lines (145 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""
More involved iOS tests, using UICatalog application.
"""
import unittest
import os
import random
import string
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
# from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.touch_actions import TouchActions
# from selenium.webdriver.common.keys import Keys
import urllib2
import json
from time import sleep
def str_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for x in range(size))
class ComplexIOSTests(unittest.TestCase):
def setUp(self):
# set up appium
# ** Important Note **
# Make sure you have build the UICatalog applcation in your local repository
app = os.path.join(os.path.dirname(__file__),
'../../apps/UICatalog.zip')
app = os.path.abspath(app)
self.driver = webdriver.Remote(
command_executor='http://127.0.0.1:4723/wd/hub',
desired_capabilities={
'app': app,
'platformName': 'iOS',
'platformVersion': '10.0',
'deviceName': 'iPhone 6'
})
self._values = []
def tearDown(self):
self.driver.quit()
def _open_menu_position(self, index):
# Opens up the menu at position [index] : starts at 0.
table = self.driver.find_element_by_class_name("XCUIElementTypeTable")
self._row = table.find_elements_by_class_name("XCUIElementTypeTableCell")[index]
self._row.click()
def test_find_element(self):
# first view in UICatalog is a table
table = self.driver.find_element_by_class_name("XCUIElementTypeTable")
self.assertIsNotNone(table)
# is number of cells/rows inside table correct
rows = table.find_elements_by_class_name("XCUIElementTypeTableCell")
self.assertEqual(18, len(rows))
# is first one about buttons
self.assertEqual(rows[0].get_attribute("name"), "Action Sheets")
# there is nav bar inside the app
nav_bar = self.driver.find_element_by_class_name("XCUIElementTypeNavigationBar")
self.assertTrue(nav_bar)
def test_frames(self):
# go into webview frame
self._open_menu_position(15)
# get the contexts and switch to webview
contexts = self.driver.contexts
self.assertEqual([u'NATIVE_APP', u'WEBVIEW_1'], contexts)
self.driver.switch_to.context(contexts[1])
# Find the store link
sleep(4) # let the page load, perhaps
logo = self.driver.find_element_by_xpath('//*/UIATextField[@value="http://apple.com"]')
self.assertIsNotNone(logo)
# leave the webview
self.driver.switch_to.context(contexts[0])
# Verify we are out of the webview
scroll_after = self.driver.find_element_by_class_name("XCUIElementTypeScrollView")
self.assertTrue(scroll_after)
def test_location(self):
# get third row location
row = self.driver.find_elements_by_class_name("XCUIElementTypeTableCell")[2]
self.assertEqual(row.location['x'], 0)
self.assertEqual(row.location['y'], 178.8125)
def test_screenshot(self):
# make screenshot and get is as base64
screenshot = self.driver.get_screenshot_as_base64()
self.assertTrue(screenshot)
# make screenshot and save it to the local filesystem
success = self.driver.get_screenshot_as_file("foo.png")
self.assertTrue(success)
self.assertTrue(os.path.isfile("foo.png"))
# get rid of the file
os.remove("foo.png")
def test_text_field_edit(self):
# go to the text fields section
self._open_menu_position(13)
text_field = self.driver.find_elements_by_class_name("XCUIElementTypeTextField")[0]
# get default/empty text
default_val = text_field.get_attribute("value")
# write some random text to element
rnd_string = str_generator()
text_field.send_keys(rnd_string)
self.assertEqual(text_field.get_attribute("value"), rnd_string)
# clear and check if is empty/has default text
text_field.clear()
self.assertEqual(text_field.get_attribute("value"), default_val)
def test_alert_interaction(self):
# go to the alerts section
self.driver.find_element_by_name('Alert Views').click()
triggerOk = self.driver.find_element_by_accessibility_id("Simple")
# TOFIX: Looks like alert object is not proper state
# something to do with UIActionSheet vs. UIAlertView?
# triggerOk = elements[0]
triggerOk.click()
alert = self.driver.switch_to_alert()
# dismiss alert
alert.accept()
# trigger modal alert with cancel & ok buttons
triggerOkCancel = self.driver.find_element_by_accessibility_id("Okay / Cancel")
triggerOkCancel.click()
alert = self.driver.switch_to_alert()
# check if title of alert is correct
self.assertEqual(alert.text, "A Short Title Is Best A message should be a short, complete sentence.")
alert.accept()
def test_slider(self):
# go to controls
self._open_menu_position(10)
# get the slider
slider = self.driver.find_element_by_class_name("XCUIElementTypeSlider")
self.assertEqual(slider.get_attribute("value"), "42%")
slider.set_value(0)
self.assertEqual(slider.get_attribute("value"), "0%")
def test_sessions(self):
data = json.loads(urllib2.urlopen("http://localhost:4723/wd/hub/sessions").read())
self.assertEqual(self.driver.session_id, data['sessionId'])
def test_size(self):
table = self.driver.find_element_by_class_name("XCUIElementTypeTable").size
row = self.driver.find_elements_by_class_name("XCUIElementTypeTableCell")[0].size
self.assertEqual(table['width'], row['width'])
self.assertNotEqual(table['height'], row['height'])
def test_source(self):
# get main view soruce
source_main = self.driver.page_source
self.assertIn("XCUIElementTypeTable", source_main)
self.assertIn("Text Fields", source_main)
# got to text fields section
self._open_menu_position(13)
sleep(10)
source_textfields = self.driver.page_source
self.assertIn("XCUIElementTypeStaticText", source_textfields)
self.assertIn("Text Fields", source_textfields)
self.assertNotEqual(source_main, source_textfields)
suite = unittest.TestLoader().loadTestsFromTestCase(ComplexIOSTests)
unittest.TextTestRunner(verbosity=2).run(suite)