Skip to content

Build Test Scripts for iOS App using Appium Inspector

In this article, we will be going through steps to construct locators for elements in iOS apps which will be used in automation.

In this test case, you need to verify the calculated premium and penalty. You can achieve this with the following steps:

  1. Launch the iOS Mobile App in Appium Inspector

  2. Login to the Worksoft SaaS Domain

  3. Click on the Hamburger icon and click on the Test Scripts link

  4. From the Android test scripting article, we created a test script called ‘Enter Inputs and Verify Calculated Premium and Penalty’. Let us reuse this script for iOS. Click on the three ellipses and click on the ‘Edit’ option.

  5. Switch to the Appium Inspector where the app is launched.


  6. Select the Health Insurance Cost Calculator header text using the 'Select Elements' tool.


  7. Under Selected Element properties, we cannot see id or accessibility id listed. In their absence and keeping in line with our locator strategy practices, we will use the xpath attribute as a locator. Go ahead and click on it to copy it.


  8. After copying the required locator, we can paste this in our test script. Paste this locator in the first assertText instruction in the script.


  9. Note that we have appended “xpath=” before the locator that we copied. This is so that SaaS understands what kind of a locator the provided string/word is. You can save this instruction now. This instruction has both locators for Android and iOS versions of the app, and can be used to execute tests on both the devices.

  10. To summarize what we have done so far, we selected the HICC header text from Appium, copied its xpath, due to the absence of id and accessibility-id, pasted it in the test script and provided an appropriate instruction. Let us complete the other steps.

  11. Use the Select Elements tool and select the Age input field.


  12. We observe that id or accessibility-id are not listed for this input field, and only xpath is available. But unlike the XPath for HICC header text, this one is very long and hard to read. In cases like these, we must construct XPaths by ourselves to keep them short and readable.

  13. Let us look at the tagname for this element - XCUIElementTypeTextField. We will use this element name directly as the XPath to refer to the element, since it is representative of the input field and all data will pass through it. You can copy this tagname either from the very last string of the XPath or from the type attribute.


  14. Once you have copied the tagname/XPath, it is always a good idea to validate it before using it in automation. To do this, click on the magnifying glass icon  on the top most toolbar of Appium. This is the Search for Element feature. Once the dialog box loads, you would need to select the locator strategy, which in this case is XPath and enter the string to search.


  15. Once you have entered the locator, you can click on search to check for validity. (Note that we have added ‘//’ before our XPath).


  16. Interestingly, we have three results, instead of the expected one. This is because of the other two input fields - Income and Zip Code - also sharing the same tagname - XCUIElementTypeTextField.

  17. We can fix this by encapsulating the XPath in parentheses. This tells Appium and also SaaS to treat the XPath as a group instead of individual instances, and then we can provide a number to select the one we want. The new XPath would be this:

    (//XCUIElementTypeTextField)[1]

    …where [1] signifies selecting the first result from the group, which is Age input, and we can change it to 2 and 3 to later select the Income and Zip Code inputs.



  18. Once you have all these XPaths ready, you can paste these in the target fields for the existing type commands respectively.



  19. We have added ‘xpath=’ at the start to let SaaS know what kind of a locator it is. Note that instructions 2-4 also contain locators for Android, and the iOS locators are only displayed because they were added recently.

  20. We now need to verify the premium for the provided values. In Appium, enter data into the input fields and select the Calculate button to copy its locator.


  21. Paste this locator in the existing tap instruction. Also tap on the Calculate button in Appium to navigate to the results page.

  22. Select the results header text, annual premium cost and penalty values and copy their locators.



  23. Here, while accessibility-id is listed for all these elements, they are actually their own values. In case of differing inputs previously, the results will also differ, which will then affect your locators since they will not be found in the app. One solution is to parameterize the locators themselves. For example, if you have the value of results header stored in a variable called resultsHeader, provide the locator as:

    id=${resultsHeader}


    …so no matter the inputs, these locators will always work because they change with the input data.

    Another way of writing a locator is using relative XPaths. We would need to build an XPath that only references the actual element of a result but we use another unique element as an anchor to the former element. In our case, we can use the “Results” element to refer to the results header text. Let us take a look at the app source more closely:


    XCUIElementTypeStaticText tags which have the Results heading and You are eligible for a subsidy value are on the same level, making them, in technical terms, siblings. So, to refer to the latter using the former, we would write a locator like this:
    //XCUIElementTypeStaticText[@name=”Results”]//following-sibling::XCUIElementTypeStaticText[1]

    You would read this locator as “find the element immediately next to the Results heading”. Such locators are called relative XPaths, since they establish relationships between different locators.
    Relative XPaths for annual premium cost and penalty would look this:
    //XCUIElementTypeStaticText[@name="Estimated Income (U.S $)"]//following-sibling:://XCUIElementTypeStaticText[1]

    //XCUIElementTypeStaticText[@name="Estimated Annual Premium Cost"]//following-sibling:://XCUIElementTypeStaticText[1]

    Paste these in the last three instructions in the script.


Feedback and Knowledge Base