Skip to content
New issue

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

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

Already on GitHub? # to your account

Ai 6581 make ability to switch keyboard language in ui tests #1

Merged
Merged
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
96 changes: 73 additions & 23 deletions EarlGrey/Core/GREYKeyboard.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
* A retry time interval in which we re-tap the shift key to ensure
* the alphabetic keyplane changed.
*/
static const NSTimeInterval kMaxShiftKeyToggleDuration = 3.0;
static const NSTimeInterval kMaxShiftKeyToggleDuration = 0.1;
Copy link
Owner Author

Choose a reason for hiding this comment

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

If EarlGrey fails to find a key, it taps shift several times to try another keyboard layouts (letters, capitalized letters, numbers, other symbols)
However, old delay was far to big. Tests could last for a decade if the text requires many language toggles to be typed


/**
* Time to wait for the keyboard to appear or disappear.
Expand Down Expand Up @@ -137,16 +137,18 @@ + (void)initialize {
// Note: more, numbers label must be after shift and SHIFT labels, because it is also used for
// the key for switching between keyplanes.
gShiftKeyLabels =
@[ @"shift", @"Shift", @"SHIFT", @"more, symbols", @"more, numbers", @"more", @"MORE" ];
@[@"shift", @"Shift", @"SHIFT", @"more, symbols", @"more, numbers", @"more", @"MORE", @"more, letters",
@"сдвиг", @"сдвиг , Caps lock on", @"еще, символы", @"еще, цифры", @"еще, буквы"
];

NSCharacterSet *lowerCaseSet = [NSCharacterSet lowercaseLetterCharacterSet];
gAlphabeticKeyplaneCharacters = [NSMutableCharacterSet uppercaseLetterCharacterSet];
[gAlphabeticKeyplaneCharacters formUnionWithCharacterSet:lowerCaseSet];

gModifierKeyIdentifierMapping = @{
kSpaceKeyIdentifier : @"space",
kDeleteKeyIdentifier : @"delete",
kReturnKeyIdentifier : @"return"
kSpaceKeyIdentifier : @[@"space", @"Пробел"],
kDeleteKeyIdentifier : @[@"delete", @"Удалить"],
kReturnKeyIdentifier : @[@"return", @"Ввод"]
};
}
}
Expand Down Expand Up @@ -174,11 +176,28 @@ + (BOOL)typeString:(NSString *)string
return NO;
}

__block BOOL success = YES;
for (NSUInteger i = 0; ((i < string.length) && success); i++) {
for (NSUInteger i = 0; ((i < string.length)); i++) {
NSString *characterAsString = [NSString stringWithFormat:@"%C", [string characterAtIndex:i]];
NSLog(@"Attempting to type key %@.", characterAsString);

BOOL success = [self typeCharacterAsString:characterAsString
inFullString: string
inFirstResponder:firstResponder
error:errorOrNil
trySwitchingLaunguage:YES];
if (!success) {
return NO;
}
}
return YES;
}

+ (BOOL)typeCharacterAsString:(NSString *)characterAsString
inFullString:(NSString *)string
inFirstResponder:(id)firstResponder
error:(__strong NSError **)errorOrNil
trySwitchingLaunguage:(BOOL)trySwitchingLaunguage {
__block BOOL success = YES;
id key = [GREYKeyboard grey_findKeyForCharacter:characterAsString];
// If key is not on the screen, try looking for it on another keyplane.
if (!key) {
Expand All @@ -187,7 +206,9 @@ + (BOOL)typeString:(NSString *)string
GREYLogVerbose(@"Detected an alphabetic key.");
// Switch to alphabetic keyplane if we are on numbers/symbols keyplane.
if (![GREYKeyboard grey_isAlphabeticKeyplaneShown]) {
id moreLettersKey = [GREYKeyboard grey_findKeyForCharacter:@"more, letters"];
id moreLettersKey = [GREYKeyboard grey_findKeyForCharacter:@"more, letters"]
?: [GREYKeyboard grey_findKeyForCharacter:@"еще, буквы"];

if (!moreLettersKey) {
return [GREYKeyboard grey_setErrorForkeyNotFoundWithAccessibilityLabel:@"more, letters"
forTypingString:string
Expand All @@ -205,7 +226,8 @@ + (BOOL)typeString:(NSString *)string
GREYLogVerbose(@"Detected a non-alphabetic key.");
// Switch to numbers/symbols keyplane if we are on alphabetic keyplane.
if ([GREYKeyboard grey_isAlphabeticKeyplaneShown]) {
id moreNumbersKey = [GREYKeyboard grey_findKeyForCharacter:@"more, numbers"];
id moreNumbersKey = [GREYKeyboard grey_findKeyForCharacter:@"more, numbers"]
?: [GREYKeyboard grey_findKeyForCharacter:@"еще, цифры"];
if (!moreNumbersKey) {
return [GREYKeyboard grey_setErrorForkeyNotFoundWithAccessibilityLabel:@"more, numbers"
forTypingString:string
Expand All @@ -218,14 +240,16 @@ + (BOOL)typeString:(NSString *)string
if (!key) {
if (![GREYKeyboard grey_toggleShiftKeyWithError:errorOrNil]) {
success = NO;
break;
return success;
}
key = [GREYKeyboard grey_findKeyForCharacter:characterAsString];
}
// If key is not on either number or symbols keyplane, it could be on alphabetic keyplane.
// This is the case for @ _ - on UIKeyboardTypeEmailAddress on iPad.
if (!key) {
id moreLettersKey = [GREYKeyboard grey_findKeyForCharacter:@"more, letters"];
id moreLettersKey = [GREYKeyboard grey_findKeyForCharacter:@"more, letters"]
?: [GREYKeyboard grey_findKeyForCharacter:@"еще, буквы"];

if (!moreLettersKey) {
return [GREYKeyboard grey_setErrorForkeyNotFoundWithAccessibilityLabel:@"more, letters"
forTypingString:string
Expand All @@ -236,6 +260,22 @@ + (BOOL)typeString:(NSString *)string
}
}
// If key is still not shown on screen, show error message.
if (!key && trySwitchingLaunguage) {
id changeLanguageKey = [GREYKeyboard grey_findKeyForCharacter:@"Next keyboard"];

if (!changeLanguageKey) {
return [GREYKeyboard grey_setErrorForkeyNotFoundWithAccessibilityLabel:@"Next keyboard"
forTypingString:string
error:errorOrNil];
}
[GREYKeyboard grey_tapKey:changeLanguageKey error:errorOrNil];

return [self typeCharacterAsString:characterAsString
inFullString: string
inFirstResponder:firstResponder
error:errorOrNil
trySwitchingLaunguage:NO];
}
if (!key) {
return [GREYKeyboard grey_setErrorForkeyNotFoundWithAccessibilityLabel:characterAsString
forTypingString:string
Expand All @@ -260,8 +300,7 @@ + (BOOL)typeString:(NSString *)string
// Set the keyboard type back to the Email Type.
[firstResponder setKeyboardType:UIKeyboardTypeEmailAddress];
}
}
return success;
return success;
}

+ (BOOL)waitForKeyboardToAppear {
Expand Down Expand Up @@ -360,16 +399,25 @@ + (id)grey_findKeyForCharacter:(NSString *)character {
BOOL ignoreCase = NO;
// If the key is a modifier key then we need to do a case-insensitive comparison and change the
// accessibility label to the corresponding modifier key accessibility label.
NSString *modifierKeyIdentifier = [gModifierKeyIdentifierMapping objectForKey:character];
if (modifierKeyIdentifier) {
// Check for the return key since we can have a different accessibility label
// depending upon the keyboard.
UIKeyboardImpl *currentKeyboard = [GREYKeyboard grey_keyboardObject];
if ([character isEqualToString:kReturnKeyIdentifier]) {
modifierKeyIdentifier = [currentKeyboard returnKeyDisplayName];
NSArray *modifierKeyIdentifiers = [gModifierKeyIdentifierMapping objectForKey:character];
if (modifierKeyIdentifiers.count > 0) {
for (NSUInteger i = 0; i < modifierKeyIdentifiers.count; ++i) {
NSString *modifierKeyIdentifier = modifierKeyIdentifiers[i];
// Check for the return key since we can have a different accessibility label
// depending upon the keyboard.
UIKeyboardImpl *currentKeyboard = [GREYKeyboard grey_keyboardObject];
if ([character isEqualToString:kReturnKeyIdentifier]) {
modifierKeyIdentifier = [currentKeyboard returnKeyDisplayName];
}
character = modifierKeyIdentifier;
ignoreCase = YES;

id result = [self grey_keyForCharacterValue:character
inKeyboardLayoutWithCaseSensitivity:ignoreCase];
if (result != nil) {
return result;
}
}
character = modifierKeyIdentifier;
ignoreCase = YES;
}

// iOS 9 changes & to ampersand.
Expand Down Expand Up @@ -424,7 +472,9 @@ + (id)grey_keyForCharacterValue:(NSString *)character
+ (BOOL)grey_isAlphabeticKeyplaneShown {
// Arbitrarily choose e/E as the key to look for to determine if alphabetic keyplane is shown.
return [GREYKeyboard grey_findKeyForCharacter:@"e"] != nil
|| [GREYKeyboard grey_findKeyForCharacter:@"E"] != nil;
|| [GREYKeyboard grey_findKeyForCharacter:@"E"] != nil
|| [GREYKeyboard grey_findKeyForCharacter:@"е"] != nil // Cyrillic
|| [GREYKeyboard grey_findKeyForCharacter:@"Е"] != nil; // Cyrillic
}

/**
Expand Down