
Paulo Andrade
If your Mac app uses NSScrollViews and you’re writing UI tests, eventually you’ll have the need to click a cell, button or view that is outside the scrollview’s view port.
Turns out the solution is quite simple but since this is a fairly common problem I felt it was worthy of a blog post.1.
@implementation XCUIElement (Additions)
- (BOOL)scrollRevealing:(XCUIElement *)element
{
if(self.elementType != XCUIElementTypeScrollView){
return NO;
}
CGRect scrollFrame = [self frame];
CGRect elementFrame = [element frame];
// figure out if we need to scroll up or down
CGFloat direction = elementFrame.origin.y < scrollFrame.origin.y ? 1 : -1;
while(!CGRectContainsRect(scrollFrame, elementFrame)){
[self scrollByDeltaX:0 deltaY:direction * 50];
CGRect newElementFrame = [element frame];
if (CGRectEqualToRect(elementFrame, newElementFrame)) {
// scrolling did not move the element
// either the element is not on this scroll view or
// we've scrolled the entire scrollview
break;
}
elementFrame = newElementFrame;
}
return CGRectContainsRect(scrollFrame, elementFrame);
}
@end
The implemention is fairly easy to understand but here’s a quick run down:
XCUIElement
of the scrollview type. This will only work if that’s the case. I tried finding a way to get the enclosing scroll view and make this work on any element but couldn’t. If you know of a way to do that please let me know!Happy testing!
At least I wish I’d found one when I had to implement this… ↩︎