1
1
/// Represents a sequence of WebDriver key events and characters.
2
2
public struct Keys : RawRepresentable {
3
+ /// A string encoding the key sequence as defined by the WebDriver spec.
3
4
public var rawValue : String
4
5
5
6
public init ( rawValue: String ) { self . rawValue = rawValue }
6
7
7
- public static func + ( lhs: Self , rhs: Self ) -> Self { Self ( rawValue: lhs. rawValue + rhs. rawValue) }
8
+ /// Concatenates multiple key sequences into a single one.
9
+ public static func sequence( _ keys: [ Self ] ) -> Self {
10
+ Self ( rawValue: keys. reduce ( " " ) { $0 + $1. rawValue } )
11
+ }
12
+
13
+ /// Concatenates multiple key sequences into a single one.
14
+ public static func sequence( _ keys: Self ... ) -> Self {
15
+ sequence ( keys)
16
+ }
17
+ }
8
18
19
+ // MARK: Key constants
20
+ extension Keys {
9
21
public static let a = Self ( rawValue: " a " )
10
22
public static let b = Self ( rawValue: " b " )
11
23
public static let c = Self ( rawValue: " c " )
@@ -109,47 +121,30 @@ public struct Keys: RawRepresentable {
109
121
public static let releaseModifiers = Keys ( rawValue: " \u{E000} " )
110
122
}
111
123
124
+ // MARK: Modifier sequences
112
125
extension Keys {
113
126
/// Wraps a keys sequence with holding and releasing the shift key.
114
127
public static func shift( _ keys: Self ) -> Self {
115
- Self ( rawValue : " \( shiftModifier. rawValue ) \( keys. rawValue ) \( shiftModifier. rawValue ) " )
128
+ sequence ( shiftModifier, keys, shiftModifier)
116
129
}
117
130
118
131
/// Wraps a keys sequence with holding and releasing the control key.
119
132
public static func control( _ keys: Self ) -> Self {
120
- Self ( rawValue : " \( controlModifier. rawValue ) \( keys. rawValue ) \( controlModifier. rawValue ) " )
133
+ sequence ( controlModifier, keys, controlModifier)
121
134
}
122
135
123
136
/// Wraps a keys sequence with holding and releasing the alt key.
124
137
public static func alt( _ keys: Self ) -> Self {
125
- Self ( rawValue : " \( altModifier. rawValue ) \( keys. rawValue ) \( altModifier. rawValue ) " )
138
+ sequence ( altModifier, keys, altModifier)
126
139
}
127
140
128
141
/// Wraps a keys sequence with holding and releasing the meta key.
129
142
public static func meta( _ keys: Self ) -> Self {
130
- Self ( rawValue: " \( metaModifier. rawValue) \( keys. rawValue) \( metaModifier. rawValue) " )
131
- }
132
-
133
- /// Wraps a keys sequence with holding and releasing modifier keys.
134
- public static func combo( _ keys: Self , shift: Bool = false , control: Bool = false , alt: Bool = false , meta: Bool = false ) -> Self {
135
- var rawValue = " "
136
-
137
- if shift { rawValue += shiftModifier. rawValue }
138
- if control { rawValue += controlModifier. rawValue }
139
- if alt { rawValue += altModifier. rawValue }
140
- if meta { rawValue += metaModifier. rawValue }
141
-
142
- rawValue += keys. rawValue
143
-
144
- if meta { rawValue += metaModifier. rawValue }
145
- if alt { rawValue += altModifier. rawValue }
146
- if control { rawValue += controlModifier. rawValue }
147
- if shift { rawValue += shiftModifier. rawValue }
148
-
149
- return Self ( rawValue: rawValue)
143
+ sequence ( metaModifier, keys, metaModifier)
150
144
}
151
145
}
152
146
147
+ // MARK: Text and typing
153
148
extension Keys {
154
149
public enum TypingStrategy {
155
150
case assumeUSKeyboard
@@ -210,4 +205,4 @@ extension Keys {
210
205
default : return false
211
206
}
212
207
}
213
- }
208
+ }
0 commit comments