Skip to content

Instantly share code, notes, and snippets.

@wh1pch81n
Created April 21, 2017 03:02
Show Gist options
  • Save wh1pch81n/dcdcd73a316e1aca1034409f458da8f6 to your computer and use it in GitHub Desktop.
Save wh1pch81n/dcdcd73a316e1aca1034409f458da8f6 to your computer and use it in GitHub Desktop.
A quick class that could aid in finding a view with a specific identifier. Meant to be used in XCTest unit tests. Might be able to get around the limitations of UI Testing i.e. TableViews with 40000 rows.
class Finder<VIEW_TYPE: UIView> {
var foundView: VIEW_TYPE?
public func findView(with identifier: String) throws {
foundView = findView(with: identifier, parent: UIApplication.shared.keyWindow!)
if foundView == nil {
throw NSError()
}
}
private func findView(with identifier: String, parent: UIView) -> VIEW_TYPE? {
if parent.accessibilityIdentifier == identifier {
return parent as? VIEW_TYPE
}
var v: VIEW_TYPE?
for view in parent.subviews {
if let _v = findView(with: identifier, parent: view) {
v = _v
break
}
}
return v
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment