AndroidDriver Class
AndroidDriver Class
If you are starting with mobile automation testing using Appium, one of the most important classes you will use is AndroidDriver. In this tutorial, you will learn what AndroidDriver is, why it is used, and how to use it with simple examples.
What is AndroidDriver?
AndroidDriver is a class provided by Appium that is used to automate Android applications. AndroidDriver belongs to the Appium Java client library and extends the AppiumDriver class. It is specially designed for Android platform automation.
It allows your test scripts to:
- Launch Android apps
- Click buttons
- Enter text
- Scroll the screen
- Verify elements
AndroidDriver works with:
- Real Android devices
- Android emulators
Using AndroidDriver
Before writing your first AndroidDriver script, you need:
- Java installed
- Appium Server installed
- Android SDK installed
- An emulator or real Android device
- A testing framework like TestNG or JUnit (optional but recommended)
Import AndroidDriver
First, you need to import AndroidDriver in your Java class:
import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.remote.MobileCapabilityType; import org.openqa.selenium.remote.DesiredCapabilities;
Create an AndroidDriver Object
To use AndroidDriver, you must:
- Set Desired Capabilities
- Start Appium Server
- Create AndroidDriver object
Step-by-Step Example
import java.net.URL;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class FirstTest {
public static void main(String[] args) throws Exception {
// Step 1: Set Desired Capabilities
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("platformName", "Android");
caps.setCapability("deviceName", "Android Emulator");
caps.setCapability("appPackage", "com.android.settings");
caps.setCapability("appActivity", ".Settings");
// Step 2: Create AndroidDriver
AndroidDriver driver = new AndroidDriver(
new URL("http://127.0.0.1:4723/wd/hub"), caps);
// Step 3: Close the session
driver.quit();
}
}
This example launches the Android Settings app and then closes it.
Perform Basic Actions
Click an Element
driver.findElementByXPath("//android.widget.TextView[@text='Network & Internet']").click();
Send Text
driver.findElementById("com.example:id/username").sendKeys("Admin");
Close the App
driver.quit();
Important Methods of AndroidDriver
| Method | Description |
|---|---|
| findElement() | Finds a single element |
| findElements() | Finds multiple elements |
| quit() | Ends the session |
| getPageSource() | Returns current screen XML |
AppiumDriver and AndroidDriver
AppiumDriver is a generic driver for mobile automation. AndroidDriver is specifically designed for Android platform and provides Android-specific methods.
AndroidDriver is the core class used for automating Android apps with Appium.
In this tutorial, you learned:
- What AndroidDriver is
- How to create it
- How to perform basic actions
- Important methods
Once you understand AndroidDriver, you are ready to build complete mobile automation frameworks.