This page explains how users can add locators that specify the test elements, and how to edit the added locators. This page serves as guide for advanced users familiar with the mechanisms of Appium and Selenium locators.
Table of contents
1. Add a locator
First, click a UI element on the captured UI.
*While in element selection mode, either click the pencil icon that appears upon hovering, or press Shift and click the element.
Then, on the pop-up that appears
If you want to duplicate and add an existing locator, select the locator from the dropdown and click the copy button.
Then, you can edit the pre-populated keys likexpath
and values like(//XCUIElementTypeTextField)[1]
. After editing, click the OK button to add the locator.
If you want to add a locator from scratch, click +Add.
Then you can edit in an empty state. Fill in the key and value, then click OK to add the locator.
2. Edit a locator
To edit a locator you have added, click the pencil icon.
Then, the locator becomes editable. Edit it, then click OK to complete the editing.
3. Locators that use variables
When dealing with a large number of similar UI elements:
- Capturing many elements can be challenging.
- Auto-generated UI elements that are difficult to use may require time-consuming repetitive modifications.
These issues can be resolved by using variables.
3-1. Create variable-based locators
We will explain using the Android API Demos app as an example. As the name suggests, this app allows you to list various features of Android, and although many menus are hierarchical, most of its locators have the same structure.
First, decide which UI elements to utilize as variable-based locators. Elements like menus or links that are matched by character strings are suitable. Here, we'll select "Accessibility" located at the top. Looking at the locator, we can see that elements are identified based on the character string "Accessibility."
Add a locator and create a new one with variables. Select the base locator from the dropdown menu, then click the copy button next to it.
You can edit the pre-populated keys likexpath
and values like//android.widget.TextView[@content-desc='Accessibility']
.
Replace "Accessibility" with "${1}". By assigning a variable name like the number 1, it will be treated as a local variable within MagicPod, which behaves differently from standard variables. Once input is completed, click OK to save.
The locator you just created will be automatically selected.
For better clarity, you can rename the element.
This completes the creation of the locator. Note that when you insert a variable in this way, it will no longer directly reference the original "Accessibility" element.
3-2. Use variable-based locators
Now, let’s try using this locator in a test case. When you drag and drop the "Menu" element we just created into the steps, an input field for values will appear. The value entered here will be assigned to the earlier "${1}" part.
Enter the actual character string of the menu, as shown in the figure.
The "Custom View" menu will be displayed on the screen after tapping the "Accessibility" menu.
If you run the test, you can see that "Accessibility" and "Custom View" are tapped in the intended order.
The effectiveness of using variable-based locators compared to the standard approach of capturing every screen may vary depending on the application's structure and test scenario. Choose the method that best fits your objectives.
If the locator’s string changes with each execution
It is also possible to put a variable in the step's value input field. This is useful when you want to use a variable as the locator's string, such as one generated by the Store the current second based unique value command.
It is also possible to put variables directly into the locator.
3. MagicPod’s unique locator keys
Locator keys for browser testing supported by Selenium, and for mobile app testing supported by Appium, are available. In addition to these, MagicPod offers several unique locator keys:
- ai: MagicPod calculates the English names of elements then uses these names to identify the elements. The calculation of English names utilizes not only Selenium/Appium's element tree information but also utilizes results of OCR (Optical Character Recognition) and Deep Learning icon image recognition. However, OCR and image recognition can sometimes fail depending on the device and are generally less stable than other locators. Therefore, it is recommended to use this method only when no other alternatives are available or when testing specific devices.
- (Searching from the UI tree): For iOS native app elements, in addition to standard Appium locators, you can specify locators with a note (searching from the UI tree). When such a locator is specified, it retrieves the entire UI tree of the screen and uses that tree to identify the position of elements for screen operations. Occasionally, due to issues within the iOS framework, some elements are not detected, and using the locator (searching from the UI tree) can help solve such issues. However, compared to standard Appium locators, this tends to be slower and less stable, so it is advised to use this only when recommended by MagicPod support.
- shadow: This enables testing against elements within shadow DOM. For more information, refer to Test against elements in the Shadow DOM using the shadow locator.
This problem can occur in the case of tables that have a large number of cells.