-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathXcode_UI_Testing.rb
205 lines (177 loc) · 6.66 KB
/
Xcode_UI_Testing.rb
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
cheatsheet do
title 'Xcode UI Testing'
docset_file_name 'Xcode_UI_Testing'
keyword 'xctui'
source_url 'http://cheat.kapeli.com'
category do
id 'Basic Functionality'
entry do
name 'Testing if an element exists'
notes '
```swift
XCTAssert(app.staticTexts["Welcome"].exists)
```
'
end
entry do
name 'Testing if text with an ellipse exists'
notes '
A full text match will find an element even if the displayed text has an ellipse due to truncation.
```swift
let longNameCell = app.staticTexts["Adolph Blaine Charles David Earl Frederick Gerald Hubert Irvin John Kenneth Lloyd Martin Nero Oliver Paul Quincy Randolph Sherman Thomas Uncas Victor William Xerxes Yancy Wolfeschlegelsteinhausenbergerdorff, Senior"]
XCTAssert(longNameCell.exists) // displayed text is "Adolph Blaine Charles David Earl Freder..."
```
'
end
entry do
name 'Waiting for an element to appear'
notes '
Set up an expectation to use with XCTest. The predicate will wait until the element\'s `-exist` property is true.
```swift
let goLabel = self.app.staticTexts["Go!"]
XCTAssertFalse(goLabel.exists)
let exists = NSPredicate(format: "exists == true")
expectationForPredicate(exists, evaluatedWithObject: goLabel, handler: nil)
app.buttons["Ready, set..."].tap()
waitForExpectationsWithTimeout(5, handler: nil)
XCTAssert(goLabel.exists)
```
'
end
end
category do
id 'Interacting with System Controls'
entry do
name 'Tapping buttons'
notes '
Identify buttons by their accessibility label.
```swift
app.buttons["Add"].tap()
```
'
end
entry do
name 'Typing text'
notes '
First make sure the text field has focus by tapping on it.
```swift
let textField = app.textFields["Username"]
textField.tap()
textField.typeText("joemasilotti")
```
'
end
entry do
name 'Dismissing alerts'
notes '
```swift
app.alerts["Alert Title"].buttons["Button Title"].tap()
```
'
end
entry do
name 'Handling system alerts'
notes '
Present a location services authorization dialog to the user and dismiss it with the following code.
Before presenting the alert add a UI Interuption Handler. When this fires, dismiss with the "Allow" button.
```swift
addUIInterruptionMonitorWithDescription("Location Services") { (alert) -> Bool in
alert.buttons["Allow"].tap()
return true
}
app.buttons["Request Location"].tap()
app.tap() // need to interact with the app again for the handler to fire
```
'
end
entry do
name 'Sliding sliders'
notes '
This will slide the value of the slider to 70%.
```swift
app.sliders.element.adjustToNormalizedSliderPosition(0.7)
```
'
end
entry do
name 'Interacting with pickers'
notes '
A picker with one wheel:
```swift
app.pickerWheels.element.adjustToPickerWheelValue("Picker Wheel Item Title")
```
A picker with multiple wheels. Make sure to set the accessibility delegate so the framework can identify the different wheels.
```swift
let firstPredicate = NSPredicate(format: "label BEGINSWITH \'First Picker\'")
let firstPicker = app.pickerWheels.elementMatchingPredicate(firstPredicate)
firstPicker.adjustToPickerWheelValue("first value")
let secondPredicate = NSPredicate(format: "label BEGINSWITH \'Second Picker\'")
let secondPicker = app.pickerWheels.elementMatchingPredicate(secondPredicate)
secondPicker.adjustToPickerWheelValue("second value")
```
'
end
entry do
name 'Tapping links in web views'
notes '
```swift
app.links["Tweet this"].tap()
```
'
end
end
category do
id 'Interactions'
entry do
name 'Verifying the current controller\'s title'
notes '
```swift
XCTAssert(app.navigationBars["Details"].exists)
```
'
end
entry do
name 'Reordering table cells'
notes '
If you have a `UITableViewCell` with default style and set the text to "Title", the reorder control\'s accessibility label becomes "Reorder Title".
Using this we can drag one reorder control to another, essentially reordering the cells.
```swift
let topButton = app.buttons["Reorder Top Cell"]
let bottomButton = app.buttons["Reorder Bottom Cell"]
bottomButton.pressForDuration(0.5, thenDragToElement: topButton)
XCTAssertLessThanOrEqual(bottomButton.frame.maxY, topButton.frame.minY)
```
'
end
entry do
name 'Pull to refresh'
notes '
Create a `XCUICoordinate` from the first cell in your table and another one with a `dy` of six. Then drag the first coordinate to the second.
```swift
let firstCell = app.staticTexts["Adrienne"]
let start = firstCell.coordinateWithNormalizedOffset(CGVectorMake(0, 0))
let finish = firstCell.coordinateWithNormalizedOffset(CGVectorMake(0, 6))
start.pressForDuration(0, thenDragToCoordinate: finish)
```
'
end
entry do
name 'Pushing and popping view controllers'
notes '
Test if a view controller was pushed onto the navigation stack.
```swift
app.buttons["More Info"].tap()
XCTAssert(app.navigationBars["Volleyball?"].exists)
```
Pop a view controller by tapping the back button in the navigation bar and assert that the title in the navigation bar has changed.
```swift
app.navigationBars.buttons.elementBoundByIndex(0).tap()
XCTAssert(app.navigationBars["Volley"].exists)
```
'
end
end
notes <<-'END'
* Based on a [cheat sheet](https://github.com/joemasilotti/UI-Testing-Cheat-Sheet) by [joemasilotti](https://twitter.com/joemasilotti)
END
end