सामग्री पर जाएँ

लेसन 23 · Android and iOS Specifics

Device Features: Permissions, Location, Biometrics and Deep Links

Automate what makes mobile different: runtime permissions, system alerts, GPS location, orientation, app lifecycle, biometrics, notifications and deep links.

A mobile app lives inside a device that has sensors, system dialogs, a lock screen and an operating system that can interrupt it at any moment. Real users grant permissions, rotate the phone, switch to another app and come back, tap a link from an email that opens the app on a specific screen. Tests that never exercise these situations miss the bugs users hit.

This lesson collects the Appium commands for these device-level behaviors, with notes about what differs between Android and iOS. The real commands need a device or emulator, so they are shown read-only. The runnable examples are stand-ins: a deep-link parser and a lifecycle state machine.

Permissions and system alerts

When an app first asks for the camera, location or notifications, the operating system shows a dialog that is not part of your app. Two strategies exist.

Avoid the dialog with capabilities:

Python
options.auto_grant_permissions = True            # Android: grant all manifest permissions
options.set_capability("appium:autoAcceptAlerts", True)   # iOS: tap Allow automatically
# or autoDismissAlerts to tap Don't Allow

Handle the dialog when you want to test both choices. On iOS use alert commands; on Android click the system button:

Python
# iOS
driver.execute_script("mobile: alert", {"action": "accept", "buttonLabel": "Allow While Using App"})
# Android 13+
driver.find_element(AppiumBy.ID,
    "com.android.permissioncontroller:id/permission_allow_foreground_only_button").click()

Android also lets you grant or revoke permissions directly: driver.execute_script("mobile: changePermissions", {"permissions": ["android.permission.CAMERA"], "action": "grant"}). Test the denied path at least once; apps that crash when permission is refused are common.

Location

You can fake the GPS position, which is perfect for store locators or delivery apps:

Python
driver.set_location(52.5200, 13.4050, 34)     # latitude, longitude, altitude

Emulators and simulators accept this directly. On real devices it needs a mock-location app (Android) or Xcode location simulation, and cloud farms usually expose their own capability for it. Always check what the cloud vendor supports.

Orientation, keyboard and clipboard

Python
driver.orientation = "LANDSCAPE"
assert driver.orientation == "LANDSCAPE"
driver.orientation = "PORTRAIT"

driver.hide_keyboard()                       # Android: works; iOS: may need a key to tap
driver.set_clipboard_text("hello")
text = driver.get_clipboard_text()

Rotation is a classic source of lost state: a form that clears itself when the phone turns is a bug your test can find in two lines.

App lifecycle

Mobile operating systems suspend and kill apps. Appium can reproduce this:

Python
driver.background_app(5)                     # send to background for 5 seconds, then return
driver.terminate_app("com.example.shop")     # kill it
driver.activate_app("com.example.shop")      # bring it to the foreground (starts if needed)
state = driver.query_app_state("com.example.shop")

query_app_state returns a number: 0 not installed, 1 not running, 2 running in background (suspended), 3 running in background, 4 running in foreground. A good test sends the app to the background mid-checkout and verifies the cart survives.

Stand-in: the app lifecycle

Python App lifecycle state machine (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line shows why tests must know the difference between resuming and cold starting: the cart is gone after a kill unless the app saves it.

A deep link opens the app at a specific screen, for example myshop://product/42 or https://shop.example.com/p/42 (a universal or app link). Testing them saves many taps, so deep links are also the fastest way to put the app in a precise state at the start of a test.

Python
driver.execute_script("mobile: deepLink", {
    "url": "myshop://product/42",
    "package": "com.example.shop",      # Android
})
# iOS: driver.get("myshop://product/42") or mobile: deepLink with bundleId
Python Deep link router (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Biometrics and notifications

Simulators and emulators can fake a fingerprint or face match, so a "log in with biometrics" flow is testable:

Python
# iOS simulator
driver.execute_script("mobile: enrollBiometric", {"isEnabled": True})
driver.execute_script("mobile: sendBiometricMatch", {"type": "faceId", "match": True})
# Android emulator
driver.execute_script("mobile: fingerprint", {"fingerprintId": 1})

Run the failing case too ("match": False). Real devices cannot be driven this way; cloud vendors offer their own biometric injection.

Notifications are harder. On Android, driver.open_notifications() pulls down the shade and you can read or tap entries. iOS has no supported API to read the notification center, so teams trigger the push from a test backend and assert on what the app does after launch. Do not count on automating system settings screens; they change with every OS version.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does appium:autoGrantPermissions do on Android?

    It grants all manifest permissions at install, which removes permission dialogs from the flow.
  2. 2. Why include a test where permission is denied?

    Users do refuse permissions, and that path is often untested.
  3. 3. What is a deep link used for in tests?

    Deep links reach a screen or state without tapping through the UI.
  4. 4. What does the app state value 4 from query_app_state mean?

    0 not installed, 1 not running, 2 suspended in background, 3 background, 4 foreground.

Summary

  • Choose between avoiding permission dialogs with capabilities and handling them to test both outcomes.
  • Mock location, orientation and clipboard are one-line commands on emulators and simulators.
  • Backgrounding, terminating and reactivating an app tests state restoration; know the difference between resume and cold start.
  • Deep links are both a feature to test and a shortcut for test setup.
  • Biometrics work on simulators and emulators; notifications are limited, especially on iOS.

Next: The Page Object Model for Mobile Tests

अपनी प्रगति ट्रैक करें

मुफ़्त अकाउंट बनाएँ, लेसन पूरे के रूप में चिह्नित करें और किसी भी डिवाइस पर वहीं से शुरू करें जहाँ छोड़ा था।