Android Emulator
Introduction
When a physical Android device is not available, you can use an Android emulator to run your automated tests. This guide explains how to set up and use an Android emulator with Appium, focusing on the adjustments needed for the capabilities in the previous examples. 
Prerequisites
- Android Studio installed, with the Android Emulator component.
Creating an Android Virtual Device (AVD)
- Open Android Studio.
- Go to Tools > AVD Manager.
- Click on Create Virtual Device.
- Select a device definition (e.g., Pixel 6) and click Next.
- Select a system image (e.g., the latest stable Android version) and click Next.
- Verify the configuration and click Finish.
Running the Emulator
Once the AVD is created, you can start it from the AVD Manager by clicking the Play icon next to the virtual device.
Adjusting Capabilities for Emulator
To run the previous examples on the emulator, you need to modify the Appium capabilities. The main change is to specify the emulator's device ID (udid) and, in some cases, you might not need the unlockKey and unlockType capabilities if the emulator doesn't have a lock screen.
Finding the Emulator's udid
Start the emulator.
Open your terminal and run the following command:
shelladb devicesThe output will list the attached devices, including the emulator. The emulator's udid is typically in the format
emulator-5554.textList of devices attached emulator-5554 device
Python Example Adjustments
For the Python examples, you would adjust the capabilities dictionary as follows:
capabilities = dict(
platformName='Android',
automationName='uiautomator2',
udid='emulator-5554', # <-- Change this to your emulator's udid
autoGrantPermissions=True,
# The following lines might not be needed if the emulator has no lock screen
# autoUnlock=True,
# unlockType='pin',
# unlockKey='123456',
app='path/to/your/app.apk' # Or appPackage/appActivity
).NET Example Adjustments
For the .NET example, you would adjust the AppiumOptions as follows:
var options = new AppiumOptions
{
PlatformName = "Android",
AutomationName = "UiAutomator2",
App = @"C:\path\to\your\app.apk" // Or appPackage/appActivity
};
options.AddAdditionalAppiumOption("udid", "emulator-5554"); // <-- Change this to your emulator's udid
options.AddAdditionalAppiumOption("autoGrantPermissions", true);
// The following lines might not be needed if the emulator has no lock screen
// options.AddAdditionalAppiumOption("autoUnlock", true);
// options.AddAdditionalAppiumOption("unlockType", "pin");
// options.AddAdditionalAppiumOption("unlockKey", "123456");Recommendations
- Performance: Emulators can be resource-intensive. Ensure your computer has enough RAM and CPU power to run the emulator smoothly.
- Clean State: For consistent test results, it's a good practice to start the emulator in a clean state for each test run. You can achieve this by wiping the data of the AVD from the AVD Manager.
- Google Play Services: If your application depends on Google Play Services, make sure to select a system image that includes the Google APIs when creating your AVD.