Skip to content

Commit

Permalink
Merge pull request #1317 from kif-framework/markj/enter_text_characte…
Browse files Browse the repository at this point in the history
…r_delay

Add character typing delay option for enter text
  • Loading branch information
mjohnson12 authored Jan 6, 2025
2 parents bee9233 + db117b5 commit 1c7a174
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 5 deletions.
19 changes: 19 additions & 0 deletions Sources/KIF/Classes/KIFUITestActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,14 @@ typedef NS_ENUM(NSUInteger, KIFPullToRefreshTiming) {
- (void)enterTextIntoCurrentFirstResponder:(NSString *)text;
- (void)enterTextIntoCurrentFirstResponder:(NSString *)text fallbackView:(UIView *)fallbackView;

/*!
@abstract Enters text into a the current first responder.
@discussion Text is entered into the view by simulating taps on the appropriate keyboard keys if the keyboard is already displayed. Useful to enter text in WKWebViews or components with no accessibility labels.
@param text The text to enter.
@param characterTypingDelay the amount to delay between typing each character.
*/
- (void)enterTextIntoCurrentFirstResponder:(NSString *)text fallbackView:(UIView *)fallbackView characterTypingDelay:(CFTimeInterval)characterTypingDelay;

/*!
@abstract Enters text into a particular view in the view hierarchy.
@discussion If the element isn't currently tappable, then the step will attempt to wait until it is. Once the view is present and tappable, a tap event is simulated in the center of the view or element, then text is entered into the view by simulating taps on the appropriate keyboard keys.
Expand All @@ -411,6 +419,17 @@ typedef NS_ENUM(NSUInteger, KIFPullToRefreshTiming) {
*/
- (void)enterText:(NSString *)text intoElement:(UIAccessibilityElement *)element inView:(UIView *)view expectedResult:(NSString *)expectedResult;

/*!
@abstract Enters text into a particular view in the view hierarchy.
@discussion If the element isn't currently tappable, then the step will attempt to wait until it is. Once the view is present and tappable, a tap event is simulated in the center of the view or element, then text is entered into the view by simulating taps on the appropriate keyboard keys.
@param text The text to enter.
@param expectedResult What the text value should be after entry, including any formatting done by the field. If this is nil, the "text" parameter will be used.
@param element the element to type into.
@param view the view to type into.
@param characterTypingDelay the amount to delay between typing each character.
*/
- (void)enterText:(NSString *)text intoElement:(UIAccessibilityElement *)element inView:(UIView *)view expectedResult:(NSString *)expectedResult characterTypingDelay:(CFTimeInterval)characterTypingDelay;

/*!
@abstract Enters text into a particular view in the view hierarchy.
@discussion The view or accessibility element with the given label is searched for in the view hierarchy. If the element isn't found or isn't currently tappable, then the step will attempt to wait until it is. Once the view is present and tappable, a tap event is simulated in the center of the view or element, then text is entered into the view by simulating taps on the appropriate keyboard keys.
Expand Down
16 changes: 14 additions & 2 deletions Sources/KIF/Classes/KIFUITestActor.m
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,11 @@ - (void)enterTextIntoCurrentFirstResponder:(NSString *)text
[self enterTextIntoCurrentFirstResponder:text fallbackView:nil];
}

- (void)enterTextIntoCurrentFirstResponder:(NSString *)text fallbackView:(UIView *)fallbackView
- (void)enterTextIntoCurrentFirstResponder:(NSString *)text fallbackView:(UIView *)fallbackView {
[self enterTextIntoCurrentFirstResponder:text fallbackView:fallbackView characterTypingDelay:0.0];
}

- (void)enterTextIntoCurrentFirstResponder:(NSString *)text fallbackView:(UIView *)fallbackView characterTypingDelay:(CFTimeInterval)characterTypingDelay
{
[text enumerateSubstringsInRange:NSMakeRange(0, text.length)
options:NSStringEnumerationByComposedCharacterSequences
Expand Down Expand Up @@ -551,6 +555,9 @@ - (void)enterTextIntoCurrentFirstResponder:(NSString *)text fallbackView:(UIView

[self failWithError:[NSError KIFErrorWithFormat:@"Failed to find key for character \"%@\"", characterString] stopTest:YES];
}
if (characterTypingDelay > 0) {
CFRunLoopRunInMode(UIApplicationCurrentRunMode, characterTypingDelay, false);
}
}];

NSTimeInterval remainingWaitTime = 0.01 - [KIFTypist keystrokeDelay];
Expand All @@ -575,14 +582,19 @@ - (void)enterText:(NSString *)text intoViewWithAccessibilityLabel:(NSString *)la
}

- (void)enterText:(NSString *)text intoElement:(UIAccessibilityElement *)element inView:(UIView *)view expectedResult:(NSString *)expectedResult;
{
[self enterText:text intoElement:element inView:view expectedResult:expectedResult characterTypingDelay:0.0];
}

- (void)enterText:(NSString *)text intoElement:(UIAccessibilityElement *)element inView:(UIView *)view expectedResult:(NSString *)expectedResult characterTypingDelay:(CFTimeInterval)characterTypingDelay;
{
// In iOS7, tapping a field that is already first responder moves the cursor to the front of the field
if (view.window.firstResponder != view) {
[self tapAccessibilityElement:element inView:view];
[self waitForTimeInterval:0.25 relativeToAnimationSpeed:YES];
}

[self enterTextIntoCurrentFirstResponder:text fallbackView:view];
[self enterTextIntoCurrentFirstResponder:text fallbackView:view characterTypingDelay:characterTypingDelay];
if (self.validateEnteredText) {
[self expectView:view toContainText:expectedResult ?: text];
}
Expand Down
26 changes: 26 additions & 0 deletions Sources/KIF/Classes/KIFUIViewTestActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,15 @@ extern NSString *const inputFieldTestString;
*/
- (void)enterText:(NSString *)text expectedResult:(NSString *)expectedResult;

/*!
@abstract Enters text into a particular view matching the tester's search predicate, then asserts that the view contains the expected text.
@discussion If the element isn't currently tappable, then the step will attempt to wait until it is. Once the view is present and tappable, a tap event is simulated in the center of the view or element, then text is entered into the view by simulating taps on the appropriate keyboard keys.
@param text The text to enter.
@param expectedResult What the text value should be after entry completes, including any formatting done by the field. If this is nil, the "text" parameter will be used.
@param characterTypingDelay the amount to delay between typing each character.
*/
- (void)enterText:(NSString *)text expectedResult:(NSString *)expectedResult characterTypingDelay:(CFTimeInterval)characterTypingDelay;

/*!
@abstract Enters text into a the current first responder.
@discussion Text is entered into the view by simulating taps on the appropriate keyboard keys if the keyboard is already displayed. Useful to enter text in WKWebViews or components with no accessibility labels.
Expand Down Expand Up @@ -388,6 +397,14 @@ extern NSString *const inputFieldTestString;
@param text The text to enter after clearing the view.
*/
- (void)clearAndEnterText:(NSString *)text;

/*!
@abstract Clears text from a particular view matching the tester's search predicate, then sets new text.
@discussion If the element isn't currently tappable, then the step will attempt to wait until it is. Once the view is present and tappable, a tap event is simulated in the center of the view or element, then text is cleared from the view by simulating taps on the backspace key, the new text is then entered by simulating taps on the appropriate keyboard keys.
@param text The text to enter after clearing the view.
@param characterTypingDelay the amount to delay between typing each character.
*/
- (void)clearAndEnterText:(NSString *)text characterTypingDelay:(CFTimeInterval)characterTypingDelay;
/*!
@abstract Clears text from a particular view matching the tester's search predicate, sets new text, then asserts that the view contains the expected text.
@discussion If the element isn't currently tappable, then the step will attempt to wait until it is. Once the view is present and tappable, a tap event is simulated in the center of the view or element, then text is cleared from the view by simulating taps on the backspace key, the new text is then entered by simulating taps on the appropriate keyboard keys, finally the text of the view is compared against the expected result.
Expand All @@ -397,6 +414,15 @@ extern NSString *const inputFieldTestString;
*/
- (void)clearAndEnterText:(NSString *)text expectedResult:(NSString *)expectedResult;

/*!
@abstract Clears text from a particular view matching the tester's search predicate, sets new text, then asserts that the view contains the expected text.
@discussion If the element isn't currently tappable, then the step will attempt to wait until it is. Once the view is present and tappable, a tap event is simulated in the center of the view or element, then text is cleared from the view by simulating taps on the backspace key, the new text is then entered by simulating taps on the appropriate keyboard keys, finally the text of the view is compared against the expected result.
@param text The text to enter after clearing the view.
@param expectedResult What the text value should be after entry completes, including any formatting done by the field. If this is nil, the "text" parameter will be used.
@param characterTypingDelay the amount to delay between typing each character.
*/
- (void)clearAndEnterText:(NSString *)text expectedResult:(NSString *)expectedResult characterTypingDelay:(CFTimeInterval)characterTypingDelay;

/*!
@abstract Sets text into a particular view matching the tester's search predicate.
@discussion The text is set on the view directly with 'setText:'. Does not result in first responder changes. Does not perform expected result validation.
Expand Down
21 changes: 18 additions & 3 deletions Sources/KIF/Classes/KIFUIViewTestActor.m
Original file line number Diff line number Diff line change
Expand Up @@ -326,26 +326,41 @@ - (void)enterText:(NSString *)text;
}

- (void)enterText:(NSString *)text expectedResult:(NSString *)expectedResult;
{
[self enterText:text expectedResult:expectedResult characterTypingDelay:0.0];
}

- (void)enterText:(NSString *)text expectedResult:(NSString *)expectedResult characterTypingDelay:(CFTimeInterval)characterTypingDelay;
{
if (!self.validateEnteredText && expectedResult) {
[self failWithMessage:@"Can't supply an expectedResult string if `validateEnteredText` is NO."];
}

@autoreleasepool {
KIFUIObject *found = [self _predicateSearchWithRequiresMatch:YES mustBeTappable:NO];
[self.actor enterText:text intoElement:found.element inView:found.view expectedResult:expectedResult];
[self.actor enterText:text intoElement:found.element inView:found.view expectedResult:expectedResult characterTypingDelay:characterTypingDelay];
}
}

- (void)clearAndEnterText:(NSString *)text;
{
[self clearAndEnterText:text expectedResult:nil];
[self clearAndEnterText:text expectedResult:nil characterTypingDelay:0.0];
}

- (void)clearAndEnterText:(NSString *)text characterTypingDelay:(CFTimeInterval)characterTypingDelay;
{
[self clearAndEnterText:text expectedResult:nil characterTypingDelay:characterTypingDelay];
}

- (void)clearAndEnterText:(NSString *)text expectedResult:(NSString *)expectedResult;
{
[self clearAndEnterText:text expectedResult:expectedResult characterTypingDelay:0.0];
}

- (void)clearAndEnterText:(NSString *)text expectedResult:(NSString *)expectedResult characterTypingDelay:(CFTimeInterval)characterTypingDelay;
{
[self clearText];
[self enterText:text expectedResult:expectedResult];
[self enterText:text expectedResult:expectedResult characterTypingDelay:characterTypingDelay];
}

- (void)enterTextIntoCurrentFirstResponder:(NSString *)text;
Expand Down

0 comments on commit 1c7a174

Please sign in to comment.