📜 ⬆️ ⬇️

Translation of the Appium Essentials book. Chapter 4

Hey. I came back and prepared for you a translation of the 4th chapter of the book on Appium. If you just turned on, here we have the previous 3 chapters:




Chapter 4. Search for items by different locators


Appium has several ways to localize items in a mobile application. In this chapter, some of the element search techniques for native and hybrid applications are using uiautomator and Appium inspector. To define elements in web applications, we’ll review add-ons for Chrome to remotely localize elements.

In this chapter:
')
  1. Search for items using the Chrome ADB plugin
  2. Search for items using Safari Develop
  3. Search for items using UIAutomator and Appium Inspector
  4. Search for items by id, Name, LinkText, Xpath, cssSelector, ClassName, AccessibilityId, AndroidUIAutomator and IosUIAutomation



Search for items using the Chrome ADB plugin


To search for items in a web application, you need to install an add-on. Chrome browser provides ADB (add-on) to the source code [markup] of the mobile application. You can download from here .

After installing the add-on, follow these steps to connect the device:

  1. Navigate [to smartphone] to Settings | About Phone and tap Build number 7 times (it is assumed that you have Android version 4.2 or higher). Then, go back to the previous screen and find the Developer options:

    (If this step did not give anything to your smartphone, google “How to enable developer options” and indicate your phone model.)


  2. Tap the Developer options, then ON in the developer settings (a dialog will pop up to confirm that the mode is activated; click OK); Make sure USB debugging is enabled:


  3. Now open the desktop version of Chrome (it is assumed that you have already installed the ADB plugin), click on the ADB plugin icon in the upper right corner of the browser, then on View Inspection Targets:
  4. Connect your Android device to your computer (make sure that the appropriate USB driver for your device is installed on your computer). After connecting, the device should prompt you to debug via USB; Click OK.

  5. Open Chrome on the device and go to the desired site (we will have this www.google.com ).

  6. Now at the address [on the desktop] chrome: // inspect / # devices all connected devices with open tabs will be displayed. Make sure Discover USB devices is selected:

  7. Click on inspect to open the developer tools; This window should open. Click on the screencast icon in the upper right corner to display the device screen:


Search for items in iOS web applications using the Safari Develop feature


Safari comes with a built-in solution for finding items, we just have to follow a few steps to prepare the device for remote debugging:

  1. Go to Settings | Safari | Advanced:

  2. Enable Web Inspector:

  3. Open Safari on your device or simulator and follow the link.
  4. Now, open Safari on Mac, in the menu click on Develop, select the device you need (simulator) (assuming the device is connected to Mac), and click on the URL:

  5. You should get a markup screen like this:



Search by ID


To interact with a web page, we first need to find the elements on it. All functions of the Appium client need some kind of tool for working with a web page.
Item search by ID is used to search for one specific item in the application.

findElement(By.id(String id)); 

We need to pass the ID of the item we want to work with. Now let's find out the ID of the element using the Chrome ADB plugin. Below is an example of how to work with the Google search page:

  1. In the Chrome mobile browser, go to www.google.com .
  2. In the ADB plugin, click the Inspect element button.
  3. Hover over the line to enter a Google request, as shown in the screenshot:



We can use a dedicated id to interact with the web element. An example of using the command:

 WebElement searchBox=driver.findElement(By.id("lst-ib")); 

If you want to enter text in the search box, use the variable. For example, like this:

 searchBox.sendKeys("Manoj Hans"); 

Now let's see the same example for working with Safari on an iOS device. You need to complete the following steps:

  1. In Safari, go to www.google.com .
  2. Click the URL under the iOS simulator in the Develop tab of the Safari browser for Mac.
  3. Click on the Inspect icon and then on the search bar on the iOS simulator screen, as shown here:



Search for items by name


Another way to find an item is to search by name; This is how the corresponding method will look like:

 findElement(By.name(String Name)); 

As with id, we need to pass the name of the element we are looking for. An instance of the WebElement class will return, with which we will continue to work. Again, let's go with the example of Google, the search line, in addition to the id, also has a name. The steps are the same as with ID:



This is what the command will look like:

 WebElement searchBox=driver.findElement(By.name("q")); 

Item search by linkText


This method is useful when you search for a link on a page. Signature method:

 findElement(By.linkText(String text)); 

It is necessary to transmit the text that should be in the desired link. Also, the WebElement instance will be returned.

We will try to find the link on the Google page, whose text is “Images”:

 WebElement imagesLink=driver.findElement(By.linkText("Images")); 

Xpath Search


Xpath works with both XML and HTML structures for searching for elements. It is slightly slower than the methods of working with ID and name, but this is a very good way to find an element on the page where the ID is generated automatically. We will not study Xpath in detail, if necessary, search for the tutorial on Google. Signature method:

 findElement(By.xpath(String XPath)); 

You must pass Xpath to the element we are looking for. Here we built Xpath, based on the attributes:

 WebElement searchBox=driver.findElement(By.xpath("//input[@id='lst-ib']")); 

Search by cssSelector


cssSelector works strictly with HTML, and it is faster than Xpath. Signature method:

 findElement(By.cssSelector(String cssSelector); 

An element selector must be passed to the method.

Construct the cssSelector Google search string:

 WebElement searchBox=driver.findElement(By.cssSelector("#lst-ib")); 



Search for elements for native and hybrid applications


There are many ways to find elements for native and hybrid applications, such as UIAutomatorviewer (for Android) and Appium Inspector. Let's start with the first.

Search items with UIAutomatorviewer


UIAutomatorviewer can be found in the Android SDK folder (C: \% android-sdk% \ tools); on Mac, you can find it in the tools folder:



When you open, you will see this screen:



Consider an example with a standard Android calculator:

  1. Open Android emulator or real device.
  2. Run the calculator.
  3. In the window UIAutomatorviewer, click on the icon of the screenshot of the device. If more than one device is running, UIAutomatorviewer will ask you to choose a device.



Now you need to find the elements of the application using different locators.

Search by ID


The method signature is the same as searching for an item by ID in a web application:

 findElement(By.id(String id)); 

The ID of the element we want to work with is passed to the method. Below is an example of identifying the “5” key in a calculator using the UI Automator Viewer:

  1. Open the UI Automator Viewer and click on the "5" key in the application.
  2. In the Node Details section, you will receive a resource-id of the form com.android.calculator2: id / digit5:


  3. We can resource-id as an ID to interact with the key:

     WebElement digit_5=driver.findElement(By.id("com.android.calculator2:id/digit5")); 

  4. To click a key, use the command:

     digit_5.click(); 

Search by name


The signature of the method is already familiar to us:

 findElement(By.name(String Name)); 

You need to pass the name of the element you want to work with. Below is an example of the DELETE button search:

  1. Click on the DELETE button in the UI Automator Viewer.
  2. In the Node Details section, you will see a text field with the value DELETE:


  3. To find and click the DELETE button, use the following code:

     delete.click(); WebElement delete=driver.findElement(By.name("DELETE")); 

Search className


Signature:

 findElement(By.className(String ClassName)); 

Below is an example of finding the input window by className:

  1. Click on EditBox in UI Automator Viewer.
  2. In the Node Detail section, find the class with the value android.widget.EditText:


  3. An example of finding an item by className:

     WebElement editBox=driver.findElement(By.className("android.widget.EditText")); 

  4. To get the value of the EditBox element, use the command:
     editBox.getText(); 


If this class is used for multiple items, we can refer to the item by index in the returned collection. For example, we want to refer to the key "7" by the class name:

 List<WebElement> editBox=driver.findElements(By.className("android.widget.Button")); editBox.get(1).click(); 

We used the findElements method instead of findElement ; so we get more than one element by the selector. In our example, the key “7” has an index value of 1. Therefore, we turned to the 1st element.

Search by AccessibilityId


Appium developers wanted to give us more options for localizing elements, so they created AccessibilityId. This locator identifies elements in the same way as ID and name. Signature method:

 findElement(By.AccessibilityId(String AccId)); 

Below is an example of interaction with the "+" button:

  1. Open the UI Automator Viewer and click on the "+" in the calculator.
  2. In the Node Details section, you will find the content-desc with the value “plus”:


  3. This content-desc can be used as an AccId:

     WebElement plusSign=driver. findElementByAccessibilityId("plus"); plusSign.click(); 


Search by AndroidUIAutomator


AndroidUIAutomator is a very powerful locator. It uses the Android library UIAutomator to search for items.

 findElement(By.AndroidUIAutomator(String UIAuto)); 

Below is an example of interaction with the "=" button:

  1. In the UI Automator Viewer, click on "=".
  2. In Node details, we can select any field. For example, resource-id with value com.android.calculator2: id / equal. We can use resource-id as UIAuto to work with the "=" key:

     WebElement equal=driver. findElementByAndroidUIAutomator("new UiSelector().resourceId(\"com.android.calculator2:id/equal\")"; 
  3. Another example is to select content-desc with equals value, now the command will look like this:

     WebElement equal=driver. findElementBy.AndroidUIAutomator("new UiSelector().description(\"equals\")"); 

    To learn more about this locator, visit this and this page.

Search for items with Appium Inspector


In the previous chapter, we already learned that Appium Inspector works well on a Mac. Working on a Mac, we can use it to search for items. To run Appium Inspector for Android, we need to complete the following steps:

  1. You need to determine the path to the application, package or activation name in Appium GUI when working with the emulator. If we work with a device, the package name or activation is enough.

    How to determine the name of the package or activation, if the application is running on the device? You can install on the device APK Info from the Play Store. If you have a test application on your computer, then the Appium server will automatically pull up all the names after specifying the path to the application.

  2. In the General Settings section, the Prelaunch Application option must be selected.

  3. If you are working with an emulator, then the Launch AVD option must be selected in the Android Settings section (assuming you have already created the emulator). If you work with a device, it must be connected and USB debugging must be enabled.

  4. Click the start button.

  5. Click the Inspector button. Appium Inspector starts.



We have already learned many ways to find items. Now we will use the search on Xpath

Xpath Search


Signature:

 findElement(By.xpath(String XPath)); 

Below is an example of working with the "9" key:

 WebElement digit_9=driver.findElement(By.xpath("//android.widget.LinearLayout[1]/ android.widget.FrameLayout[1]/ android.widget.LinearLayout[1]/ android.support.v4.view.viewPager[1]/ android.widget.LinearLayout[1]/ android.widget.LinearLayout[1]/ android.widget.Button[3]")); 



You can work with the digit_9 variable to interact with the element.
Now, how to work with Appium Inspector for iOS:

  1. In Appium GUI you need to specify the path to the application.
  2. The Prelaunch Application option must be enabled (in the General Settings section).
  3. If you are working with a simulator, the Force Device option must also be enabled in the iOS Settings section.
  4. Click the start button.
  5. Click on the Inspector button.

In the example we will work with TestApp, it can be downloaded from Appium GitHub .

Search by name


Signature method:

 findElement(By.name(String Name)); 

Below is an example of working with the second EditBox in the TestApp application:

  1. Click on the second EditBox in the Appium Inspector.
  2. In the Details tab, find the name field with the value IntegerB. We can use the value to detect the second EditBox:

     WebElement editBox=driver.findElement(By.name("IntegerB")); 
  3. To enter text in EditBox, use the command:

     editBox.sendKeys("12"); 



Search by IosUIAutomation


UIAutomation is a JavaScript library used to search for items in Apple's Automation Instruments. Appium developers gave us a similar way to find items in Appium using IosUIAutomation:

 findElements(By.IosUIAutomation(String IosUIAuto)); 

To start using IosUIAutomation, you need:

  1. It is necessary to transfer the value of IosUIAuto for the element with which we want to work. Below is an example of finding the first EditBox in the TestApp application:

     WebElement editBox=driver. findElements(By.IosUIAutomation(".elements()[0]")); // '0' -    

  2. To enter the text in the first EditBox, use the command:

     editBox.sendKeys("10"); 

  3. Another example is searching for an element by textFields:

     WebElement editBox=driver. findElements(By.IosUIAutomation(".textFields()[0]")); 

To learn more about the UIAutomation library, visit this page.



That's all. it's time to automate our application. In the next chapter, we will work with Appium driver.

Source: https://habr.com/ru/post/332706/


All Articles