mirror of
https://github.com/priyanshujain/sanderling.git
synced 2026-10-04 20:17:09 +00:00
fix(companion): type on the main thread and recover from focus assertions
This commit is contained in:
1 parent
76cbf1a102
commit
fc215695a2
2 files changed
+51
-14
No files matched your search
@@ -35,4 +35,20 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
completion:(void (^)(NSError *_Nullable error))completion;
|
completion:(void (^)(NSError *_Nullable error))completion;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
// Runs the block and converts any raised NSException into an NSError so a
|
||||||
|
// testing-framework assertion does not terminate the runner process.
|
||||||
|
NS_INLINE BOOL CompanionRunCatching(void (^block)(void), NSError *_Nullable *_Nullable error) {
|
||||||
|
@try {
|
||||||
|
block();
|
||||||
|
return YES;
|
||||||
|
} @catch (NSException *exception) {
|
||||||
|
if (error) {
|
||||||
|
*error = [NSError errorWithDomain:@"dev.sanderling.companion"
|
||||||
|
code:1
|
||||||
|
userInfo:@{NSLocalizedDescriptionKey: exception.reason ?: exception.name}];
|
||||||
|
}
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_END
|
NS_ASSUME_NONNULL_END
|
||||||
@@ -5,39 +5,60 @@ import XCTest
|
|||||||
// length is consumed first by prepending that many delete keys so the whole
|
// length is consumed first by prepending that many delete keys so the whole
|
||||||
// replacement happens in one typeText call.
|
// replacement happens in one typeText call.
|
||||||
enum TextInput {
|
enum TextInput {
|
||||||
|
enum TextInputError: Error {
|
||||||
|
case typingFailed(String)
|
||||||
|
}
|
||||||
|
|
||||||
static func type(text: String, replace: Bool, bundleIdentifier: String) throws {
|
static func type(text: String, replace: Bool, bundleIdentifier: String) throws {
|
||||||
let application = XCUIApplication(bundleIdentifier: bundleIdentifier)
|
let application = XCUIApplication(bundleIdentifier: bundleIdentifier)
|
||||||
let focused = focusedElement(in: application)
|
|
||||||
|
|
||||||
var payload = text
|
var payload = text
|
||||||
if replace {
|
if replace {
|
||||||
let currentLength = currentValueLength(of: focused)
|
let currentLength = focusedValueLength(bundleIdentifier: bundleIdentifier)
|
||||||
if currentLength > 0 {
|
if currentLength > 0 {
|
||||||
let deletes = String(repeating: XCUIKeyboardKey.delete.rawValue, count: currentLength)
|
let deletes = String(repeating: XCUIKeyboardKey.delete.rawValue, count: currentLength)
|
||||||
payload = deletes + text
|
payload = deletes + text
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let focused = focused {
|
// Type at the application level so it lands in whatever holds keyboard
|
||||||
focused.typeText(payload)
|
// focus, without resolving a specific element. Compose Multiplatform
|
||||||
} else {
|
// text fields do not reliably expose hasKeyboardFocus to the query layer.
|
||||||
|
// typeText must run on the main thread.
|
||||||
|
var caughtError: NSError?
|
||||||
|
var completed = false
|
||||||
|
runOnMain {
|
||||||
|
completed = CompanionRunCatching({
|
||||||
application.typeText(payload)
|
application.typeText(payload)
|
||||||
|
}, &caughtError)
|
||||||
|
}
|
||||||
|
if !completed {
|
||||||
|
throw TextInputError.typingFailed(caughtError?.localizedDescription ?? "unknown")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static func focusedElement(in application: XCUIApplication) -> XCUIElement? {
|
private static func runOnMain(_ work: () -> Void) {
|
||||||
let predicate = NSPredicate(format: "hasKeyboardFocus == true")
|
if Thread.isMainThread {
|
||||||
let matches = application.descendants(matching: .any).matching(predicate)
|
work()
|
||||||
if matches.count > 0 {
|
} else {
|
||||||
return matches.element(boundBy: 0)
|
DispatchQueue.main.sync(execute: work)
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static func currentValueLength(of element: XCUIElement?) -> Int {
|
// Reads the current value length of the focused editable field from a flat
|
||||||
guard let element = element, let value = element.value as? String else {
|
// snapshot. Used to size the delete prefix for replace.
|
||||||
return 0
|
private static func focusedValueLength(bundleIdentifier: String) -> Int {
|
||||||
|
let elements = Snapshot.elements(bundleIdentifier: bundleIdentifier)
|
||||||
|
for element in elements {
|
||||||
|
guard let type = element["type"] as? String,
|
||||||
|
type == "TextArea" || type == "TextField",
|
||||||
|
let value = element["AXValue"] as? String else {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
if !value.isEmpty {
|
||||||
return value.count
|
return value.count
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user