-
Notifications
You must be signed in to change notification settings - Fork 741
/
EarlGreyExampleSwiftTests.swift
254 lines (231 loc) · 9.68 KB
/
EarlGreyExampleSwiftTests.swift
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//
// Copyright 2016 Google Inc.
//
// 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.
//
import EarlGrey
import XCTest
@testable import EarlGreyExampleSwift
class EarlGreyExampleSwiftTests: XCTestCase {
func testBasicSelection() {
// Select the button with Accessibility ID "clickMe".
// This should throw a warning for "Result of Call Unused."
EarlGrey.selectElement(with: grey_accessibilityID("ClickMe"))
}
func testBasicSelectionAndAction() {
// Select and tap the button with Accessibility ID "clickMe".
EarlGrey.selectElement(with: grey_accessibilityID("ClickMe"))
.perform(grey_tap())
}
func testBasicSelectionAndAssert() {
// Select the button with Accessibility ID "clickMe" and assert it's visible.
EarlGrey.selectElement(with: grey_accessibilityID("ClickMe"))
.assert(grey_sufficientlyVisible())
}
func testBasicSelectionActionAssert() {
// Select and tap the button with Accessibility ID "clickMe", then assert it's visible.
EarlGrey.selectElement(with: grey_accessibilityID("ClickMe"))
.perform(grey_tap())
.assert(grey_sufficientlyVisible())
}
func testSelectionOnMultipleElements() {
// This test will fail because both buttons are visible and match the selection.
// We add a custom error here to prevent the Test Suite failing.
var error: NSError?
EarlGrey.selectElement(with: grey_text("Non-Existent Element Text"))
.perform(grey_tap(), error: &error)
if let _ = error {
print("Test Failed with Error : \(error.self!)")
}
}
func testCollectionMatchers() {
// First way to disambiguate: use collection matchers.
let visibleSendButtonMatcher: GREYMatcher! =
grey_allOf([grey_accessibilityID("ClickMe"), grey_sufficientlyVisible()])
EarlGrey.selectElement(with: visibleSendButtonMatcher)
.perform(grey_doubleTap())
}
func testWithInRoot() {
// Second way to disambiguate: use inRoot to focus on a specific window or container.
// There are two buttons with accessibility id "Send", but only one is inside SendMessageView.
EarlGrey.selectElement(with: grey_accessibilityID("Send"))
.inRoot(grey_kindOfClass(SendMessageView.self))
.perform(grey_doubleTap())
}
func testWithCustomMatcher() {
// Define the match condition: matches table cells that contains a date for a Thursday.
let matches: MatchesBlock = { (element: Any?) -> Bool in
if let cell = element as? UITableViewCell {
let formatter = DateFormatter()
formatter.dateStyle = DateFormatter.Style.long
if let date = formatter.date(from: cell.textLabel!.text!) {
let calendar = Calendar.current
let weekday = (calendar as NSCalendar).component(NSCalendar.Unit.weekday, from: date)
return weekday == 5
} else {
return false
}
} else {
return false
}
}
// Create a description for the matcher.
let describe: DescribeToBlock = { (description: Any) -> Void in
let greyDescription:GREYDescription = description as! GREYDescription
greyDescription.appendText("Date for a Thursday")
}
// Create an EarlGrey custom matcher.
let matcherForThursday: GREYElementMatcherBlock! =
GREYElementMatcherBlock.init(matchesBlock: matches, descriptionBlock: describe)
// Profit
EarlGrey.selectElement(with: matcherForThursday)
.perform(grey_doubleTap())
}
func testTableCellOutOfScreen() {
// Go find one cell out of the screen.
EarlGrey.selectElement(with: grey_accessibilityID("Cell30"))
.usingSearch(grey_scrollInDirection(GREYDirection.down, 100),
onElementWith: grey_accessibilityID("table"))
.perform(grey_tap())
// Move back to top of the table.
EarlGrey.selectElement(with: grey_accessibilityID("Cell1"))
.usingSearch(grey_scrollInDirection(GREYDirection.up, 500),
onElementWith: grey_accessibilityID("table"))
.perform(grey_doubleTap())
}
func testCatchErrorOnFailure() {
// TapMe doesn't exist, but the test doesn't fail because we are getting a pointer to the
// error.
var error: NSError?
EarlGrey.selectElement(with: grey_accessibilityID("TapMe"))
.perform(grey_tap(), error: &error)
if let myError = error {
print(myError)
}
}
func testCustomAction() {
// Fade in and out an element.
let fadeInAndOut = { (element: UIView) -> Void in
UIView.animate(withDuration: 1.0, delay: 0.0,
options: UIViewAnimationOptions.curveEaseOut,
animations: { element.alpha = 0.0 }, completion: {
(finished: Bool) -> Void in
UIView.animate(withDuration: 1.0,
delay: 0.0,
options: UIViewAnimationOptions.curveEaseIn,
animations: { element.alpha = 1.0 },
completion: nil)
})
}
// Define a custom action that applies fadeInAndOut to the selected element.
let tapClickMe: GREYActionBlock =
GREYActionBlock.action(withName: "Fade In And Out", perform: {
(element: Any?, errorOrNil: UnsafeMutablePointer<NSError?>?) -> Bool in
// First make sure element is attached to a window.
let elementAsView :UIView = element as! UIView
guard let window = elementAsView.window! as UIView! else {
let errorInfo = [NSLocalizedDescriptionKey:
NSLocalizedString("Element is not attached to a window",
comment: "")]
errorOrNil?.pointee = NSError(domain: kGREYInteractionErrorDomain,
code: 1,
userInfo: errorInfo)
return false
}
fadeInAndOut(window)
return true
});
EarlGrey.selectElement(with: grey_accessibilityID("ClickMe"))
.perform(tapClickMe)
}
func testWithCustomAssertion() {
// Write a custom assertion that checks if the alpha of an element is equal to the expected
// value.
let alphaEqual = { (expectedAlpha: CGFloat) -> GREYAssertionBlock in
return GREYAssertionBlock.assertion(withName: "Assert Alpha Equal",
assertionBlockWithError: {
(element: Any?, errorOrNil: UnsafeMutablePointer<NSError?>?) -> Bool in
guard let view = element! as! UIView as UIView! else {
let errorInfo = [NSLocalizedDescriptionKey:
NSLocalizedString("Element is not a UIView",
comment: "")]
errorOrNil?.pointee =
NSError(domain: kGREYInteractionErrorDomain,
code: 2,
userInfo: errorInfo)
return false
}
return view.alpha == expectedAlpha
})
}
EarlGrey.selectElement(with: grey_accessibilityID("ClickMe"))
.assert(alphaEqual(1.0))
}
func testWithCustomFailureHandler() {
// This test will fail and use our custom handler to handle the failure.
// The custom handler is defined at the end of this file.
let myHandler = SampleFailureHandler()
EarlGrey.setFailureHandler(handler: myHandler)
EarlGrey.selectElement(with: grey_accessibilityID("TapMe"))
.perform(grey_tap())
}
func testLayout() {
// Define a layout constraint.
let onTheRight: GREYLayoutConstraint =
GREYLayoutConstraint(attribute: GREYLayoutAttribute.left,
relatedBy: GREYLayoutRelation.greaterThanOrEqual,
toReferenceAttribute: GREYLayoutAttribute.right,
multiplier: 1.0,
constant: 0.0)
EarlGrey.selectElement(with: grey_accessibilityLabel("SendForLayoutTest"))
.assert(grey_layout([onTheRight], grey_accessibilityID("ClickMe")))
}
func testWithCondition() {
let myCondition = GREYCondition.init(name: "Example condition", block: { () -> Bool in
for j in 0...100000 {
_ = j
}
return true
})
// Wait for my condition to be satisfied or timeout after 5 seconds.
let success = myCondition.wait(withTimeout: 5)
if !success {
// Just printing for the example.
print("Condition not met")
} else {
EarlGrey.selectElement(with: grey_accessibilityID("ClickMe"))
.perform(grey_tap())
}
}
func testWithGreyAssertions() {
GREYAssert(1 == 1, reason: "Assert with GREYAssert")
GREYAssertTrue(1 == 1, reason: "Assert with GREYAssertTrue")
GREYAssertFalse(1 != 1, reason: "Assert with GREYAssertFalse")
GREYAssertNotNil(1, reason: "Assert with GREYAssertNotNil")
GREYAssertNil(nil, reason: "Assert with GREYAssertNil")
GREYAssertEqualObjects(1, 1, reason: "Assert with GREYAssertEqualObjects")
// Uncomment one of the following lines to fail the test.
//GREYFail("Failing with GREYFail")
}
}
class SampleFailureHandler : NSObject, GREYFailureHandler {
/**
* Called by the framework to raise an exception.
*
* @param exception The exception to be handled.
* @param details Extra information about the failure.
*/
public func handle(_ exception: GREYFrameworkException!, details: String!) {
print("Test Failed With Reason : \(exception.reason!) and details \(details)")
}
}