On this page, we explain the way the user adds the “locator” that specifies the tested elements. This page is a guide for an advanced user who has a certain degree of understanding of the mechanisms of the Appium and Selenium locator.
1. Adding a Locator
In case you want to add locators to certain UI elements, click the element on the captured UI, and select the “+Add” button on the pop-up window that is displayed.
Following that, on this screen, specify the appropriate locator key and value, and press the "OK" button to add the locator.
2. Locators that use variables
If there are a large number of similar UI elements,
- It is difficult to take a large number of captures
- If auto-generated UI elements are difficult to use, the same modifications need to be made to each one.
This is a laborious process. It can be resolved 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.
Figure 1 Menu item locator
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.
Figure 2 Additional input fields for the locator
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.
Figure 3 Saving locators
The locator you just created is automatically selected.
Figure 4 The locator is automatically changed.
You can change element names to make them easier to understand.
Figure 5 Changing the element name
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.
Figure 6 UI elements including input columns
Enter the character string in the actual menu, as shown in the figure.
Figure 7 Enter the variable values
The "Custom View" menu will be displayed on the screen after tapping the "Accessibility" menu.
Figure 8 The "Custom View" 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: Calculated by MagicPod. Use the Japanese name of the element to search for the element. When calculating the Japanese name, in addition to the Selenium/Appium element tree information, you can also use OCR (optical character recognition) for the page image or the Deep Learning icon image recognition results. However, OCR and character recognition may fail, depending on the device, and are extremely unstable compared to other locators. Therefore, they should not be used unless there are no other available methods or for testing that can only be performed on a specific device.
- (Searching from the UI tree): In case of iOS native app elements, in addition to the normal Appium locator (search from the UI tree), you can specify locators with a note. If you specify this locator, you can acquire the UI tree for the whole image, and specify the position of the element based on this tree when manipulating the image. In iOS native apps, there are rare cases in which elements that are expected to be found are not found as a result of a bug in the iOS framework. In such cases, the locator (search from UI tree) can be used to avoid the bug. This can be slower and more unstable than the regular Appium locator, so it is best not to use this unless recommended by MagicPod support.
- shadow: You can test against elements in the shadow DOM. If you want to know detailed usage, please read Use the shadow locator to test against elements in the shadow DOM *in preparation.
This problem can occur in the case of tables where there are a large number of cells.