Skip to content

Commit

Permalink
XCUIElementQuery.first(where:)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tunous committed Dec 22, 2023
1 parent 9c8c83d commit 9966898
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Utilities for easier interaction with XCUITest methods.
- Accessing elements:
- `lastMatch`
- `self[2]`
- `first(where: { $0.label == "a" })`

- `CGVector` extensions:
- Normalized offsets:
Expand Down
1 change: 1 addition & 0 deletions Sources/XCAppTest/XCAppTest.docc/XCAppTest.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ element.assertExists(timeout: 3)

- ``XCAppTest/XCTest/XCUIElementQuery/lastMatch``
- ``XCAppTest/XCTest/XCUIElementQuery/subscript(_:)``
- ``XCAppTest/XCTest/XCUIElementQuery/first(where:)``
- ``XCAppTest/XCTest/XCUIApplication/safari``
- ``XCAppTest/XCTest/XCUIApplication/springboard``
- ``XCAppTest/XCTest/XCUIApplication/messages``
Expand Down
38 changes: 38 additions & 0 deletions Sources/XCAppTest/XCUIElementQuery+Matchers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// XCUIElementQuery+Properties.swift
//
//
// Created by Łukasz Rutkowski on 13/11/2023.
//

import XCTest

extension XCUIElementQuery {
/// The last element that matches the query.
public var lastMatch: XCUIElement {
element(boundBy: count - 1)
}

/// Returns an element that will use the index into the query’s results to determine which underlying accessibility element it is matched with.
///
/// - Parameters:
/// - index: The index of element to access.
public subscript(_ index: Int) -> XCUIElement {
element(boundBy: index)
}

/// Returns the first element that matches the query and satisfies the given `predicate`.
///
/// - Parameters:
/// - predicate: A closure that takes a matching query element as its argument and returns a Boolean value indicating whether the element is a match.
/// - Returns: The first matching element that satisfies `predicate`, or `nil` if there is no element that satisfies `predicate`.
public func first(where predicate: (XCUIElement) -> Bool) -> XCUIElement? {
for index in 0..<count {
let element = self[index]
if predicate(element) {
return element
}
}
return nil
}
}
23 changes: 0 additions & 23 deletions Sources/XCAppTest/XCUIElementQuery+Properties.swift

This file was deleted.

0 comments on commit 9966898

Please sign in to comment.