On this page, we explain how users can add locators to specify tested elements. This guide is intended for advanced users familiar with the mechanisms of Appium and Selenium locators.
1. Adding a Locator
If you wish to add locators to specific UI elements, click the element in the captured UI, and select the +Add button on the displayed pop-up window.
Then, on this screen, specify the appropriate locator key and value, and click OK to add the locator.
2. Locators that use variables
When dealing with a large number of similar UI elements:
- Capturing many elements can be challenging.
- Modifying auto-generated UI elements may require repetitive changes.
To simplify this process, consider using variables.
2-1. Creating locators with variables
We will use the Android ApiDemos application as an illustrative example. This app, as the name suggests, enables you to view the various Android functions, and most locators have the same structure, despite the fact that many of the menus are hierarchical.
First, determine the UI elements to be used as locators with variables. These must be suited for use with menus, links, and other types of string matching. Here, you should choose “Accessibility” at the very top. When we look at Xpath, we can see that the character string “Accessibility” is used as a key to specify the element.
Add the locator to create a locator with variables. If you click the +Add button next to the locator,
the input field for adding the locator will be displayed.
Enter these while viewing the existing locator. The type should be transcribed as being the same as the existing "xpath," while the values should be transcribed as they are, with the exception of "Accessibility". Replace the area marked “Accessibility” with “${1}”. If you use variable names that start with the number 1 like this, they will be treated as locator variables within MagicPod, and will behave differently from normal variables. Once you have finished, click OK to save.
The locator you just created is automatically selected.
You can change element names to make them easier to understand.
This completes the creation of the locator. Note that if you include this kind of variable, it cannot point to the original element, known as “Accessibility,” as is.
2-2.Using locators with variables
We can try using this locator in a test case. If you drag and drop the element you just created called “Menu” onto the test case, it will be displayed in the value input field. The value input here shall replace the “${1}” section we just mentioned.
Enter the character string in the actual 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 will see that “Accessibility” and “Custom View” are tapped in the intended order.
The benefit of this method of using locators with variables or the method of capturing the whole screen as normal may differ depending on the application structure and test scenario. Use them according to the purpose.
If the string of locator changes with each execution
It is also possible to put a variable in the step's value input field. This is useful when using a locator string such as a variable 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 can be used for browser testing as supported by Selenium and for mobile app testing as supported by Appium. In addition to this, there are several locator keys unique to MagicPod.
- ai: Calculation is executed by MagicPod. To use this key, employ the English name of the element for searching. This involves using OCR (optical character recognition) on the page image or Deep Learning icon image recognition, in addition to Selenium/Appium element tree information. However, using OCR and character recognition may lead to issues, depending on the device, and are notably less stable compared to other locators. Consequently, it is advisable to refrain from using this method unless no other alternatives are available or for testing specific to a particular device.
- (Searching from the UI tree): This functionality is applicable to iOS native app elements, in addition to the standard Appium locator (searching from the UI tree). To use this key, specify locators with a note to acquire the UI tree for the entire image. This allows specifying the element's position based on the tree when manipulating the image. This approach is beneficial in rare cases in iOS native apps where expected elements may not be found due to a bug in the iOS framework. However, the locator (searching from UI tree) can be slower and more unstable than the regular Appium locator. As a result, it is advisable to use this method only when recommended by MagicPod support.
- shadow: This functionality enables testing against elements in the shadow DOM. For more information, please read the article Test against elements in the Shadow DOM using the shadow locator.
This problem can occur in the case of tables where there are a large number of cells.