-
you have to prepare environment for Android. Details are provided here
-
it needs to launch the appium server. You can launch Appium desktop application. If you use the server installed via npm then
$ node the_path_to_main.js_file --arg1 value1 --arg2 value2 It is not necessary to use arguments. The list of arguments
It looks like creation of a common RemoteWebDriver instance.
Common capabilities provided by Java client
Android-specific capabilities provided by Java client
import java.io.File;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.MobileElement;
import java.net.URL;
...
File app = new File("The absolute or relative path to an *.apk file");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
//you are free to set additional capabilities
AppiumDriver<MobileElement> driver = new AppiumDriver<>(
new URL("http://target_ip:used_port/wd/hub"), //if it needs to use locally started server
//then the target_ip is 127.0.0.1 or 0.0.0.0
//the default port is 4723
capabilities);
or
import java.io.File;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import java.net.URL;
...
File app = new File("The absolute or relative path to an *.apk file");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
//you are free to set additional capabilities
AppiumDriver<MobileElement> driver = new AndroidDriver<>(
new URL("http://target_ip:used_port/wd/hub"), //if it needs to use locally started server
//then the target_ip is 127.0.0.1 or 0.0.0.0
//the default port is 4723
capabilities);
This capability should be used
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, MobileBrowserType.CHROME);
//if it is necessary to use the default Android browser then MobileBrowserType.BROWSER
//is your choice
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.SELENDROID);
This automation type is usually recommended for old versions (<4.2) of Android.
Default Android UIAutomator does not require any specific capability. However you can
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.APPIUM);
You have to define this automation type to be able to use Android UIAutomator2 for new Android versions
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);
You can use io.appium.java_client.AppiumDriver
and io.appium.java_client.android.AndroidDriver
as well. The main difference
is that AndroidDriver
implements all API that describes interaction with Android native/hybrid app. AppiumDriver
allows to
use Android-specific API eventually.
The sample of the activity starting by io.appium.java_client.AppiumDriver
import io.appium.java_client.android.StartsActivity;
import io.appium.java_client.android.Activity;
...
StartsActivity startsActivity = new StartsActivity() {
@Override
public Response execute(String driverCommand, Map<String, ?> parameters) {
return driver.execute(driverCommand, parameters);
}
@Override
public Response execute(String driverCommand) {
return driver.execute(driverCommand);
}
};
Activity activity = new Activity("app package goes here", "app activity goes here")
.setWaitAppPackage("app wait package goes here");
.setWaitAppActivity("app wait activity goes here");
StartsActivity startsActivity.startActivity(activity);
Samples of the searching by AndroidUIAutomator using io.appium.java_client.AppiumDriver
import io.appium.java_client.FindsByAndroidUIAutomator;
import io.appium.java_client.android.AndroidElement;
...
FindsByAndroidUIAutomator<AndroidElement> findsByAndroidUIAutomator =
new FindsByAndroidUIAutomator<AndroidElement>() {
@Override
public AndroidElement findElement(String by, String using) {
return driver.findElement(by, using);
}
@Override
public List<AndroidElement> findElements(String by, String using) {
return driver.findElements(by, using);
};
};
findsByAndroidUIAutomator.findElementByAndroidUIAutomator("automatorString");
driver.findElement(MobileBy.AndroidUIAutomator("automatorString"));
All that AndroidDriver
can do by design.