UIAutomation: Check if element exists before tapping
Asked Answered
P

2

9

We have a iPad app which includes a two-column news reader. The left view contains the list of news of which some link directly to a news and some push another view controller with another list of news. This will also cause a UIButton to be set as the leftBarButtonItem of the navigation bar. If we are on first level, a simple image that cannot be tapped will be the leftBarButtonItem.

My goal is now to have a test that taps every news on the first level. If a news leads to a second level list, it should tap the UIButton in the navigation bar.

How can I check, if the leftBarButtonItem is "tappable"? Since it can be either an image or a button, just calling navigationBar().leftButton().tap() will lead to an error if it's an image.

I'm also using the tuneup library if that's any help.

Prestige answered 8/7, 2012 at 15:4 Comment(0)
R
17

Almost all elements in UIAutomation could be tapped. It does not matter if it is an Image, View or a Button. You will get an error in case an object you are trying to tap is invalid. How to check:

if ( navigationBar().leftButton().checkIsValid() )
{
     navigationBar().leftButton().tap();
}
else
{
     //do what you need.
}

or you can check if an object you are trying to tap is a button, for example (not the best way but it works):

if ( navigationBar().leftButton().toString() == "[object UIAButton]" )
{
    navigationBar().leftButton().tap();
}
else
{
     //do what you need.
}

checkIsValid() is available for all UI elements. It will return true if an object exists. toString() will return [object UIAElementNil] if element is invalid or will return [object UIAButton] or [object UIAImage].

Also try to use Apple documentation: http://developer.apple.com/library/ios/#documentation/ToolsLanguages/Reference/UIAElementClassReference/UIAElement/UIAElement.html

Reyesreykjavik answered 9/7, 2012 at 9:14 Comment(0)
R
0

You can simply use

if (navigationBar().leftButton().exists)
{
     navigationBar().leftButton().tap();
}
else
{
     //do what you need.
}
Reserved answered 10/2, 2016 at 5:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.