How to Pick the Right Locator Strategy
Reader Venkata recently asked us to compare and contrast the different selector strategies available. If you have the sumptuosity to choose, which is the good cream? We & # x27; ve write about element selector quite a bit already. For those new to the subject, we have a good overview of what selector strategy are and how to use them. I & # x27; m going to omit mention of Web and Hybrid tests here and focus just on the chooser strategies provided by Appium for native iOS and using the UiAutomator2 and XCUITest drivers. Without further ado, hither & # x27; s our prioritized list of locator strategies: Now don & # x27; t go running to your fellow tester utilize this list to decide an argument. Every app is different, and assembling this list is like making a list of the & quot; better & quot; pieces in cheat: they all have their uses. That this is the top pick should surprise nobody. If you feature the selection of using accessibility IDs, use them. Normally an app developer take to add these specifically to UI factor in the codification. The major benefit of approachability IDs over just theid locator strategy is that while app developers add these IDs for testing, users with deterrent or accessibility issues benefit. People who use screen subscriber or former device, and algorithm which inspect UIs, can better navigate your app! On Android, this locator scheme uses the accessibilitycontentDescriptionproperty. On iOS, this locator strategy uses the accessibility identifier. Here & # x27; s something surprising: in the XCUITest driver, theaccessibility id, id, and namelocator strategies are all indistinguishable. They are implemented the like way. Go ahead and try swop your locator strategy, you will get the same termination. This may change in the future, but for now you can find an element using the gens or text in it because iOS has many ways in which it sets a default availableness identifier if one is not provided by the developer. For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users. Element IDs need to be added by a developer, but they allow us to pinpoint the exact element in the app we care about, even if the UI changes appearing. The drawback is you need to be able to utter to your developers. Many testing teams do not have this luxury. This locator strategy is pretty similar tohandiness idexcept that you don & # x27; t get the added welfare of availability. As noted above, on iOS, this is actually identical toaccessibility id. On Android, the idlocator strategy is implemented using Resource IDs. These are usually bring to UI element manually by app developers, though are not involve, so many app developers will omit them if they don & # x27; t think they & # x27; re important. Now this is contentious. XPath is the most expressive and commonly accepted locator strategy, but Appium developers have long discourage of its drawbacks. This early edition goes to some deepness in explaining why XPath should be avoided. Despite Appium developers monish against XPath & # x27; s low performance for years, it still seems to be the most popularly habituate locater scheme. This is probably because there are many selections that can & # x27; t easy be create any former way. For example, there & # x27; s no way to take the parent of an element using the simpleidselectors. The benefit of being able to express more complicated queries must outweigh the price to performance for all but the testers whose apps have such tumid XML component hierarchies that XPath is completely unusable. Those who need performanceandflexibility can use the aboriginal locator strategies in the next section (and I & # x27; ll explain why I put them at # 4 instead of # 3). XPath selectors can be very brittle, but they can be responsibly wielded to great effect if you follow the guidepost of. Being intentional and carefully picking picker rather than taking whatever an inspector provide can mitigate the brittleness. I think part of the popularity of XPath staunch from its use with Selenium and web growing, as good as it being the nonremittal of many tutorials and inspection tool. When working on Appium I perpetually expected our XPath handling to break more often, but I recollect few bugs, credibly the benefit open-source XPath libraries built for more generalised use. The Android OS provides a usefuldumpWindowHierarchywhich afford us an XML document of all the elements on the blind. From there we apply the XPath query and find component. iOS execute not supply a method of getting the total hierarchy. Appium & # x27; s implementation starts at the root covering factor and recurses through each element & # x27; s nestling and populates and XML document which we can then employ the XPath question to. I still think XPath is unintuitive, especially for those new to programming, but at least it & # x27; s a well-accepted manufacture standard. I phone these the & quot; native & quot; locator strategies because they are provided by Appium as a means of creating selectors in the native endorse by the device. These locater scheme experience many fans, who love the fine-grained face and great performance (as or only slightly less performant thanaccessibility id or id). These locator strategies are crucial for those who receive UIs which escape the grasp of the other locator strategies or experience an element tree which is too bombastic to let the use of XPath. In my view, they have several drawback. These native locator scheme demand a more elaborated understanding of the underlying automation fabric. Uiautomator and XCUITest can be hard to use, especially for those less conversant with Android and iOS particular. These locater strategies are not cross platform and knowing the ins-and-outs of both iOS and Android is challenging. In addition, the selectors passed to these native locater strategy are not instantly evaluated by the mobile OS. Java, Kotlin, Objective C and Swift all miss aneval function which would allow interpreting a string of text as code. When you post anandroid uiautomatorselector to Appium, the text pass through a very simplistic parser and utilizationReflectionto rebuild the objects cite in the text. Because of this, small mistake in syntax can throw off the entire selector and only the most mutual methods are supported. This system is unreliable and often encounters difficult glitch. If Android or iOS change the testing classes in new updates, your selectors might need to be updated. Using XPath, Appium will proceed track of the OS updates and your picker should keep workings. A personal quibble I have with these locator scheme is that you are essentially writing a different programing lyric (such as Java) inside of a string in your test code. Your text editor will not volunteer syntax highlighting or semantic analysis inside of these queries which makes them harder to maintain. On the former manus, sometimes there & # x27; s just no other way, and for those who are skilful in these methods XPath can seem clumsy in comparison. This locator strategy is pretty nifty. Supported on all platform and drivers, you can legislate an image file to Appium, and it will try to site the twin elements on the blind. We already proceed over this in. This supports fuzzy matching, and surely has its coating, but you should only use it when the early locator strategies aren & # x27; t working. It may not constantly behave deterministically, which mean it can be a source of test flakiness. Also, unless you have a special code editor, they may be a slight more difficult to act with and conserve. Of course, visual UI changes will always break-imageselectors, whereas the other picker only separate when the factor hierarchy changes. That & # x27; s it! Feel free to drop us a line if you feel passionately about one of these strategies! Lead, Content Marketing, HeadSpin Inc. Piali is a dynamic and results-driven Content Marketing Specialist with 8+ years of experience in crafting engage narratives and market collateral across diverse industries. She excels in collaborating with cross-functional teams to develop innovational content strategies and deliver compelling, authentic, and impactful content that resonates with target audiences and enhances make authenticity. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts..png)

.jpg)
.jpg)
How to Pick the Right Locator Strategy
AI-Powered Key Takeaways
Check out:
1. handiness id
Also check:
Read:
2. id
Also read:
3. xpath
Check out:
4. -android uiautomator, -ios predicate string or -ios category concatenation
Read:
Check:
5. -image
Piali Mazumdar
How to Pick the Right Locator Strategy
4 Parts
-1280X720-Final-2.jpg)
Regression Intelligence practical guidebook for advanced users (Part 3)
-1280X720-Final-2.jpg)
Regression Intelligence practical guidebook for modern users (Part 4)
Discover how HeadSpin can invest your business with superior testing capabilities







Discover how HeadSpin can invest your business with superior quiz capabilities
Discover how HeadSpin can empower your business with superior testing capabilities
Connet Now


Automate This With SUSA
Test Your App Autonomously







.png)












