Skip to content

Commit

Permalink
Make findElements return a nonoptional array (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle authored Oct 31, 2023
1 parent 9e4cbbd commit 5c23942
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
12 changes: 6 additions & 6 deletions Sources/Element.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,44 +126,44 @@ public struct Element {
/// - Parameter byId: id of the element to search for.
/// - Parameter retryTimeout: Optional value to override defaultRetryTimeout.
/// - Returns: The elements that were found, if any.
public func findElements(byId id: String, retryTimeout: TimeInterval? = nil) throws -> [Element]? {
public func findElements(byId id: String, retryTimeout: TimeInterval? = nil) throws -> [Element] {
try findElements(using: "id", value: id, retryTimeout: retryTimeout)
}

/// Search for elements by name, starting from this element.
/// - Parameter byName: name of the element to search for.
/// - Parameter retryTimeout: Optional value to override defaultRetryTimeout.
/// - Returns: The elements that were found, if any.
public func findElements(byName name: String, retryTimeout: TimeInterval? = nil) throws -> [Element]? {
public func findElements(byName name: String, retryTimeout: TimeInterval? = nil) throws -> [Element] {
try findElements(using: "name", value: name, retryTimeout: retryTimeout)
}

/// Search for elements in the accessibility tree, starting from this element.
/// - Parameter byAccessibilityId: accessibiilty id of the element to search for.
/// - Parameter retryTimeout: Optional value to override defaultRetryTimeout.
/// - Returns: The elements that were found, if any.
public func findElements(byAccessibilityId id: String, retryTimeout: TimeInterval? = nil) throws -> [Element]? {
public func findElements(byAccessibilityId id: String, retryTimeout: TimeInterval? = nil) throws -> [Element] {
try findElements(using: "accessibility id", value: id, retryTimeout: retryTimeout)
}

/// Search for elements by xpath, starting from this element.
/// - Parameter byXPath: xpath of the element to search for.
/// - Parameter retryTimeout: Optional value to override defaultRetryTimeout.
/// - Returns: The elements that were found, if any.
public func findElements(byXPath xpath: String, retryTimeout: TimeInterval? = nil) throws -> [Element]? {
public func findElements(byXPath xpath: String, retryTimeout: TimeInterval? = nil) throws -> [Element] {
try findElements(using: "xpath", value: xpath, retryTimeout: retryTimeout)
}

/// Search for elements by class name, starting from this element.
/// - Parameter byClassName: class name of the element to search for.
/// - Parameter retryTimeout: Optional value to override defaultRetryTimeout.
/// - Returns: The elements that were found, if any.
public func findElements(byClassName className: String, retryTimeout: TimeInterval? = nil) throws -> [Element]? {
public func findElements(byClassName className: String, retryTimeout: TimeInterval? = nil) throws -> [Element] {
try findElements(using: "class name", value: className, retryTimeout: retryTimeout)
}

// Helper for findElements functions above.
private func findElements(using: String, value: String, retryTimeout: TimeInterval?) throws -> [Element]? {
private func findElements(using: String, value: String, retryTimeout: TimeInterval?) throws -> [Element] {
try session.findElements(startingAt: self, using: using, value: value, retryTimeout: retryTimeout)
}

Expand Down
28 changes: 10 additions & 18 deletions Sources/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,63 +164,55 @@ public class Session {
/// - Parameter byId: id of the element to search for.
/// - Parameter retryTimeout: Optional value to override defaultRetryTimeout.
/// - Returns: The elements that were found, if any.
public func findElements(byId id: String, retryTimeout: TimeInterval? = nil) throws -> [Element]? {
public func findElements(byId id: String, retryTimeout: TimeInterval? = nil) throws -> [Element] {
try findElements(startingAt: nil, using: "id", value: id, retryTimeout: retryTimeout)
}

/// Finds elements by name, starting from the root.
/// - Parameter byName: name of the element to search for.
/// - Parameter retryTimeout: Optional value to override defaultRetryTimeout.
/// - Returns: The elements that were found, if any.
public func findElements(byName name: String, retryTimeout: TimeInterval? = nil) throws -> [Element]? {
public func findElements(byName name: String, retryTimeout: TimeInterval? = nil) throws -> [Element] {
try findElements(startingAt: nil, using: "name", value: name, retryTimeout: retryTimeout)
}

/// Finds elements by accessibility id, starting from the root.
/// - Parameter byAccessibilityId: accessibiilty id of the element to search for.
/// - Parameter retryTimeout: Optional value to override defaultRetryTimeout.
/// - Returns: The elements that were found, if any.
public func findElements(byAccessibilityId id: String, retryTimeout: TimeInterval? = nil) throws -> [Element]? {
public func findElements(byAccessibilityId id: String, retryTimeout: TimeInterval? = nil) throws -> [Element] {
try findElements(startingAt: nil, using: "accessibility id", value: id, retryTimeout: retryTimeout)
}

/// Finds elements by xpath, starting from the root.
/// - Parameter byXPath: xpath of the element to search for.
/// - Parameter retryTimeout: Optional value to override defaultRetryTimeout.
/// - Returns: The elements that were found, if any.
public func findElements(byXPath xpath: String, retryTimeout: TimeInterval? = nil) throws -> [Element]? {
public func findElements(byXPath xpath: String, retryTimeout: TimeInterval? = nil) throws -> [Element] {
try findElements(startingAt: nil, using: "xpath", value: xpath, retryTimeout: retryTimeout)
}

/// Finds elements by class name, starting from the root.
/// - Parameter byClassName: class name of the element to search for.
/// - Parameter retryTimeout: Optional value to override defaultRetryTimeout.
/// - Returns: The elements that were found, if any.
public func findElements(byClassName className: String, retryTimeout: TimeInterval? = nil) throws -> [Element]? {
public func findElements(byClassName className: String, retryTimeout: TimeInterval? = nil) throws -> [Element] {
try findElements(startingAt: nil, using: "class name", value: className, retryTimeout: retryTimeout)
}

// Helper for findElements functions above.
internal func findElements(startingAt element: Element?, using: String, value: String, retryTimeout: TimeInterval?) throws -> [Element]? {
internal func findElements(startingAt element: Element?, using: String, value: String, retryTimeout: TimeInterval?) throws -> [Element] {
let request = Requests.SessionElements(session: id, element: element?.id, using: using, value: value)

let elementIds = try poll(timeout: retryTimeout ?? defaultRetryTimeout) {
let elementIds: [String]?
return try poll(timeout: retryTimeout ?? defaultRetryTimeout) {
do {
// Allow errors to bubble up unless they are specifically saying that the element was not found.
elementIds = try webDriver.send(request).value.map { $0.element }
return PollResult.success(try webDriver.send(request).value.map { Element(session: self, id: $0.element) })
} catch let error as ErrorResponse where error.status == .noSuchElement {
elementIds = nil
// Follow the WebDriver spec and keep polling if no elements are found
return PollResult.failure([])
}

guard let elementIds, elementIds.count > 0 else {
return PollResult.failure(elementIds)
}

return PollResult.success(elementIds)
}.value

return elementIds.map { $0.map { Element(session: self, id: $0) } }
}

/// Moves the pointer to a location relative to the current pointer position or an element.
Expand Down

0 comments on commit 5c23942

Please sign in to comment.