When we doing test automation for iPhone or iPad using the UISpec framework we have to pay attention to the existence or not of the UI elements we interact with. If we try to touch a button, fill in a text field o perform any kind of interaction with an element and the element doesn't exist the Test application will crash and will stop the execution of the rest of specs. So, we have to check the existence of the element before its interaction. We can avoid the crash setting expectations before interact with the element. Let's imagine a screen with two buttons adn we wan to touch the second one, the UIquery of the button is
[[[app button] index:1] touch];
so for avoid the crash we need to set the expectation before the touch
[expectThat([app button] index:1]) should:be(YES)];
[[[app button] index:1] touch];
as the expectation will not be satisfied the execution of this spec will finish at this point and it doesn't try to touch it. But the execution application won't stop here.
A more elaborated implementation could be
BOOL existElement;
@try {
existElement = [[[[app button] index:1] should] exist];
}
@catch (NSException *exception) {
NSLog(@"EXCEPTION CAUSED BY: %@", [NSThread callStackSymbols]);
}
@finally {
[expectThat(existElement) should:be(YES)];
}
Avoiding crashes - dejalatele's personal blog
Tracked: Jan 25, 09:34