-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathUITest.swift
111 lines (87 loc) · 2.97 KB
/
UITest.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
//
// Copyright 2016 Novoda
//
// 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 XCTest
import EarlGrey
// This code allows a nicer API around [EarlGrey](https://github.com/google/EarlGrey).
// Let your `XCTest` class implement this protocol, and then use the protocol extensions. For example:
// `onView(with: .accessibilityLabel("doneButton")).tap()`
protocol UITest {
}
extension UITest {
func onView(with matchers: Matcher...) -> GREYElementInteraction {
let greyMatchers = matchers.map {
$0.function()
}
return EarlGrey.select(elementWithMatcher: grey_allOfMatchers(greyMatchers))
}
func onTabBarButton(with matcher: Matcher) -> GREYElementInteraction {
return onView(with: matcher)
.inRoot(grey_kindOfClass(UITabBar.classForCoder()))
}
}
struct Matcher {
fileprivate let function: ((Void) -> GREYMatcher)
static func accessibilityIdentifier(_ accessibilityIdentifier: String) -> Matcher {
return Matcher(function: {
return grey_accessibilityID(accessibilityIdentifier)
})
}
static func accessibilityLabel(_ accessibilityLabel: String) -> Matcher {
return Matcher(function: {
return grey_accessibilityLabel(accessibilityLabel)
})
}
static func type(_ classType: AnyClass) -> Matcher {
return Matcher(function: {
return grey_kindOfClass(classType)
})
}
static func title(_ title: String) -> Matcher {
return Matcher(function: {
return grey_buttonTitle(title)
})
}
static func text(_ text: String) -> Matcher {
return Matcher(function: {
return grey_text(text)
})
}
}
extension GREYElementInteraction {
func tap() {
perform(GREYActions.actionForTap())
}
func doubleTap() {
perform(GREYActions.actionForTap())
perform(GREYActions.actionForTap())
}
func swipeLeft() {
perform(GREYActions.actionForSwipeSlow(in: GREYDirection.left))
}
func assertIsVisible() {
assert(with: GREYMatchers.matcherForSufficientlyVisible())
}
func assertText(matches string: String) {
assert(with: GREYMatchers.matcher(forText: string))
}
func type(_ text: String) {
perform(GREYActions.action(forTypeText: text))
}
func backspace(times: Int) {
let backspaceString = String(repeating: "\u{8}", count: times)
type(backspaceString)
}
}