Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#import "RunnerObjCExceptionCatcher.h"
#import "RunnerAXSnapshotBridge.h"
#import "RunnerSynthesizedGesture.h"
#import "RunnerTextEntry.h"
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ extension RunnerTests {
}
}

func typeIntoCurrentTarget(_ value: String) -> XCUIElement? {
func typeIntoCurrentTarget(_ value: String, allowBulkInput: Bool = true) -> XCUIElement? {
if let currentTarget = resolveTextEntryElement(app: app, target: activeTarget) {
app.typeText(value)
typeTextIntoTarget(value, target: currentTarget, allowBulkInput: allowBulkInput)
return currentTarget
} else {
app.typeText(value)
Expand All @@ -249,7 +249,7 @@ extension RunnerTests {
if delaySeconds > 0 && characters.count > 1 {
var typedTarget: XCUIElement?
for (index, character) in characters.enumerated() {
typedTarget = typeIntoCurrentTarget(String(character)) ?? typedTarget
typedTarget = typeIntoCurrentTarget(String(character), allowBulkInput: false) ?? typedTarget
if index + 1 < characters.count {
sleepFor(delaySeconds)
}
Expand Down Expand Up @@ -338,13 +338,30 @@ extension RunnerTests {
observedText.count
)
clearTextInput(repairTarget)
app.typeText(expectedText)
typeTextIntoTarget(expectedText, target: repairTarget)
return verifyTextEntry(app: app, target: target, expectedText: expectedText, repaired: true)
#else
return TextEntryResult(verified: nil, repaired: false, expectedText: expectedText, observedText: nil)
#endif
}

private func typeTextIntoTarget(
_ value: String,
target: XCUIElement,
allowBulkInput: Bool = true
) {
#if os(iOS) && targetEnvironment(simulator)
if allowBulkInput, value.count > 1 {
if let error = RunnerTextEntry.typeText(value, intoElement: target, typingSpeed: 120.0) {
NSLog("AGENT_DEVICE_RUNNER_FAST_TEXT_ENTRY_FALLBACK=%@", error)
} else {
return
}
}
#endif
target.typeText(value)
}

private func verifyTextEntry(
app: XCUIApplication,
target: TextEntryTarget,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface RunnerTextEntry : NSObject

+ (NSString * _Nullable)typeText:(NSString *)text
intoElement:(id)element
typingSpeed:(double)typingSpeed;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#import "RunnerTextEntry.h"

#import <objc/message.h>
#import <objc/runtime.h>

typedef void (*RunnerMsgSendTypeTextWithSpeed)(id, SEL, NSString *, NSUInteger, double, BOOL);

@implementation RunnerTextEntry

+ (NSString * _Nullable)typeText:(NSString *)text
intoElement:(id)element
typingSpeed:(double)typingSpeed {
SEL selector = NSSelectorFromString(@"typeText:atOffset:typingSpeed:shouldRedact:");
if (element == nil || ![element respondsToSelector:selector]) {
return [NSString stringWithFormat:
@"private XCTest text entry unavailable: selector missing on %@ (%@)",
NSStringFromClass([element class]),
[self textEntrySelectorListForElement:element]
];
}

@try {
((RunnerMsgSendTypeTextWithSpeed)objc_msgSend)(element, selector, text, 0, typingSpeed, NO);
} @catch (NSException *exception) {
NSString *name = exception.name ?: @"NSException";
NSString *reason = exception.reason ?: @"private XCTest text entry failed";
return [NSString stringWithFormat:@"%@: %@", name, reason];
}
return nil;
}

+ (NSString *)textEntrySelectorListForElement:(id)element {
NSMutableArray<NSString *> *selectors = [NSMutableArray array];
Class cls = [element class];
while (cls != Nil) {
unsigned int count = 0;
Method *methods = class_copyMethodList(cls, &count);
for (unsigned int index = 0; index < count; index += 1) {
SEL selector = method_getName(methods[index]);
NSString *name = NSStringFromSelector(selector);
if ([name containsString:@"type"] || [name containsString:@"Text"] || [name containsString:@"keyboard"]) {
[selectors addObject:[NSString stringWithFormat:@"%@:%@", NSStringFromClass(cls), name]];
}
}
free(methods);
cls = class_getSuperclass(cls);
}
return [selectors componentsJoinedByString:@", "];
}

@end
Loading