The main method for interacting with a Device is using Elements. The Elements can execute actions like clicks, scrolls, input text, double taps, long press and so on. There are also some specific Elements like ScrollableView
, DatePicker
and TimePicker
that have additional functionality. The main method for creating Elements is by using an ElementSelector
which contains the attributes that the desired Element should have. Note that if there are two or more Elements with the same attributes an error occurs. We can get an instance of an Element from the Active Screen. More information about the Active Screen and how to create a Screen instance can be found here.
- Create a
Screen
instance. - Create an
ElementSelector
and addCssAttributes
.- Creating and adding
CssAttributes
is explained here.
- Creating and adding
- Use the
screen.getElement(selector)
method and pass the createdElementSelector
as an argument.
Example code:
Screen screen = device.getActiveScreen();
UiElementSelector elementSelector = new UiElementSelector();
elementSelector.addSelectionAttribute(CssAttribute.CLASS_NAME, "android.widget.TextView");
elementSelector.addSelectionAttribute(CssAttribute.TEXT, "Browser");
UiElement browserElement = screen.getElement(elementSelector);
- Create a
Screen
instance. - Construct a
CssQuery
that contains Element attributes.
- More information about constructing a
CssQuery
can be found here.
- Use the
screen.getElementByCSS(query)
method and pass the createdCssQuery
as an argument.
Example code:
Screen screen = device.getActiveScreen();
final String CSS_QUERY = "[bounds=[37,27][99,46]][class=android.widget.TextView]";
UiElement textViewElement = screen.getElementByCSS(CSS_QUERY);
- Create a
Screen
instance. - Construct an
xPathQuery
that contains Element attributes.
- More information about constructing an
XpathQuery
can be found here.
- Use the
screen.getElementByXPath(xPathquery)
method and pass the createdxPathQuery
as an argument.
Example code:
Screen screen = device.getActiveScreen();
final String XPATH_QUERY = "//node[@class='android.widget.ImageView']";
UiElement textViewElement = screen.getElementByXPath(XPATH_QUERY);
This method is useful when we want to start an Application or execute some action which might take some time to load the next screen. As you know you can't get or interact with an Element that is not present on the active screen, so in some cases you need to wait for the Element
to show.
- Create a
Screen
instance. - Create an
ElementSelector
and addCssAttributes
.
- Creating and adding
CssAttributes
is explained here.
- Use the
screen.getElementWhenPresent(selector, timeout)
method using the createdElementSelector
and a given timeout.
- Timeout is the time you are willing to wait for the Element to show on the screen. If there is no such Element on the screen after that time, an error will occur.
- Timeout is set in milliseconds, so if you want to wait for a second, give a timeout of 1000.
- Note that the
getElementWhenPresent()
method contains a default timeout, so it's not necessary to always pass a timeout parameter.
Example Code:
Screen screen = device.getActiveScreen();
UiElementSelector elementSelector = new UiElementSelector();
elementSelector.addSelectionAttribute(CssAttribute.CLASS_NAME, "android.widget.TextView");
elementSelector.addSelectionAttribute(CssAttribute.TEXT, "Browser");
UiElement browserElement = screen.getElementWhenPresent(elementSelector, 3000);
There is an option for getting many Elements with common Attributes. For example, we could get all Elements that have the class name android.widget.EditText
. They can also be selected by CssQuery, XPath and ElementSelector
. These methods return List<UiElement>
.
You can get the Children of an Element by a given selector. For example, we could get all TextView
Elements inside a FrameLayout
Element. More information about how to get Children Elements can be found here.
You can select an Element with the attributes in NoteDetails in the Dump XML. Every Element has these attributes and can be selected by one or more of them. Also they contain fields like Enabled
, Checked
, Focused
and others. With them we can check if the interactions are successful or not. For more information you can check How to dump XML.