Android smallest UI unit is View
. However, in Appium
, the unit is called element
instead. We find element
instead of View
.
Before we take any action on the screen, we have to locate the element
first.There are various way to findElement
from your codes. But some of them don't work well, so I gather here what work as expected for me.
Most of them rely on Ui Automation framework
. After that, we take action by using built-in methods of element
. For example,
element.click()
to click an element or element.getText()
to get word from that element.
You should declare a field variable driver
. As follows:
protected static AndroidDriver<AndroidElement> driver;
Text in this case is a word that is shown on the screen. It will return the first element that has a text Graphics
.
driver.findElementByAndroidUIAutomator("text(\"Graphics\")");
resourceId
is an attribute of element
. You can obtain it by using uiautomatorviewer
. It will return the first element that has id of
io.appium.android.apis:id/edit
.
driver.findElementByAndroidUIAutomator("resourceId(\"io.appium.android.apis:id/edit\")");
This is the same as Find Element by Text but this return a list of elements instead of one.
driver.findElementsByAndroidUIAutomator("text(\"Graphics\")");
This is the same as Find Element by resourceId
but this return a list of elements instead of one.
driver.findElementsByAndroidUIAutomator("resourceId(\"io.appium.android.apis:id/edit\")");
Scrolling within android:id/list
to the element that has a text of Views
.
driver.findElementByAndroidUIAutomator("new UiScrollable(resourceId(\"android:id/list\")).scrollIntoView(text(\"Views\"));")
Get element index of 1
from an element list that has a resourceId
of android:id/text
.
driver.findElementByAndroidUIAutomator("resourceId(\"android:id/text1\").instance(1)");
AppiumFlow for Android.