Skip to content
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
34 changes: 26 additions & 8 deletions Sources/ScreenObject/ScreenObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import XCTest
// and those are obviously specific to each screen, hence should be added by each subclass.
open class ScreenObject {

public enum WaitForScreenError: Equatable, Error {
case timedOut
}

/// The `XCUIApplication` instance this screen is part of. This is the value passed at
/// initialization time.
public let app: XCUIApplication
Expand Down Expand Up @@ -34,17 +38,31 @@ open class ScreenObject {

@discardableResult
func waitForScreen() throws -> Self {
XCTContext.runActivity(named: "Confirm screen \(self) is loaded") { (activity) in
let result = waitFor(element: expectedElement, predicate: "isEnabled == true", timeout: 20)
XCTAssert(result, "Screen \(self) is not loaded.")
try XCTContext.runActivity(named: "Confirm screen \(self) is loaded") { (activity) in
let result = waitFor(
element: expectedElement,
predicate: "isEnabled == true",
timeout: self.waitTimeout
)

guard result == .completed else { throw WaitForScreenError.timedOut }
Comment on lines -39 to +48
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of failing the tests in here, simply throw an error.

This allows consumers to catch the error if needed to continue the test execution.

}
return self
}

private func waitFor(element: XCUIElement, predicate: String, timeout: Int = 5) -> Bool {
let elementPredicate = XCTNSPredicateExpectation(predicate: NSPredicate(format: predicate), object: element)
let result = XCTWaiter.wait(for: [elementPredicate], timeout: TimeInterval(timeout))

return result == .completed
private func waitFor(
element: XCUIElement,
predicate: String,
timeout: TimeInterval
) -> XCTWaiter.Result {
XCTWaiter.wait(
for: [
XCTNSPredicateExpectation(
predicate: NSPredicate(format: predicate),
object: element
)
],
timeout: timeout
)
}
Comment on lines +53 to 67
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a minor line length and style change.

I decided to return the XCTWaiter wait(for:, timeout:) XCTWaiter.Result value instead of doing the check for it to be == .completed to make the consumer code clearer.

I didn't just inline this code in the consumer function because I can see us using this more in the future.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to return the XCTWaiter wait(for:, timeout:) XCTWaiter.Result value instead of doing the check for it to be == .completed to make the consumer code clearer.

This is a nice change!

}
29 changes: 25 additions & 4 deletions Tests/TestAppUITests/TestAppUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@ import XCTest

class TestAppUITests: XCTestCase {

let app = XCUIApplication()

override func setUpWithError() throws {
app.launch()
continueAfterFailure = false
}

func testExample() throws {
let app = XCUIApplication()
app.launch()

func testIsLoadedReturnsTrueWhenScreenIsLoaded() throws {
let screen = try HelloWorldScreen()
XCTAssertTrue(screen.isLoaded)
}

func testScreenInitThrowsWhenScreenIsNotLoaded() throws {
do {
_ = try MissingScreen(app: app)
XCTFail("Expected `ScreenObject` `init` to throw, but it didn't")
} catch {
XCTAssertEqual(error as? ScreenObject.WaitForScreenError, .timedOut)
}
}
}

final class HelloWorldScreen: ScreenObject {
Expand All @@ -22,3 +31,15 @@ final class HelloWorldScreen: ScreenObject {
try super.init(expectedElementGetter: { $0.staticTexts["Hello, world!"] })
}
}

/// A screen that doesn't exist. Use it to test the init failure behavior.
class MissingScreen: ScreenObject {

init(app: XCUIApplication) throws {
try super.init(
expectedElementGetter: { $0.staticTexts["this screen does not exist"] },
app: app,
waitTimeout: 1
)
}
}