From 0137dddefe3e1913e382a259d0fd0fdf47bd542b Mon Sep 17 00:00:00 2001 From: fragarsie Date: Thu, 20 Dec 2018 18:24:52 +0100 Subject: [PATCH 1/2] Create Screen concept and add some documentation --- ChaiOS/Classes/Action.swift | 5 ++ ChaiOS/Classes/ChaiOS.swift | 107 ++++++++++++++++++------------- ChaiOS/Classes/Interaction.swift | 16 ++++- ChaiOS/Classes/Matcher.swift | 26 +++++--- 4 files changed, 97 insertions(+), 57 deletions(-) diff --git a/ChaiOS/Classes/Action.swift b/ChaiOS/Classes/Action.swift index ada08a4..22d97fd 100644 --- a/ChaiOS/Classes/Action.swift +++ b/ChaiOS/Classes/Action.swift @@ -1,6 +1,11 @@ import Foundation import EarlGrey +/** + Enum for performing actions on views. + + Provides a lot of action methods, such as .tap, .scrollInDirection, etc. +*/ public enum Action { case scrollInDirection(Direction, amount: CGFloat) case scrollToContentEdge(ContentEdge) diff --git a/ChaiOS/Classes/ChaiOS.swift b/ChaiOS/Classes/ChaiOS.swift index b98a735..a4e74d9 100644 --- a/ChaiOS/Classes/ChaiOS.swift +++ b/ChaiOS/Classes/ChaiOS.swift @@ -1,53 +1,68 @@ import Foundation import EarlGrey -// TODO: Document the class +/** + Static container generic UI interaction methods, + such as rotate the device screen or close the keyboard. +*/ public final class ChaiOS { - - public static func with(_ matcher: Matcher) -> Interaction { - return Interaction(matcher: matcher) - } - - public static func that(_ condition: Condition) -> Matcher { - return Matcher.that(condition) - } - - public static func any(_ conditions: Condition...) -> Matcher { - return Matcher.any(conditions) - } - - public static func all(_ conditions: Condition...) -> Matcher { - return Matcher.all(conditions) - } - - public static func any(_ conditions: [Condition]) -> Matcher { - return Matcher.any(conditions) - } - - public static func all(_ conditions: [Condition]) -> Matcher { - return Matcher.all(conditions) - } + + public static func rotateDevice(to orientation: UIDeviceOrientation) throws { + var error: NSError? + EarlGreyImpl.invoked(fromFile: #file, lineNumber: #line).rotateDevice(to: orientation, errorOrNil: &error) + if let error = error { + throw error + } + } + + public static func dismissKeyboard() throws { + var error: NSError? + EarlGreyImpl.invoked(fromFile: #file, lineNumber: #line).dismissKeyboardWithError(&error) + if let error = error { + throw error + } + } + + public static func condition(with: Matcher) -> Interaction { + + } } -extension ChaiOS { - - public static func rotateDevice(to orientation: UIDeviceOrientation) throws { - var error: NSError? - EarlGreyImpl.invoked(fromFile: #file, lineNumber: #line).rotateDevice(to: orientation, errorOrNil: &error) - if let error = error { - throw error - } - } - - public static func dismissKeyboard() throws { - var error: NSError? - EarlGreyImpl.invoked(fromFile: #file, lineNumber: #line).dismissKeyboardWithError(&error) - if let error = error { - throw error - } - } - - public static func condition(with: Matcher) -> Interaction { - - } +/** + Protocol for container classes of UI elements. + + This protocol groups UI elements and grants access to basic actions, + such as perform() and assert() +*/ +public protocol Screen { } + +extension Screen { + + public func test(_ closure: (Self) -> Void) { + closure(self) + } + + public static func with(_ matcher: Matcher) -> Interaction { + return Interaction(matcher: matcher) + } + + public static func that(_ condition: Condition) -> Matcher { + return Matcher.that(condition) + } + + public static func any(_ conditions: Condition...) -> Matcher { + return Matcher.any(conditions) + } + + public static func all(_ conditions: Condition...) -> Matcher { + return Matcher.all(conditions) + } + + public static func any(_ conditions: [Condition]) -> Matcher { + return Matcher.any(conditions) + } + + public static func all(_ conditions: [Condition]) -> Matcher { + return Matcher.all(conditions) + } } diff --git a/ChaiOS/Classes/Interaction.swift b/ChaiOS/Classes/Interaction.swift index a213a7f..d4c83be 100644 --- a/ChaiOS/Classes/Interaction.swift +++ b/ChaiOS/Classes/Interaction.swift @@ -1,6 +1,11 @@ import Foundation import EarlGrey +/** + Provides the primary methods for test authors to perform actions or asserts on views. + + Each interaction is associated with a view identified by a matcher. +*/ public class Interaction: GreyConvertible { public typealias Grey = GREYInteraction @@ -24,7 +29,10 @@ public class Interaction: GreyConvertible { public var toGrey: GREYInteraction { return index == nil ? selection : selection.atIndex(index!) } - + + /** + Asserts the given condition on the view selected by the current matcher. + */ @discardableResult public func assert(_ condition: Condition) -> Self { var selection = self.selection @@ -34,7 +42,7 @@ public class Interaction: GreyConvertible { selection.assert(condition.toGrey) return self } - + @discardableResult public func assert(_ otherMatcher: Matcher) -> Self { self.toGrey.assert(otherMatcher.toGrey) @@ -52,7 +60,9 @@ public class Interaction: GreyConvertible { self.toGrey.using(searchAction: action.toGrey, onElementWithMatcher: matcher.toGrey) return self } - + /** + Performs the given action on the view selected by the current matcher. + */ @discardableResult public func perform(_ action: Action) -> Self { self.toGrey.__perform(action.toGrey) diff --git a/ChaiOS/Classes/Matcher.swift b/ChaiOS/Classes/Matcher.swift index b0a1544..1b3cc39 100644 --- a/ChaiOS/Classes/Matcher.swift +++ b/ChaiOS/Classes/Matcher.swift @@ -1,9 +1,15 @@ import Foundation import EarlGrey +/** + A matcher over acceptable conditions. +*/ public enum Matcher { + ///Returns a Matcher associated to the view that satisfy the given condition. case that(Condition) + ///Returns a Matcher associated to the view that satisfy any of the given conditions. case any([Condition]) + ///Returns a Matcher associated to the view that satisfy all the given conditions. case all([Condition]) } @@ -23,14 +29,18 @@ extension Matcher: GreyConvertible { } extension Matcher { - - public func selected(file: StaticString = #file, line: UInt = #line) -> Interaction { - return Interaction.init(file: file, line: line, matcher: self) - } - - public subscript(_ at: UInt) -> Interaction { - return Interaction(matcher: self, index: at) - } + + public func selected(file: StaticString = #file, line: UInt = #line, closure: (Interaction) -> Void) { + closure(Interaction.init(file: file, line: line, matcher: self)) + } + + public func selected(file: StaticString = #file, line: UInt = #line) -> Interaction { + return Interaction.init(file: file, line: line, matcher: self) + } + + public subscript(_ at: UInt) -> Interaction { + return Interaction(matcher: self, index: at) + } } extension Matcher { From afbaaca026ae2dfb18211fd3133d43b340ea4725 Mon Sep 17 00:00:00 2001 From: fragarsie Date: Fri, 21 Dec 2018 13:04:06 +0100 Subject: [PATCH 2/2] Move Screen code and add static methods to ChaiOS class --- ChaiOS/Classes/ChaiOS.swift | 19 ------------------ ChaiOS/Classes/Screen.swift | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 19 deletions(-) create mode 100644 ChaiOS/Classes/Screen.swift diff --git a/ChaiOS/Classes/ChaiOS.swift b/ChaiOS/Classes/ChaiOS.swift index a4e74d9..2cf2b16 100644 --- a/ChaiOS/Classes/ChaiOS.swift +++ b/ChaiOS/Classes/ChaiOS.swift @@ -23,25 +23,6 @@ public final class ChaiOS { } } - public static func condition(with: Matcher) -> Interaction { - - } -} - -/** - Protocol for container classes of UI elements. - - This protocol groups UI elements and grants access to basic actions, - such as perform() and assert() -*/ -public protocol Screen { } - -extension Screen { - - public func test(_ closure: (Self) -> Void) { - closure(self) - } - public static func with(_ matcher: Matcher) -> Interaction { return Interaction(matcher: matcher) } diff --git a/ChaiOS/Classes/Screen.swift b/ChaiOS/Classes/Screen.swift new file mode 100644 index 0000000..073eb4f --- /dev/null +++ b/ChaiOS/Classes/Screen.swift @@ -0,0 +1,40 @@ +import Foundation + +/** +Protocol for container classes of UI elements. + +This protocol groups UI elements and grants access to basic actions, +such as perform() and assert() +*/ +public protocol Screen { } + +extension Screen { + + public func test(_ closure: (Self) -> Void) { + closure(self) + } + + public static func with(_ matcher: Matcher) -> Interaction { + return ChaiOS.with(matcher) + } + + public static func that(_ condition: Condition) -> Matcher { + return ChaiOS.that(condition) + } + + public static func any(_ conditions: Condition...) -> Matcher { + return ChaiOS.any(conditions) + } + + public static func all(_ conditions: Condition...) -> Matcher { + return ChaiOS.all(conditions) + } + + public static func any(_ conditions: [Condition]) -> Matcher { + return ChaiOS.any(conditions) + } + + public static func all(_ conditions: [Condition]) -> Matcher { + return ChaiOS.all(conditions) + } +}