The 'Android Data Matcher' Locator Strategy
One of the well-known limitation of finding elements on Android is that the Android UI renderer doesn & # x27; t actually build and render elements until they & # x27; re about to seem on the blind. What this means is that, if we are looking for an element which is far downward in a listing, and not yet visible on the blind, the element just does not & quot; exist & quot;. You can prove this to yourself by generating a source snapshot of your app while there are lots of list elements present below the edge of the screen. They won & # x27; t be thither! Let & # x27; s take as an illustration the ApiDemos app; let & # x27; s say we & # x27; re on the screen below, and want to tap on the & quot; Layouts & quot; item, which is not on the first screen & # x27; s worth of detail (you can see the first screen on the left, and how we had to scroll to get to & quot; Layouts & quot; on the right): How do we handle a situation like this with Appium? Well, what I usually say is to & quot; think like a user & quot; -- how would a user find this item? By sneak to scroll the tilt, of course, and using their eyes to sporadically assure for the appropriate content. We could, thus, implement a scroll-and-find helper method with built-in retry loops. If we & # x27; re using the Espresso driver, nonetheless, there & # x27; s a better way: the & # x27; Android Data Matcher & # x27; strategy! Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script. This strategy take advantage of the fact that Espresso runs in the app context, not on top of the Accessibility stratum, so it has access to the data which backs a leaning, not just the elements which are displayed on the screen. (In Android,ListViewsare a subtype of what & # x27; s called anAdapterView,which associate the listdatasource). We can use a part of the Espresso API to essentially target thisdata, and have Espresso automatically scroll to the element which represents that data for us. As you can see in Appium & # x27; sData Matcher Guide, if we were expend Espresso directly (not through Appium), we might accomplish this feat with some code that looks like the pursuit: This Espresso codification waits for some & quot; information & quot; consort to the parameters we & # x27; ve given theonData dictation, namely that we want an debut which has a specific title, within the list that has a certain Android ID. The important bit here is thehasEntryfunction, which builds aHamcrest Matcher. The matcher is employ by Espresso to assist observe the desired data and turn it into a scene we can interact with. There are all kinds of matchers, which you can explore and use with Espresso, and now with Appium. So how do we do this with Appium? Basically, we postulate to use the -androiddatamatcherlocator strategy, which in Java is accessed asMobileBy.androidDataMatcher. And for the selector, rather than a mere string, we construct a JSON object containing the Matcher information,stringify it, and pass it as the selector. It & # x27; s a bit sneaky (or, how I like to think of it, a clever way of act within the W3C WebDriver protocol definitions for locator strategy chooser). Here & # x27; s how we & # x27; d construct the attempt to encounter the & quot; Layouts & quot; detail we saw in the screenshot above: As you can see, we have to do a bit of employment to codify the Hamcrest Matcher we want to use, by background itsname and args. What should the values hither be? Basically, whatever they would be in the raw Espresso case (the gens of the matchmaker ishasEntry, and its arguments aretitle and Layouts, because we & # x27; re trying to find an entry whose title is & # x27; Layouts & # x27;). That & # x27; s essentially it! When you see this work, it & # x27; s pretty awesome -- the screen will jump real quickly to the expected item, without having to do a caboodle of manual scrolling. Below is a total representative, with two tests; one essay to find the & # x27; Layouts & # x27; item using XPath alone, which neglect (because the element is not on the blind), and a 2d test which succeeds because it uses this locater scheme. This strategy is an especially good surrogate for XPath when you only experience fond information about the element you require to find. (Of class, you can invariably see thefull code samplingon GitHub!) Lead, Content Marketing, HeadSpin Inc. Piali is a dynamic and results-driven Content Marketing Specialist with 8+ years of experience in crafting engaging tale and market collateral across diverse industry. She excels in collaborating with cross-functional teams to develop innovative content strategies and deliver compelling, authentic, and impactful content that resonates with target audiences and enhances brand legitimacy. 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)



The & # x27; Android Data Matcher & # x27; Locator Strategy
AI-Powered Key Takeaways
Check out:

Accelerate Appium test round with the HeadSpin, a solution for mobile app automation.!
onData (hasEntry (`` title '', `` textClock '') .inAdapterView (withId (`` android: id/list)) .perform (click ());Also check:
// first, find the AdapterView (not necessary if there 's only one) WebElement list = wait.until (ExpectedConditions.presenceOfElementLocated (MobileBy.className (`` android.widget.ListView ''))); // Construct the Hamcrest Matcher spec String chooser = new Json () .toJson (ImmutableMap.of (`` name '', `` hasEntry '', `` args '', ImmutableList.of (`` title '', `` Layouts ''))); // find and tick on the element habituate the androidDataMatcher selector list.findElement (MobileBy.androidDataMatcher (selector)) .click ();Read:
public class Edition095_Android_Datamatcher {private AndroidDriver driver; individual String APP = `` https: //github.com/appium/android-apidemos/releases/download/v3.1.0/ApiDemos-debug.apk ''; WebDriverWait wait; @ Before public nihility frame-up () throws Exception {DesiredCapabilities capabilities = new DesiredCapabilities (); capabilities.setCapability (`` platformName '', `` Android ''); capabilities.setCapability (`` deviceName '', `` Android Emulator ''); capabilities.setCapability (`` automationName '', `` Espresso ''); capabilities.setCapability (`` app '', APP); driver = new AndroidDriver (new URL (`` http: //localhost:4723/wd/hub ''), capability); wait = new WebDriverWait (driver, 10);} @ After public void tearDown () {if (driver! = null) {driver.quit ();}} @ Test public void testFindHiddenListItemNormally () {wait.until (ExpectedConditions.presenceOfElementLocated (MobileBy.AccessibilityId (`` Views ''))) .click (); wait.until (ExpectedConditions.presenceOfElementLocated (MobileBy.xpath (`` // * [@ content-desc='Layouts '] ''))) .click (); wait.until (ExpectedConditions.presenceOfElementLocated (MobileBy.AccessibilityId (`` Baseline '')));} @ Test public void testFindHiddenListItemWithDatamatcher () {wait.until (ExpectedConditions.presenceOfElementLocated (MobileBy.AccessibilityId (`` Views ''))) .click (); WebElement list = wait.until (ExpectedConditions.presenceOfElementLocated (MobileBy.className (`` android.widget.ListView ''))); String chooser = new Json () .toJson (ImmutableMap.of (`` name '', `` hasEntry '', `` args '', ImmutableList.of (`` rubric '', `` Layouts ''))); list.findElement (MobileBy.androidDataMatcher (picker)) .click (); wait.until (ExpectedConditions.presenceOfElementLocated (MobileBy.AccessibilityId (`` Baseline '')));}}Piali Mazumdar
The & # x27; Android Data Matcher & # x27; Locator Strategy
4 Parts
-1280X720-Final-2.jpg)
Regression Intelligence pragmatic guidebook for advanced users (Part 3)
-1280X720-Final-2.jpg)
Regression Intelligence practical guide for advanced users (Part 4)
Discover how HeadSpin can endue your business with superior testing capabilities







Discover how HeadSpin can empower your concern with superior testing capabilities
Discover how HeadSpin can empower your concern with superior testing capabilities
Connet Now


Automate This With SUSA
Test Your App Autonomously







.png)












