Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename waiting methods #5

Merged
merged 4 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ let myButton = try eventGenerator.viewWithIdentifier("my_button", ofType: UIButt
You will often need to wait for the simulator to finish displaying something on the screen or for an animation to end. Hammer provides multiple methods to wait until a view is visible on screen or if a control is hittable

```swift
try eventGenerator.wait(untilVisible: "myLabel", timeout: 1)
try eventGenerator.wait(untilHittable: "myButton", timeout: 1)
try eventGenerator.waitUntilVisible("my_label", timeout: 1)
try eventGenerator.waitUntilHittable("my_button", timeout: 1)
```

## Troubleshooting
Expand Down
2 changes: 1 addition & 1 deletion Sources/Hammer/EventGenerator/EventGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public final class EventGenerator {
/// - parameter timeout: The maximum time to wait for the window to be ready.
public func waitUntilWindowIsReady(timeout: TimeInterval = 2) throws {
do {
try self.wait(until: self.isWindowReady, timeout: timeout)
try self.waitUntil(self.isWindowReady, timeout: timeout)
} catch {
throw HammerError.windowIsNotReadyForInteraction
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Hammer/Utilties/Subviews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ extension EventGenerator {
checkInterval: TimeInterval = 0.1) throws -> UIView
{
do {
return try self.wait(until: {
return try self.waitUntilExists({
do {
return try self.viewWithIdentifier(accessibilityIdentifier)
} catch HammerError.unableToFindView {
Expand Down Expand Up @@ -115,7 +115,7 @@ extension EventGenerator {
checkInterval: TimeInterval = 0.1) throws -> T
{
do {
return try self.wait(until: {
return try self.waitUntilExists({
do {
return try self.viewWithIdentifier(accessibilityIdentifier, ofType: type)
} catch HammerError.unableToFindView {
Expand Down
71 changes: 37 additions & 34 deletions Sources/Hammer/Utilties/Waiting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ extension EventGenerator {
/// - parameter checkInterval: How often should the condition be checked.
///
/// - throws: An error if the condition did not return true within the specified time.
public func wait(until condition: @autoclosure @escaping () throws -> Bool,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws
public func waitUntil(_ condition: @autoclosure @escaping () throws -> Bool,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws
{
let startTime = Date().timeIntervalSinceReferenceDate
while try !condition() {
Expand All @@ -41,8 +41,8 @@ extension EventGenerator {
///
/// - returns: The non-nil object.
@discardableResult
public func wait<T>(until exists: @autoclosure @escaping () throws -> T?,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws -> T
public func waitUntilExists<T>(_ exists: @autoclosure @escaping () throws -> T?,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws -> T
{
let startTime = Date().timeIntervalSinceReferenceDate
while true {
Expand All @@ -65,10 +65,10 @@ extension EventGenerator {
/// - parameter checkInterval: How often should the view be checked.
///
/// - throws: An error if the view does not exist after the specified time.
public func wait(untilExists accessibilityIdentifier: String,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws
public func waitUntilExists(_ accessibilityIdentifier: String,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws
{
try self.wait(until: self.viewWithIdentifier(accessibilityIdentifier),
try self.waitUntilExists(self.viewWithIdentifier(accessibilityIdentifier),
timeout: timeout, checkInterval: checkInterval)
}

Expand All @@ -80,11 +80,11 @@ extension EventGenerator {
/// - parameter checkInterval: How often should the view be checked.
///
/// - throws: An error if the view does not exist after the specified time.
public func wait(untilVisible accessibilityIdentifier: String, visibility: Visibility = .partial,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws
public func waitUntilVisible(_ accessibilityIdentifier: String, visibility: Visibility = .partial,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws
{
try self.wait(until: self.viewIsVisible(accessibilityIdentifier, visibility: visibility),
timeout: timeout, checkInterval: checkInterval)
try self.waitUntil(self.viewIsVisible(accessibilityIdentifier, visibility: visibility),
timeout: timeout, checkInterval: checkInterval)
}

/// Waits for a view with the specified identifier to be visible within the specified time.
Expand All @@ -95,11 +95,11 @@ extension EventGenerator {
/// - parameter checkInterval: How often should the view be checked.
///
/// - throws: An error if the view does not exist after the specified time.
public func wait(untilVisible view: UIView, visibility: Visibility = .partial,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws
public func waitUntilVisible(_ view: UIView, visibility: Visibility = .partial,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws
{
try self.wait(until: self.viewIsVisible(view, visibility: visibility),
timeout: timeout, checkInterval: checkInterval)
try self.waitUntil(self.viewIsVisible(view, visibility: visibility),
timeout: timeout, checkInterval: checkInterval)
}

/// Waits for a rect to be visible on screen within the specified time.
Expand All @@ -110,11 +110,11 @@ extension EventGenerator {
/// - parameter checkInterval: How often should the view be checked.
///
/// - throws: An error if the rect is not visible within the specified time.
public func wait(untilVisible rect: CGRect, visibility: Visibility = .partial,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws
public func waitUntilVisible(_ rect: CGRect, visibility: Visibility = .partial,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws
{
try self.wait(until: self.rectIsVisible(rect, visibility: visibility),
timeout: timeout, checkInterval: checkInterval)
try self.waitUntil(self.rectIsVisible(rect, visibility: visibility),
timeout: timeout, checkInterval: checkInterval)
}

/// Waits for a point to be visible on screen within the specified time.
Expand All @@ -124,11 +124,11 @@ extension EventGenerator {
/// - parameter checkInterval: How often should the view be checked.
///
/// - throws: An error if the point is not visible within the specified time.
public func wait(untilVisible point: CGPoint,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws
public func waitUntilVisible(_ point: CGPoint,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws
{
try self.wait(until: self.pointIsVisible(point),
timeout: timeout, checkInterval: checkInterval)
try self.waitUntil(self.pointIsVisible(point),
timeout: timeout, checkInterval: checkInterval)
}

/// Waits for a view with the specified identifier to be hittable within the specified time.
Expand All @@ -138,11 +138,11 @@ extension EventGenerator {
/// - parameter checkInterval: How often should the view be checked.
///
/// - throws: An error if the view does not exist after the specified time.
public func wait(untilHittable accessibilityIdentifier: String,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws
public func waitUntilHittable(_ accessibilityIdentifier: String,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws
{
try self.wait(until: self.viewIsHittable(accessibilityIdentifier),
timeout: timeout, checkInterval: checkInterval)
try self.waitUntil(self.viewIsHittable(accessibilityIdentifier),
timeout: timeout, checkInterval: checkInterval)
}

/// Waits for a view with the specified identifier to be hittable within the specified time.
Expand All @@ -152,10 +152,11 @@ extension EventGenerator {
/// - parameter checkInterval: How often should the view be checked.
///
/// - throws: An error if the view does not exist after the specified time.
public func wait(untilHittable view: UIView,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws
public func waitUntilHittable(_ view: UIView,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws
{
try self.wait(until: self.viewIsHittable(view), timeout: timeout, checkInterval: checkInterval)
try self.waitUntil(self.viewIsHittable(view),
timeout: timeout, checkInterval: checkInterval)
}

/// Waits for a point to be visible and hittable on screen within the specified time.
Expand All @@ -165,10 +166,11 @@ extension EventGenerator {
/// - parameter checkInterval: How often should the view be checked.
///
/// - throws: An error if the point is not hittable within the specified time.
public func wait(untilHittable point: CGPoint,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws
public func waitUntilHittable(_ point: CGPoint,
timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws
{
try self.wait(until: self.pointIsHittable(point), timeout: timeout, checkInterval: checkInterval)
try self.waitUntil(self.pointIsHittable(point),
timeout: timeout, checkInterval: checkInterval)
}

/// Waits for the default touch location to be visible and hittable on screen within the specified time.
Expand All @@ -178,6 +180,7 @@ extension EventGenerator {
///
/// - throws: An error if the point is not hittable within the specified time.
public func waitUntilHittable(timeout: TimeInterval, checkInterval: TimeInterval = 0.1) throws {
try self.wait(until: self.defaultTouchLocation, timeout: timeout, checkInterval: checkInterval)
try self.waitUntilExists(self.defaultTouchLocation,
timeout: timeout, checkInterval: checkInterval)
}
}
4 changes: 2 additions & 2 deletions Tests/HammerTests/ViewControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class ViewControllerTests: XCTestCase {
let viewController = TestSignInViewController()
let navigationController = UINavigationController(rootViewController: viewController)
let eventGenerator = try EventGenerator(viewController: navigationController)
try eventGenerator.wait(untilHittable: "username_field", timeout: 1)
try eventGenerator.waitUntilHittable("username_field", timeout: 1)

let usernameTextField = try eventGenerator.viewWithIdentifier("username_field",
ofType: UITextField.self)
Expand All @@ -29,7 +29,7 @@ final class ViewControllerTests: XCTestCase {
XCTAssertTrue(signInButton.isEnabled)
try eventGenerator.keyPress(.returnOrEnter)

try eventGenerator.wait(untilExists: "username_label", timeout: 1)
try eventGenerator.waitUntilExists("username_label", timeout: 1)
let usernameLabel = try eventGenerator.viewWithIdentifier("username_label", ofType: UILabel.self)

XCTAssertEqual(usernameLabel.text, "Hello GabrielUsername123")
Expand Down
21 changes: 12 additions & 9 deletions Tests/HammerTests/WaitingTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import CoreGraphics
import Dispatch
import Hammer
import UIKit
import XCTest
Expand All @@ -16,13 +15,14 @@ final class WaitingTests: XCTestCase {
try eventGenerator.waitUntilHittable(timeout: 1)

view.isHidden = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.02) {
let timer = Timer.scheduledTimer(withTimeInterval: 0.02, repeats: false) { _ in
view.isHidden = false
}

XCTAssertFalse(eventGenerator.viewIsVisible("my_button"))
try eventGenerator.wait(untilVisible: "my_button", timeout: 1)
try eventGenerator.waitUntilVisible("my_button", timeout: 1)
XCTAssertTrue(eventGenerator.viewIsVisible("my_button"))
timer.invalidate()
}

func testWaitUntilVisible() throws {
Expand All @@ -36,13 +36,14 @@ final class WaitingTests: XCTestCase {
try eventGenerator.waitUntilHittable(timeout: 1)

view.isHidden = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.02) {
let timer = Timer.scheduledTimer(withTimeInterval: 0.02, repeats: false) { _ in
view.isHidden = false
}

XCTAssertFalse(eventGenerator.viewIsVisible(view))
try eventGenerator.wait(untilVisible: view, timeout: 1)
try eventGenerator.waitUntilVisible(view, timeout: 1)
XCTAssertTrue(eventGenerator.viewIsVisible(view))
timer.invalidate()
}

func testWaitUntilVisibleMove() throws {
Expand All @@ -58,13 +59,14 @@ final class WaitingTests: XCTestCase {
let eventGenerator = try EventGenerator(view: scrollView)
try eventGenerator.waitUntilHittable(timeout: 1)

DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
let timer = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: false) { _ in
scrollView.scrollRectToVisible(view.frame, animated: false)
}

XCTAssertFalse(eventGenerator.viewIsVisible("my_button"))
try eventGenerator.wait(untilVisible: "my_button", timeout: 1)
try eventGenerator.waitUntilVisible("my_button", timeout: 1)
XCTAssertTrue(eventGenerator.viewIsVisible("my_button"))
timer.invalidate()
}

func testWaitUntilHittableWithIdentifier() throws {
Expand All @@ -78,13 +80,14 @@ final class WaitingTests: XCTestCase {
try eventGenerator.waitUntilHittable(timeout: 1)

view.isUserInteractionEnabled = false
DispatchQueue.main.asyncAfter(deadline: .now() + 0.02) {
let timer = Timer.scheduledTimer(withTimeInterval: 0.02, repeats: false) { _ in
view.isUserInteractionEnabled = true
}

XCTAssertFalse(eventGenerator.viewIsHittable("my_button"))
try eventGenerator.wait(untilHittable: "my_button", timeout: 1)
try eventGenerator.waitUntilHittable("my_button", timeout: 1)
XCTAssertTrue(eventGenerator.viewIsHittable("my_button"))
timer.invalidate()
}

func testViewForIdentifierWithTimeout() throws {
Expand Down