Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
KIF enter text in UITextField inside UIWebView
If you are the only controlling the HTML, change it so that your input field has default values for accessibility. You can do this via several of the attributes. If you don't have access to the HTML...
You can inject javascript into your webview and change the title attribute of the <input> tags, giving them a default and static accessibilityLabel.
NSString *result = [self.webView stringByEvaluatingJavaScriptFromString:@"var inputs = document.getElementsByTagName('input');"
"for (var index = 0; index < inputs.length; index++){inputs[index].title = 'input ' + index;}"
Here I give the nth <input> tag an accessibility label of "input n".
However, you're not done yet. Using stepToEnterText:intoViewWithAccessibilityLabel: will fail because at the end, the KIF framework tries to compare the value of the text you entered to the text in the label, which errors because UIBrowserWebView obscures the view. You need to implement your own stepToEnterText: method that purely presses keyboard buttons
+ (id)stepToEnterText:(NSString *)text
NSString *description = [NSString stringWithFormat:@"Type the text \"%@\"", text];
return [self stepWithDescription:description executionBlock:^(KIFTestStep *step, NSError **error){
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.5, false);
for (NSUInteger characterIndex = 0; characterIndex < [text length]; characterIndex++)
NSString *characterString = [text substringWithRange:NSMakeRange(characterIndex, 1)];
if (![self performSelector:@selector(_enterCharacter:) withObject:characterString])
//no other option for us
return KIFTestStepResultSuccess;
Now we can go somewhere:
unfortunately, you'll have to go to figure out which input field you want.
[scenario addStep:[KIFTestStep stepToTapViewWithAccessibilityLabel:@"input 7"]]; // go to the 7th input field
[scenario addStep:[KIFTestStep stepToEnterText:@"admin"]]; //enters "admin" into that UITextField
*rant* First I spent hours looking through the Apple private apis of UIBrowserWebView and UIDocumentWebView to try and access the hidden subviews of UIWebView to get to the UITextField and change the accessibility label to something more appropriate to no avail. *end rant*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment