🔍 Locator Strategies & Page Object Model
Locator Strategies & Page Object Model: Think of mobile element locators as a city addressing system: `resource-id` works like a door number — direct and unique, like "42 Main St
Think of mobile element locators as a city addressing system: `resource-id` works like a door number — direct and unique, like "42 Main Street, Apt 3." `accessibility id` is like a building name — "Central Post Office" is universally known and usually survives platform changes. `xpath` is like giving directions by landmarks — "leave the market, turn left, second block, building next to the coffee shop" — you always arrive eventually, but the route is long, fragile, and slow. But if you're used to `By.id()` or `By.cssSelector()` in Selenium, why must you learn `MobileBy.AccessibilityId()` at all? Because native mobile UI has no HTML IDs or CSS classes — instead, elements are identified through accessibility tree nodes, content-desc fields, and UI hierarchy; same conceptual framework as Selenium's locator system, completely different technical reality. In Java Selenium, `By.id("loginButton")` targets the `id` attribute in the DOM; in Appium, `MobileBy.AccessibilityId("loginButton")` targets the `content-desc` in UIAutomator2's visibility tree. The critical QA truth: tests that rely on xpath break with the smallest UI hierarchy change — one button moved inside a `LinearLayout` — and flaky test alarms drown your CI pipeline in noise while the real bugs go undetected.
Locator Strategies Comparison
Java — All Locator Strategies
Java — AppiumBy Locator Examples
Micro Lab: Code practice
Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.
Why Are All 7 Locator Strategies Shown Together in the Same Class?
AppiumBy.id(...) is the FASTEST and…
`AppiumBy.id(...)` is the FASTEST and MOST RELIABLE method — it DIRECTLY targets Android's `resource-id`, just like Selenium's `By.id()` but WITH a package-name prefix.
AppiumBy.accessibilityId(...) is NOT just…
`AppiumBy.accessibilityId(...)` is NOT just a locator — because it targets the SAME `content-desc` field that accessibility tools like TalkBack/VoiceOver USE, using this locator indirectly performs accessibility TESTING too.
The `UiSelector().scrollable(true)).scrollIntoView(...)` chain written via `androidUIAutomator` is a capability Selenium does NOT have — it AUTOMATICALLY scrolls to REACH an element not currently visible on the mobile screen.
AppiumBy.xpath(...) is MARKED "last resort…
`AppiumBy.xpath(...)` is MARKED "last resort, slow!" in the comment — being placed LAST among the 7 strategies is not a coincidence, it also SHOWS the priority ORDER.