Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add khidusage_keyboardclear as possible option for text field clear on iOS #810

Closed
wants to merge 1 commit into from
Closed
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
38 changes: 24 additions & 14 deletions WebDriverAgentLib/Categories/XCUIElement+FBTyping.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
#import "NSString+FBVisualLength.h"
#import "FBXCElementSnapshotWrapper.h"
#import "FBXCElementSnapshotWrapper+Helpers.h"
#import "XCUIDevice+FBHelpers.h"
#import "XCUIElement+FBCaching.h"
#import "XCUIElement+FBUtilities.h"
#import "FBXCodeCompatibility.h"

#define MAX_CLEAR_RETRIES 2
#define MAX_CLEAR_RETRIES 3


@interface NSString (FBRepeat)
Expand Down Expand Up @@ -141,24 +142,33 @@ - (BOOL)fb_clearTextWithSnapshot:(FBXCElementSnapshotWrapper *)snapshot
NSString *placeholderValue = snapshot.placeholderValue;
NSUInteger preClearTextLength = [currentValue fb_visualLength];
do {
if (retry >= MAX_CLEAR_RETRIES - 1) {
// Last chance retry. Tripple-tap the field to select its content

if ([self respondsToSelector:@selector(tapWithNumberOfTaps:numberOfTouches:)]) {
// e.g. tvOS 17 raised unrecognized selector error for XCUIElementTypeSearchField
// while following typeText worked.
[self tapWithNumberOfTaps:3 numberOfTouches:1];
}
return [FBKeyboard typeText:backspaceDeleteSequence error:error];
}

NSString *textToType = [backspaceDeleteSequence fb_repeatTimes:preClearTextLength];
if (shouldPrepareForInput && 0 == retry) {
[self fb_prepareForTextInputWithSnapshot:snapshot];
}
if (![FBKeyboard typeText:textToType error:error]) {
NSString *backspacesToType = [backspaceDeleteSequence fb_repeatTimes:preClearTextLength];
#if TARGET_OS_IOS
if (retry == MAX_CLEAR_RETRIES - 2) {
Copy link
Member

@KazuCocoa KazuCocoa Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so,

1st attempt -> typeText
2nd -> fb_performIOHIDEventWithPage
3rd -> tapWithNumberOfTaps:3

correct? Or I wondered if we could try the fb_performIOHIDEventWithPage first, then typeText, but the last one will be tapWithNumberOfTaps:3

Copy link
Member

@KazuCocoa KazuCocoa Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's about #811 ?

// https://github.com/appium/appium/issues/19389
[[XCUIDevice sharedDevice] fb_performIOHIDEventWithPage:0x07
KazuCocoa marked this conversation as resolved.
Show resolved Hide resolved
usage:0x9c
duration:0.05
error:nil];
} else if (retry >= MAX_CLEAR_RETRIES - 1) {
// Last chance retry. Tripple-tap the field to select its content
[self tapWithNumberOfTaps:3 numberOfTouches:1];
return [FBKeyboard typeText:backspaceDeleteSequence error:error];
} else if (![FBKeyboard typeText:backspacesToType error:error]) {
return NO;
}
#else
if (retry >= MAX_CLEAR_RETRIES - 1) {
return [[[FBErrorBuilder builder]
withDescriptionFormat:@"'%@' cannot be cleared of its text", snapshot.fb_description]
buildError:error];
} else if (![FBKeyboard typeText:backspacesToType error:error]) {
Comment on lines +164 to +168
Copy link
Member

@KazuCocoa KazuCocoa Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change here to:

    if (retry >= MAX_CLEAR_RETRIES - 1) {
      return [FBKeyboard typeText:backspaceDeleteSequence error:error];
    } else if (![FBKeyboard typeText:backspacesToType error:error]) {
      return NO;
    }

?
In the current implementation, calling clear text for an empty search field generates the below error. (This was Apple's general search field in the tvOS's content search)

> e.clear
Selenium::WebDriver::Error::InvalidElementStateError: Error Domain=com.facebook.WebDriverAgent Code=1 "'XCUIElementTypeSearchField' cannot be cleared of its text" UserInfo={NSLocalizedDescription='XCUIElementTypeSearchField' cannot be cleared of its text}

This was because self.fb_takeSnapshot.value kept returning its place holder while the field was empty (self.fb_takeSnapshot.label was nil). So we potentially can add label comparison as well in addition to currentValue = self.fb_takeSnapshot.value... but not sure it is a better idea.

So, just keeping existing behavior with return [FBKeyboard typeText:backspaceDeleteSequence error:error]; would be safe to keep current clear text's behavior for already cleared field.

return NO;
}
#endif

currentValue = self.fb_takeSnapshot.value;
if (nil != placeholderValue && [currentValue isEqualToString:placeholderValue]) {
Expand Down
Loading