Interacting with Elements
Everything what you see in an app is a visual representation of native element. These elements refers to text boxes, checkboxes, buttons, radio buttons, spinners and many more that display or accept data from the user. These elements are designed using native code. When we interact with these elements, we are interacting with these native codes.
In this chapter we will learn how to interact with different native elements.
The most commonly used native elements are as follows:-
- Textbox
- Button
- Labels
- Radio buttons
- Checkboxes
- Spinners (Dropdown)
- Lists
To interact with these elements we need to identify it first. This is done with the help of locators. You need to find the right and unique locator for the element and use the web related methods to interact with these elements.
There are two methods provided by appium to find the element.
- Find Single Element
- Find List of Elements Using the findElements() method of Appium, more than one element matching the locator can be retrieved. This is when the targeted element is part of a list or with common locator and can't be identified uniquely.
Using findElement() method of Appium, unique single element can be located. Here the expectation is that the locator is unique and will return a single element.
Click
Click method is used to click/tap on an element. It can be used on any element which accepts click/tap event, but the most ideal use case are on the following elements:-
- Button
- Radio button
- Checkbox
- Spinner item
- List item
This method will click the element on its in-view center point. If the element's center point is obscured by another element, an element click intercepted error is returned. If the element is outside the viewport, an element not interactable error is returned.
import io.appium.java_client.AppiumBy; import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.android.options.UiAutomator2Options; import org.openqa.selenium.WebElement; import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.remote.RemoteWebDriver; public class AppiumTest { public static void main(String[] args) throws MalformedURLException { UiAutomator2Options options = new UiAutomator2Options(); options.setPlatformName("Android"); options.setDeviceName("Pixel_4_Emulator"); options.setAutomationName("UiAutomator2"); options.setApp("/path/to/your/app.apk"); URL appiumServerUrl = new URL("http://localhost:4723"); RemoteWebDriver driver = new AndroidDriver<>(appiumServerUrl, options); WebElement loginButton = driver.findElement(By.id("loginButton")); loginButton.click(); driver.quit(); } }
Send Keys
The sendKeys method types the provided Char Sequence into an editable element. Typically, this means an element is an input element or some sort of textbox which accepts user inputs. If it is not editable, an invalid element state exception is thrown.
import io.appium.java_client.AppiumBy; import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.android.options.UiAutomator2Options; import org.openqa.selenium.WebElement; import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.remote.RemoteWebDriver; public class AppiumTest { public static void main(String[] args) throws MalformedURLException { UiAutomator2Options options = new UiAutomator2Options(); options.setPlatformName("Android"); options.setDeviceName("Pixel_4_Emulator"); options.setAutomationName("UiAutomator2"); options.setApp("/path/to/your/app.apk"); URL appiumServerUrl = new URL("http://localhost:4723"); RemoteWebDriver driver = new AndroidDriver<>(appiumServerUrl, options); driver.findElement(By.id("username")).sendKeys("admin"); driver.findElement(By.id("password")).sendKeys("admin"); driver.findElement(By.id("loginButton")).click(); driver.quit(); } }
Clear
The clear method resets the content of an element. This requires an element to be editable. The element should be of a textbox or editable text.
getAttribute
The getAttribute method returns the value of an element's attribute as a string. If there is no attribute, this method will return null. This method is commonly used to get attribute value from an element.
getTagName
This method will return the tag name of an element. This is used mainly to validate if the given element is of a specific type. Example to see if the element is a textbox or label.
getText
This method will return the text of the given element, including the sub-elements.
isDisplayed
This method will check if the element is displayed or not. It will return true if the element is displayed else will return false. This operation will work on all elements.
isEnabled
This method will check if the element is enabled. It will return false if the element is disabled else will return true. This operation will work on all elements.