SLUIAElement Class Reference
Inherits from | NSObject |
Declared in | SLUIAElement.h |
Overview
SLUIAElement
is an abstract class that defines an interface to
access and manipulate a user interface element within your application.
Concrete subclasses of SLUIAElement
, SLElement
and SLStaticElement
,
handle the specifics of matching particular elements.
The methods in the SLUIAElement (Subclassing)
category are to be called
or overridden by subclasses of SLUIAElement
. Tests should not call these
methods.
Tasks
Configuring the Default Timeout
-
+ setDefaultTimeout:
Establishes the value of the grace period during which instances
SLUIAElement
wait to establish access to their corresponding interface elements. -
+ defaultTimeout
The value of the grace period during which instances of
SLUIAElement
wait to establish access to their corresponding interface elements.
Determining Element State
-
– isValid
Determines whether a user interface element matching the specified
SLUIAElement
currently exists. -
– isVisible
Determines whether the specified element is visible on the screen.
-
– isValidAndVisible
Determines whether the specified element is valid, and if so, if it is visible on the screen.
-
– isInvalidOrInvisible
Determines whether the specified element is invalid or, if it is valid, if it is invisible.
-
– isEnabled
Determines whether the specified element is enabled.
-
– isTappable
Determines whether it is possible to interact with the specified element.
-
– hasKeyboardFocus
Determines whether the specified element currently receives keyboard input.
Gestures and Actions
-
– tap
Taps the specified element.
-
– doubleTap
Double-taps the specified element.
-
– touchAndHoldWithDuration:
Touches and holds the specified element.
-
– dragWithStartOffset:endOffset:
Drags within the bounds of the specified element.
Identifying Elements
-
– scrollToVisible
If this element has a UIScrollView or UIWebview ancestor, then that ancestor will be scrolled until this element is visible.
-
– label
Returns the element’s label.
-
– value
Returns the element’s value.
Determining Element Positioning
-
– hitpoint
Returns the screen position to tap for the element.
-
– rect
Returns the location and size of the object.
Logging Element Information
-
– logElement
Logs information about the specified element.
-
– logElementTree
Logs information about the element hierarchy rooted in the specified element.
Screenshot an element
-
– captureScreenshotWithFilename:
Takes a screenshot of the specified element.
Methods for Subclasses
-
– waitUntilTappable:thenSendMessage:
Forwards an action message to the
UIAElement
corresponding to the specified element and returns the result. -
– waitUntilTappable:thenPerformActionWithUIARepresentation:timeout:
Provides access to the UIAutomation representation of the specified element within a specified block.
-
+ SLElementIsTappableFunctionName
Returns the name of the JavaScript function used to evaluate whether a
UIAElement
is tappable, loading it into the terminal’s namespace if necessary. -
– canDetermineTappability
Determines whether the specified element’s response to
isTappable
is valid.
Class Methods
SLElementIsTappableFunctionName
Returns the name of the JavaScript function used to evaluate whether a
UIAElement
is tappable, loading it into the terminal’s namespace if necessary.
+ (NSString *)SLElementIsTappableFunctionName
Return Value
The name of the JavaScript function used to evaluate whether a
UIAElement
is tappable.
Discussion
This method is used internally by SLUIAElement
and its subclasses
SLElement
and SLStaticElement
. It should not need to be used by additional
descendants of SLUIAElement
.
Declared In
SLUIAElement+Subclassing.h
defaultTimeout
The value of the grace period during which instances of SLUIAElement
wait to establish access to their corresponding interface elements.
+ (NSTimeInterval)defaultTimeout
Return Value
The value of the grace period that instances of SLUIAElement
use
to establish access to their corresponding interface elements.
Discussion
When asked to access their corresponding elements, instances of SLUIAElement
automatically wait for those elements to become valid, for not more
than this timeout value. If that access requires simulating user interaction,
instances of SLUIAElement
will additionally wait, for the remainder of the
timeout, for their corresponding elements to become tappable.
This means that tests rarely need to wait for the user interface to update before attempting to access or interact with interface elements.
See Also
Declared In
SLUIAElement.h
setDefaultTimeout:
Establishes the value of the grace period during which instances SLUIAElement
wait to establish access to their corresponding interface elements.
+ (void)setDefaultTimeout:(NSTimeInterval)timeout
Parameters
- timeout
The new value of the “grace period” that instances of
SLUIAElement
use to establish access to their corresponding interface elements.
Discussion
Clients change this value by setting the default timeout used by the shared test controller ([SLTestController setDefaultTimeout:]) before testing begins.
See Also
Declared In
SLUIAElement.h
Instance Methods
canDetermineTappability
Determines whether the specified element’s response to isTappable
is valid.
- (BOOL)canDetermineTappability
Discussion
This should return YES
unless the specified element identifies an instance
of UIScrollView
and tests are running on an iPad simulator or device running
iOS 5.x. On those platforms, UIAutomation reports that scroll views are always
invisible, and thus not tappable.
If this method returns NO
, tappability will not be enforced as a prerequisite
for simulating user interaction. This will let Subliminal attempt interaction
with scroll views despite UIAutomation’s response. Testing reveals that certain
forms of interaction, e.g. dragging, will yet succeed (barring factors like the
scroll view actually being hidden or having user interaction disabled, etc.).
Exceptions
- SLUIAElementInvalidException
Raised if the element is not valid.
Declared In
SLUIAElement+Subclassing.h
captureScreenshotWithFilename:
Takes a screenshot of the specified element.
- (void)captureScreenshotWithFilename:(NSString *)filename
Parameters
- filename
An optional string to use as the name for the resultant image file (provide nil to use a generic name.).
Discussion
The image is viewable from the UIAutomation debug log in Instruments.
When running subliminal-test
from the command line, the images are also saved
as PNGs within the specified output directory.
Exceptions
- SLUIAElementInvalidException
Raised if the element is not valid by the end of the default timeout.
Declared In
SLUIAElement.h
doubleTap
Double-taps the specified element.
- (void)doubleTap
Discussion
Bug: This method will fail if the specified element identifies a scroll view
in an application running on an iPad simulator or device running iOS 5.x, due
to a bug in UIAutomation (see isTappable). An SLUIAElementAutomationException
will be raised in such a scenario.
UIAutomation is able to doubletap scroll view child elements regardless of platform.
Exceptions
- SLUIAElementInvalidException
Raised if the element is not valid by the end of the default timeout.
- SLUIAElementNotTappableException
Raised if the element is not tappable when whatever amount of time remains of the default timeout after the element becomes valid elapses.
Declared In
SLUIAElement.h
dragWithStartOffset:endOffset:
Drags within the bounds of the specified element.
- (void)dragWithStartOffset:(CGPoint)startOffset endOffset:(CGPoint)endOffset
Parameters
- startOffset
The offset, within the element’s rect, at which to begin dragging.
- endOffset
The offset, within the element’s rect, at which to end dragging.
Discussion
Each offset specifies a pair of x and y values, each ranging from 0.0
to 1.0
.
These values represent, respectively, relative horizontal and vertical positions
within the element’s rect
, with {0.0, 0.0}
as the top left and {1.0, 1.0}
as the bottom right.
This method uses a drag duration of 1.0 seconds (the documented default duration
for touch-and-hold gestures according to Apple’s UIAElement
class reference).
Bug: On simulators running iOS 7.x, UIAutomation drag gestures do not cause
scroll views to scroll. Instances of SLElement
are able to work around this;
instances of SLStaticElement
are not.
Exceptions
- SLUIAElementInvalidException
Raised if the element is not valid by the end of the default timeout.
- SLUIAElementNotTappableException
Raised if the element is not tappable when whatever amount of time remains of the default timeout after the element becomes valid elapses.
Declared In
SLUIAElement.h
hasKeyboardFocus
Determines whether the specified element currently receives keyboard input.
- (BOOL)hasKeyboardFocus
Return Value
YES
if the user interface element represented by the specified element
is the current receiver of keyboard input, NO
otherwise.
Discussion
That is, whether the receiver is first responder.
Exceptions
- SLUIAElementInvalidException
Raised if the element is not currently valid.
Declared In
SLUIAElement.h
hitpoint
Returns the screen position to tap for the element.
- (CGPoint)hitpoint
Return Value
The position to tap for the user interface element represented by the
specified element, in screen coordinates, or SLCGPointNull
if such a position
cannot be determined.
Discussion
Below iOS 7, this defaults to the midpoint of the element’s rect
;
at or above iOS 7, this defaults to the element’s accessibility activation point accessibilityActivationPoint]).
If the default hitpoint cannot be tapped, this method returns an alternate point, if possible.
Exceptions
- SLUIAElementInvalidException
Raised if the element is not valid by the end of the default timeout.
Declared In
SLUIAElement.h
isEnabled
Determines whether the specified element is enabled.
- (BOOL)isEnabled
Return Value
YES
if the user interface element represented by the specified element
is enabled, NO
otherwise.
Discussion
The user interface element represented by this element defines what
“enabled” means. When the interface element is an instance of UIControl
,
this returns the value of that control’s -isEnabled
property.
This method otherwise appears to evaluate to YES
.
Exceptions
- SLUIAElementInvalidException
Raised if the element is not valid by the end of the default timeout.
Declared In
SLUIAElement.h
isInvalidOrInvisible
Determines whether the specified element is invalid or, if it is valid, if it is invisible.
- (BOOL)isInvalidOrInvisible
Return Value
YES
if the user interface element represented by the specified element
does not exist or is invisible, NO
otherwise.
Discussion
Unlike isVisible
, this method will not raise an SLUIAElementInvalidException
if the element is not valid—it will just return NO
. This allows test writers
to wait for an element to disappear without having to worry about whether
the element will become invisible or simply invalid.
Declared In
SLUIAElement.h
isTappable
Determines whether it is possible to interact with the specified element.
- (BOOL)isTappable
Return Value
YES
if it is possible to tap on or otherwise interact with the user
interface element represented by the specified element, NO
otherwise.
Discussion
All methods that simulate user interaction with an element require that
that element be tappable. When a method involving user interaction is invoked
on an SLUIAElement
, that method will wait up to the default timeout
for the element to become valid, and then will wait for the remainder
of the timeout for the element to become tappable. If the element does not
become tappable within that interval, the method will raise an
SLUIAElementNotTappableException
.
This method returns, or raises an SLUIAElementInvalidException
, immediately.
Use of the UIAElement
macro can help tests diagnose tappability failures.
Warning: Visibility correlates very closely with tappability, but in some circumstances, an element may be tappable even if it is not visible, and vice versa, due to bugs in UIAutomation. See the implementation for details. Tests should assert visibility separately if desired.
Bug: UIAutomation reports that scroll views are never tappable in applications running on iPad simulators or devices running iOS 5.x. On such platforms, Subliminal attempts to interact with scroll views despite UIAutomation reporting that they are not tappable. Testing reveals that tapping scroll views on an iPad simulator or device running iOS 5.x will fail, but dragging will succeed (barring factors like the scroll view actually being hidden or having user interaction disabled, etc.).
UIAutomation correctly reports scroll view child elements as tappable regardless of platform.
Exceptions
- SLUIAElementInvalidException
Raised if the element is not currently valid.
See Also
Declared In
SLUIAElement.h
isValid
Determines whether a user interface element matching the specified SLUIAElement
currently exists.
- (BOOL)isValid
Return Value
YES
if a matching element currently exists, NO
otherwise.
Discussion
An element’s state can be accessed if and only if it is valid.
This method returns immediately. All other methods, unless marked otherwise,
wait up to default timeout for the element to become valid,
and raise an SLUIAElementInvalidException
if the element does not become valid
within that interval.
Use of the UIAElement
macro can help tests diagnose validity failures.
Validity is not cached, but is determined anew every time a SLUIAElement
is
asked to access a corresponding interface element.
See Also
Declared In
SLUIAElement.h
isValidAndVisible
Determines whether the specified element is valid, and if so, if it is visible on the screen.
- (BOOL)isValidAndVisible
Return Value
YES
if the user interface element represented by the specified element
both exists and is visible, NO
otherwise.
Discussion
This method returns immediately, but unlike isVisible
, will not raise
an SLUIAElementInvalidException
if the element is not valid—it will
just return NO
. This allows test writers to wait for an element to appear
without having to worry about whether the element is valid at the start of the wait.
Declared In
SLUIAElement.h
isVisible
Determines whether the specified element is visible on the screen.
- (BOOL)isVisible
Return Value
YES
if the user interface element represented by the specified element
is visible on-screen, NO
otherwise.
Discussion
This method returns, or raises an SLUIAElementInvalidException
, immediately.
Exceptions
- SLUIAElementInvalidException
Raised if the element is not currently valid. For this reason, developers are encouraged to use
isValidAndVisible
andisInvalidOrInvisible
withSLAssertTrueWithTimeout
, rather than this method.
Declared In
SLUIAElement.h
label
Returns the element’s label.
- (NSString *)label
Return Value
The accessibility label of the user interface element represented by the specified element.
Exceptions
- SLUIAElementInvalidException
Raised if the element is not valid by the end of the default timeout.
Declared In
SLUIAElement.h
logElement
Logs information about the specified element.
- (void)logElement
Discussion
The information that is logged is determined by UIAutomation.
Exceptions
- SLUIAElementInvalidException
Raised if the element is not valid by the end of the default timeout.
See Also
[NSObject slAccessibilityDescription]
Declared In
SLUIAElement.h
logElementTree
Logs information about the element hierarchy rooted in the specified element.
- (void)logElementTree
Discussion
The information that is logged is determined by UIAutomation.
Exceptions
- SLUIAElementInvalidException
Raised if the element is not valid by the end of the default timeout.
See Also
[NSObject slRecursiveAccessibilityDescription]
Declared In
SLUIAElement.h
rect
Returns the location and size of the object.
- (CGRect)rect
Return Value
The accessibility frame of the user interface element represented by the specified element.
Discussion
The relevant coordinates are screen-relative and are adjusted to account for device orientation.
Exceptions
- SLUIAElementInvalidException
Raised if the element is not valid by the end of the default timeout.
Declared In
SLUIAElement.h
scrollToVisible
If this element has a UIScrollView or UIWebview ancestor, then that ancestor will be scrolled until this element is visible.
- (void)scrollToVisible
Discussion
Warning: This method should only be called on elements that are within a UIScrollView or UIWebView.
Exceptions
- SLUIAElementAutomationException
if the element is not within a UIScrollView or UIWebView
Declared In
SLUIAElement.h
tap
Taps the specified element.
- (void)tap
Discussion
The tap occurs at the element’s hitpoint.
Bug: This method will fail if the specified element identifies a scroll view
in an application running on an iPad simulator or device running iOS 5.x, due
to a bug in UIAutomation (see isTappable). An SLUIAElementAutomationException
will be raised in such a scenario.
UIAutomation is able to tap on scroll view child elements regardless of platform.
Exceptions
- SLUIAElementInvalidException
Raised if the element is not valid by the end of the default timeout.
- SLUIAElementNotTappableException
Raised if the element is not tappable when whatever amount of time remains of the default timeout after the element becomes valid elapses.
Declared In
SLUIAElement.h
touchAndHoldWithDuration:
Touches and holds the specified element.
- (void)touchAndHoldWithDuration:(NSTimeInterval)duration
Parameters
- duration
The duration for the touch.
Declared In
SLUIAElement.h
value
Returns the element’s value.
- (NSString *)value
Return Value
The accessibility value of the user interface element represented by the specified element.
Exceptions
- SLUIAElementInvalidException
Raised if the element is not valid by the end of the default timeout.
Declared In
SLUIAElement.h
waitUntilTappable:thenPerformActionWithUIARepresentation:timeout:
Provides access to the UIAutomation representation of the specified element within a specified block.
- (void)waitUntilTappable:(BOOL)waitUntilTappable thenPerformActionWithUIARepresentation:(void ( ^ ) ( NSString *UIARepresentation ))block timeout:(NSTimeInterval)timeout
Parameters
- waitUntilTappable
If
YES
, andcanDetermineTappability
returnsYES
, this method will wait for the remainder of timeout, after the element becomes valid, for the element to become tappable.
- block
A block which takes the UIAutomation representation of the specified element as an argument and returns
void
.
- timeout
The timeout for which this method should wait for the specified element to become valid (and tappable, if waitUntilTappable is
YES
). Clients should generally call this method with[SLUIAElement defaultTimeout]
.
Discussion
The UIAutomation representation identifies the UIAElement
corresponding to
the specified element to UIAutomation.
This method allows an element to evaluate more complex JavaScript expressions
involving its corresponding UIAElement
than simple function calls (for which
waitUntilTappable:thenSendMessage:
may be used); or to use a non-standard
timeout for resolving the corresponding UIAElement
.
This method waits until the element is valid before attempting to
access the UIAElement
, for not more than timeout. If waitUntilTappable
is YES
, the method will also wait, for the remainder of timeout, for the
element to become tappable.
Warning: If the expression to be evaluated by block involves user interaction,
the caller must pass YES
for waitUntilTappable.
Exceptions
- SLUIAElementInvalidException
Raised if the element is not valid by the end of timeout.
- SLUIAElementNotTappableException
Raised if the element waits for tappability and is not tappable when whatever amount of time remains of timeout, after the element becomes valid, elapses.
Declared In
SLUIAElement+Subclassing.h
waitUntilTappable:thenSendMessage:
Forwards an action message to the UIAElement
corresponding to the
specified element and returns the result.
- (id)waitUntilTappable:(BOOL)waitUntilTappable thenSendMessage:(NSString *)action, ...
Parameters
- waitUntilTappable
If
YES
, andcanDetermineTappability
returnsYES
, this method will wait for the remainder of the default timeout, after the element becomes valid, for the element to become tappable.
- action
A format string (in the manner of
[NSString stringWithFormat:]
) representing a JavaScript function to be called on the correspondingUIAElement
.
- ...
(Optional) A comma-separated list of arguments to substitute into
action
.
Return Value
The value returned by the function, as an Objective-C object. See
[SLTerminal eval:]
for more infomation.
Discussion
The “message” is a formatted function call. For instance, SLUIAElement
calls
tap
() on its corresponding UIAElement
by invoking
[self waitUntilTappable:YES thenSendMessage:@"tap()"]
This method automatically waits until the element is valid before
attempting to access the UIAElement
, for not more than the default timeout.
If waitUntilTappable is YES
, the method will also wait, for the remainder
of the timeout, for the element to become tappable.
Warning: All methods that involve user interaction must pass YES
for
waitUntilTappable.
Warning: Variable arguments that are strings need to be escaped,
using [NSString slStringByEscapingForJavaScriptLiteral]
,
if they are to be substituted into a JavaScript string literal.
Exceptions
- SLUIAElementInvalidException
Raised if the element is not valid by the end of the default timeout.
- SLUIAElementNotTappableException
Raised if the element waits for tappability and is not tappable when whatever amount of time remains of the default timeout, after the element becomes valid, elapses.
Declared In
SLUIAElement+Subclassing.h