Problems with stepToTapViewWithAccessibilityLabel in a UIWebView

1,106 views
Skip to first unread message

jonahgabriel

unread,
Aug 17, 2011, 5:56:56 PM8/17/11
to KIF iOS Automated Testing Framework
Hey guys...just started with KIF and am loving it but ran into a
problem where I am trying to automate a click on a button in a web
view. The accessibility element is located, but I get a log statement
with the following error:

2011-08-17 14:52:09.335 Testable (Integration Tests)[8623:207] FAILING
ERROR: Error Domain=KIFTest Code=0 "The step timed out after 10.00
seconds." UserInfo=0x84fb5d0 {NSUnderlyingError=0x8487140
"Accessibility element with label "Click Me" is not tappable. It may
be blocked by other views.", NSLocalizedDescription=The step timed out
after 10.00 seconds.}

Anyone have any ideas on how to get this to work?

Thanks!

Jonah Neugass

jonahgabriel

unread,
Aug 19, 2011, 6:11:43 PM8/19/11
to KIF iOS Automated Testing Framework
It turns out that the reason that "Click Me" is not tappable is
because it isn't on screen and the step doesn't auto-scroll to the
element.

jonahgabriel

unread,
Aug 19, 2011, 7:18:14 PM8/19/11
to KIF iOS Automated Testing Framework
I modified _accessibilityElementWithLabel to add some log statements
and it appears that sometimes the scrollview doesn't actually scroll
to the element. Below are the log statements and the changes I made to
the code.

2011-08-19 16:14:13.077 Testable (Integration Tests)[19793:207]
Element Frame Origin: x - 10, y - 918
2011-08-19 16:14:13.377 Testable (Integration Tests)[19793:207] Offset
after scroll: x - 0, y - 0
2011-08-19 16:14:13.378 Testable (Integration Tests)[19793:207] FAIL
(4.43s): Tap view with accessibility label "Click Me"
2011-08-19 16:14:13.378 Testable (Integration Tests)[19793:207]
FAILING ERROR: Error Domain=KIFTest Code=0 "The element with
accessibility label Click Me is not tappable" UserInfo=0x12600bb0
{NSLocalizedDescription=The element with accessibility label Empty
Cart is not tappable}



// Scroll the view to be visible if necessary
UIScrollView *scrollView = (UIScrollView *)view;
while (scrollView && ![scrollView isKindOfClass:[UIScrollView
class]]) {
scrollView = (UIScrollView *)scrollView.superview;
}
if (scrollView) {
if ((UIAccessibilityElement *)view == element) {
[scrollView scrollViewToVisible:view animated:YES];
} else {
CGRect elementFrame = [view.window
convertRect:element.accessibilityFrame toView:scrollView];

NSInteger x = elementFrame.origin.x;
NSInteger y = elementFrame.origin.y;

[scrollView scrollRectToVisible:elementFrame
animated:YES];

NSLog(@"Element Frame Origin: x - %d, y - %d", x,
y);
}

// Give the scroll view a small amount of time to perform the
scroll.
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.3, false);
CGPoint offset = scrollView.contentOffset;
NSLog(@"Offset after scroll: x - %d, y - %d", offset.x,
offset.y);
}

jonahgabriel

unread,
Aug 23, 2011, 5:47:44 PM8/23/11
to KIF iOS Automated Testing Framework
I was able to fix the scrolling issues I was having by modifying
_accessibilityElementWithLabel by changing the [scrollView
scrollRectToVisible:] to [scrollView setContentOffset:]. It appears
that there are also issues with trying to tap on a label in an
alertbox generated by jquery on the web page I was on. Removing the
check for tappability fixed this. Here is the modified source:

+ (UIAccessibilityElement *)_accessibilityElementWithLabel:(NSString
*)label accessibilityValue:(NSString *)value tappable:
(BOOL)mustBeTappable traits:(UIAccessibilityTraits)traits error:(out
NSError **)error;
{
UIAccessibilityElement *element = [[UIApplication
sharedApplication] accessibilityElementWithLabel:label
accessibilityValue:value traits:traits];
if (!element) {
if (error) {
// For purposes of a better error message, see if we can
find the view, just not a view with the specified value
if (value && [[UIApplication sharedApplication]
accessibilityElementWithLabel:label accessibilityValue:nil
traits:traits]) {
*error = [[[NSError alloc] initWithDomain:@"KIFTest"
code:KIFTestStepResultFailure userInfo:[NSDictionary
dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Found an
accessibility element with the label \"%@\", but not with the value
\"%@\"", label, value], NSLocalizedDescriptionKey, nil]] autorelease];

// Check the traits too
} else if (traits != UIAccessibilityTraitNone &&
[[UIApplication sharedApplication] accessibilityElementWithLabel:label
accessibilityValue:nil traits:UIAccessibilityTraitNone]) {
*error = [[[NSError alloc] initWithDomain:@"KIFTest"
code:KIFTestStepResultFailure userInfo:[NSDictionary
dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Found an
accessibility element with the label \"%@\", but not with the traits
\"%d\"", label, traits], NSLocalizedDescriptionKey, nil]]
autorelease];

} else {
*error = [[[NSError alloc] initWithDomain:@"KIFTest"
code:KIFTestStepResultFailure userInfo:[NSDictionary
dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to
find accessibility element with the label \"%@\"", label],
NSLocalizedDescriptionKey, nil]] autorelease];
}
}
return nil;
}

// Make sure the element is visible
UIView *view = [UIAccessibilityElement
viewContainingAccessibilityElement:element];
if (!view) {
if (error) {
*error = [[[NSError alloc] initWithDomain:@"KIFTest"
code:KIFTestStepResultFailure userInfo:[NSDictionary
dictionaryWithObjectsAndKeys:[NSString stringWithFormat: @"Cannot find
view containing accessibility element with the label \"%@\"", label],
NSLocalizedDescriptionKey, nil]] autorelease];
}
return nil;
}

// Scroll the view to be visible if necessary
UIScrollView *scrollView = (UIScrollView *)view;
while (scrollView && ![scrollView isKindOfClass:[UIScrollView
class]]) {
scrollView = (UIScrollView *)scrollView.superview;
}
if (scrollView) {
if ((UIAccessibilityElement *)view == element) {
[scrollView scrollViewToVisible:view animated:YES];
} else {
CGRect elementFrame = [view.window
convertRect:element.accessibilityFrame toView:scrollView];
[scrollView setContentOffset:CGPointMake(0,
elementFrame.origin.y - elementFrame.size.height) animated:YES];
}

// Give the scroll view a small amount of time to perform the
scroll.
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.3, false);
}

if ([[UIApplication sharedApplication]
isIgnoringInteractionEvents]) {
if (error) {
*error = [[[NSError alloc] initWithDomain:@"KIFTest"
code:KIFTestStepResultFailure userInfo:[NSDictionary
dictionaryWithObjectsAndKeys:@"Application is ignoring interaction
events", NSLocalizedDescriptionKey, nil]] autorelease];
}
return nil;
}


return element;
}

Jim Puls

unread,
Aug 23, 2011, 10:22:30 PM8/23/11
to kif-fr...@googlegroups.com
Put this in a pull request?

Rock on!

-> jp

jonahgabriel

unread,
Aug 24, 2011, 5:03:20 PM8/24/11
to KIF iOS Automated Testing Framework
Just sent a request...this is my first time using git and github to do
anything more than download, so I hope everything came though ok.
Reply all
Reply to author
Forward
0 new messages