Interacting with Native Elements

Demo Video

Switch to the native context

Ruby:

driver.switch_to.window('NATIVE_APP')

Java:

driver.switchTo().window("NATIVE_APP");

When a selendroid test session is started, by default the native mode is activated.

Supported Element Locators

Id
Finds the element by Id.
name
Finds the element by content description (accessibility label).
link text
Finds the element by text.
partial link text
Finds the element by partial text.
class
Finds the element by full class name (e.g. android.widget.Button).
xpath
Finds the element by a xpath expression.
tag name
Finds the element by tag name.

Native locator examples

Sample app view hierarchy in the inspector:

By Id

Means the id that is described in the layout xml file of the corresponding activity.

Ruby:

driver.find_element(:id,'buttonTest')

Java:

driver.findElement(By.id("buttonTest"));

By Name

Is mapped to the accessibility label (content description) of the view element.

Ruby:

driver.find_element(:name,'buttonTestCD')

Java:

driver.findElement(By.name("buttonTestCD"));

By Link text

Mapped to the displayed text of the element.

Ruby:

driver.find_element(:link_text,'EN Button')

Java:

driver.findElement(By.linkText("EN Button"));

By Partial Link Text

Mapped to the displayed text of the element.

Ruby:

driver.find_element(:partial_link_text,'EN Butto')

Java:

driver.findElement(By.partialLinkText("EN Butto"));

By Class

Mapped to the ui element class of the view.

Ruby:

driver.find_element(:class_name,'android.widget.Button')

Java:

driver.findElement(By.className("android.widget.Button"));

By Tag Name

Mapped to the simple name of the ui element class of the view.

Ruby:

driver.find_element(:tag_name,'Button')

Java:

driver.findElement(By.tagName("Button"));

By XPath

XPath is the language used for locating nodes in an XML document. XPath extends beyond (as well as supporting) the simple methods of locating by id or name attributes, and opens up all sorts of new possibilities such as locating the third Button on the activity.

Ruby:

driver.find_element(:xpath,"//Button[@id='buttonTest']")

Java:

driver.findElement(By.xpath("//Button[@id='buttonTest']"));

Supported Element Interactions

  • Get Text
  • Click
  • Send Keys
  • Get Attribute of Element
  • Clear
  • Is Selected
  • Is Displayed
  • Is Enabled
  • Get Size
  • Get Location

Interacting with the App using special keys

Selendroid offers you to interact with the app using some special keys e.g. like the Android menu button.

Ruby:

driver.keyboard.send_keys("\uE102")

Java:

/* When using SelendroidDriver */
new Actions(driver).sendKeys(SelendroidKeys.MENU).perform();

Python:

from selenium.webdriver.common.action_chains import ActionChains;
""" Instantiate the driver like: driver = driver=webdriver.Remote( ... ); """
chain = ActionChains(driver);
""" Send search key"""
chain.send_keys(u'\ue103').perform();

Overview about available keys

The original list is documented in the interface: SelendroidKeys

KEY
Unicode Character
ALT_LEFT
\uE00A
DEL
\uE017
DPAD_DOWN
\uE017
DPAD_LEFT
\uE012
DPAD_RIGHT
\uE014
DPAD_UP
\uE013
ENTER
\uE007
SHIFT_LEFT
\uE008
BACK
\uE100
ANDROID_HOME
\uE101
MENU
\uE102
SEARCH
\uE103
SYM
\uE104
ALT_RIGHT
\uE105
SHIFT_RIGHT
\uE106