4

Finding elements using Appium Inspector

 2 years ago
source link: https://blog.knoldus.com/finding-elements-using-appium-inspector/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Reading Time: 4 minutes

In the previous blog, we had seen how we can install Appium on our local system. So in this blog, we will see how we can use Appium inspector and find different types of elements in them. So lets first see why we require Appium inspector?

Why Appium Inspector?

Testing a mobile application and a web application is different from one another. The main difference is the locator strategy which we use to identify the elements of the application. For finding the elements on a web application we can use the element inspector of the web browser. But when we talk about the mobile application we don’t have an element inspector available. For this, we can use the Appium inspector for locating the elements in the application.

Appium Inspector

Appium inspector is an inbuilt feature of Appium which identifies different types of elements in our mobile and web application. As we all know that when we are trying to automate any application on mobile or web, we should be able to uniquely identify every element of the application. To do that we need to find out some attributes or information about the element which we need to interact with.

Appium inspector uses emulators and real-world devices as well. It simply mirrors the screen of the device and we can interact with it and get the details of the elements. There are different ways to uniquely identify elements in mobile applications.

Element locator strategy in Appium

Using the element locator we can find the address of a UI element in an application. As it is very common to have multiple elements of the same type being present on the screen it is possible that two or more elements have the same address. So, finding some elements can be quite easy but for some, we might require some extra effort.

Finding elements by ID is the simplest of all. Every element has a ID attached to it which can be used to identify it and also interact with it. In android we use resource-id and in iOS we use name or label.

dr.findElement(By.id("android:id/text1")).click();

Accessibility ID

This is considered the best element locator if you are working for both iOS and Android applications. Accessibility ID is cross-platform and can be used or accessed in both android and iOS applications as well. This makes our code reusable.

dr.findElementByAccessibilityId("Accessibility").click();

Class name

Finding an element using the class name is not much reliable. Many elements can have the same class name and they cannot be identified just by using the class name attribute of the element. So, we can use a combination of multiple attributes such as combining text with the class name together and find the element.

List<WebElement> buttons = driver.findElementsByClassName("android.widget.TextView");

for(WebElement button : buttons){
    System.out.println(button.getText());

    if(button.getText().equals("Animation")){
        button.click();
    }
}

xpath

This locator strategy works on XML structure of the application. It identifies the elements with the help of other elements locations on the page. It is one of the most unreliable element locator because if the position of any element changes on the web page the xpath of the element also changes.

dr.findElementByXPath("//android.widget.TextView[@content-desc=\"Animation\"]").click();

Launching Appium Inspector

Lets first see how we can launch the Appium inspector. For using the Appium inspector we need to install Appium.

  • Launch Appium and start the server.
  • Define the desired capabilities and start a new Appium session.
  • Appium inspector interface will now open and we can see the application opened in our window.

This was a small introduction to finding elements using the Appium. In the next blog, we will see different techniques of finding elements in Appium.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK