From d6e8b6ddee5c64bf5f00febe4a3c65dae79e3bef Mon Sep 17 00:00:00 2001 From: Szymon Kazmierczak Date: Mon, 12 Nov 2018 15:20:42 +0000 Subject: [PATCH 1/2] Updated replaceText to trigger UITextView notifications, added sample tests to exercise the changes and a character counter label for simple assertions --- EarlGrey/Action/GREYActions.m | 20 ++++++++++ .../Sources/FTRSwiftTests.swift | 26 +++++++++++++ .../TestRig/Sources/FTRTypingViewController.h | 1 + .../TestRig/Sources/FTRTypingViewController.m | 8 ++++ .../Sources/FTRTypingViewController.xib | 38 +++++++++++-------- 5 files changed, 77 insertions(+), 16 deletions(-) diff --git a/EarlGrey/Action/GREYActions.m b/EarlGrey/Action/GREYActions.m index 8fd2618a3..d6306ee4a 100644 --- a/EarlGrey/Action/GREYActions.m +++ b/EarlGrey/Action/GREYActions.m @@ -417,6 +417,7 @@ + (void)grey_setText:(NSString *)text onWebElement:(id)element { } BOOL elementIsUIControl = [element isKindOfClass:[UIControl class]]; BOOL elementIsUITextField = [element isKindOfClass:[UITextField class]]; + BOOL elementIsUITextView = [element isKindOfClass:[UITextView class]]; // Did begin editing notifications. if (elementIsUIControl) { @@ -429,6 +430,12 @@ + (void)grey_setText:(NSString *)text onWebElement:(id)element { object:element]; [NSNotificationCenter.defaultCenter postNotification:notification]; } + + if (elementIsUITextView) { + if ([((UITextView *)element).delegate respondsToSelector:@selector(textViewDidBeginEditing:)]) { + [((UITextView *)element).delegate textViewDidBeginEditing:((UITextView *)element)]; + } + } // Actually change the text. [element setText:text]; @@ -443,6 +450,12 @@ + (void)grey_setText:(NSString *)text onWebElement:(id)element { object:element]; [NSNotificationCenter.defaultCenter postNotification:notification]; } + + if (elementIsUITextView) { + if ([((UITextView *)element).delegate respondsToSelector:@selector(textViewDidBeginEditing:)]) { + [((UITextView *)element).delegate textViewDidBeginEditing:((UITextView *)element)]; + } + } // Did end editing notifications. if (elementIsUIControl) { @@ -455,6 +468,13 @@ + (void)grey_setText:(NSString *)text onWebElement:(id)element { object:element]; [NSNotificationCenter.defaultCenter postNotification:notification]; } + + if (elementIsUITextView) { + if ([((UITextView *)element).delegate respondsToSelector:@selector(textViewDidEndEditing:)]) { + [((UITextView *)element).delegate textViewDidEndEditing:((UITextView *)element)]; + } + } + } return YES; }]; diff --git a/Tests/FunctionalTests/Sources/FTRSwiftTests.swift b/Tests/FunctionalTests/Sources/FTRSwiftTests.swift index 379eb29cd..40f2d7170 100644 --- a/Tests/FunctionalTests/Sources/FTRSwiftTests.swift +++ b/Tests/FunctionalTests/Sources/FTRSwiftTests.swift @@ -137,6 +137,32 @@ class FTRSwiftTests: XCTestCase { .perform(grey_typeText("Fooo\u{8}B\u{8}Bar")) .assert(grey_text("FooBar")) } + + func testTypingOnUITextView() { + self.openTestView("Typing Views") + let typingField = grey_accessibilityID("TypingTextView") + let charCounter = grey_accessibilityID("charCounter") + + EarlGrey.selectElement(with: typingField) + .perform(grey_typeText("Simple")) + EarlGrey.selectElement(with: grey_text("Done")) + .perform(grey_tap()) + EarlGrey.selectElement(with: charCounter) + .assert(grey_text("6")) + } + + func testReplacingTextOnUITextView() { + self.openTestView("Typing Views") + let typingField = grey_accessibilityID("TypingTextView") + let charCounter = grey_accessibilityID("charCounter") + + EarlGrey.selectElement(with: typingField) + .perform(grey_replaceText("Simple")) + EarlGrey.selectElement(with: grey_text("Done")) + .perform(grey_tap()) + EarlGrey.selectElement(with: charCounter) + .assert(grey_text("6")) + } func testButtonPressWithGREYAllOf() { self.openTestView("Basic Views") diff --git a/Tests/FunctionalTests/TestRig/Sources/FTRTypingViewController.h b/Tests/FunctionalTests/TestRig/Sources/FTRTypingViewController.h index 99fe73550..5f1b2bd82 100644 --- a/Tests/FunctionalTests/TestRig/Sources/FTRTypingViewController.h +++ b/Tests/FunctionalTests/TestRig/Sources/FTRTypingViewController.h @@ -27,6 +27,7 @@ @property(nonatomic, retain) IBOutlet UITextField *inputAccessoryTextField; @property(nonatomic, retain) IBOutlet UIButton *inputButton; @property(nonatomic, retain) IBOutlet UITextField *textField; +@property(nonatomic, retain) IBOutlet UILabel *charCounter; @property(nonatomic, retain) IBOutlet UITextField *nonTypingTextField; @property(nonatomic, retain) IBOutlet FTRCustomTextView *customTextView; @property(nonatomic, retain) UIBarButtonItem *dismissKeyboardButton; diff --git a/Tests/FunctionalTests/TestRig/Sources/FTRTypingViewController.m b/Tests/FunctionalTests/TestRig/Sources/FTRTypingViewController.m index 62c0c2b8a..1fd27e48f 100644 --- a/Tests/FunctionalTests/TestRig/Sources/FTRTypingViewController.m +++ b/Tests/FunctionalTests/TestRig/Sources/FTRTypingViewController.m @@ -94,6 +94,9 @@ - (void)viewDidLoad { self.textField.userInteractionEnabled = YES; self.textField.accessibilityIdentifier = @"TypingTextField"; self.textField.autocorrectionType = UITextAutocorrectionTypeYes; + + self.charCounter.isAccessibilityElement = YES; + self.charCounter.accessibilityIdentifier = @"charCounter"; self.nonTypingTextField.delegate = self; self.nonTypingTextField.isAccessibilityElement = YES; @@ -158,6 +161,11 @@ - (void)textViewDidBeginEditing:(UITextView *)textView { self.navigationItem.rightBarButtonItem = self.dismissKeyboardButton; } +- (void)textViewDidEndEditing:(UITextView *)textView { + NSUInteger len = textView.text.length; + _charCounter.text = [NSString stringWithFormat: @"%lu", (unsigned long)len]; +} + - (void)dismissKeyboard { [self.textView resignFirstResponder]; self.navigationItem.rightBarButtonItem = nil; diff --git a/Tests/FunctionalTests/TestRig/Sources/FTRTypingViewController.xib b/Tests/FunctionalTests/TestRig/Sources/FTRTypingViewController.xib index 009a35faf..d96278fb0 100644 --- a/Tests/FunctionalTests/TestRig/Sources/FTRTypingViewController.xib +++ b/Tests/FunctionalTests/TestRig/Sources/FTRTypingViewController.xib @@ -1,13 +1,17 @@ - + + + + - - + + + @@ -21,7 +25,7 @@ - + @@ -42,7 +46,7 @@ - + @@ -55,7 +59,7 @@ - + @@ -78,15 +82,15 @@ - + - - + + @@ -96,8 +100,15 @@ + - + @@ -121,12 +132,7 @@ - + - - - - - From d66c03dff1c6b1571831f8981e8981ec8fc5086f Mon Sep 17 00:00:00 2001 From: Szymon Kazmierczak Date: Wed, 21 Nov 2018 10:56:50 +0000 Subject: [PATCH 2/2] Fix for incorrect selector --- EarlGrey/Action/GREYActions.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/EarlGrey/Action/GREYActions.m b/EarlGrey/Action/GREYActions.m index d6306ee4a..1f726b40f 100644 --- a/EarlGrey/Action/GREYActions.m +++ b/EarlGrey/Action/GREYActions.m @@ -452,8 +452,8 @@ + (void)grey_setText:(NSString *)text onWebElement:(id)element { } if (elementIsUITextView) { - if ([((UITextView *)element).delegate respondsToSelector:@selector(textViewDidBeginEditing:)]) { - [((UITextView *)element).delegate textViewDidBeginEditing:((UITextView *)element)]; + if ([((UITextView *)element).delegate respondsToSelector:@selector(textViewDidChange:)]) { + [((UITextView *)element).delegate textViewDidChange:((UITextView *)element)]; } }