How to Manage Appium Capabilities Dynamically

Hello everybody, If you are a mobile automation engineer, you would know that managing your mobile device capabilities need so much effort in case you change your device frequently. During the test, there are mandatory capabilities that you need to manage like “Platform Name”, “Device Version”, “Device Name” or “UDID” information. In this tutorial, I will explain how to manage appium capabilities dynamically.

You need to run ADB command to extract those kinds of data or you need to manually find them out.

Thanks to Bahadır Üstün, we recently developed a library to fetch information from devices connected to your computer. It fetches any information that can be captured from your IOS or Android device.

In order to use it on Android device, you need to install Android Debug Bridge (ADB) and for IOS, you should install libimobiledevice. Then you can just use a few lines to get a connected device and pass that information to your Desired Capabilities.

Here’s an example:

DeviceInfo deviceInfo = new DeviceInfoImpl(DeviceType.ALL);

Device device = deviceInfo.getFirstDevice();

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PLATFORM, device.getDeviceProductName());
capabilities.setCapability("platformName", device.getDeviceProductName());
capabilities.setCapability(CapabilityType.VERSION, device.getProductVersion());
capabilities.setCapability("deviceName", device.getModelNumber());
capabilities.setCapability("udid", device.getUniqueDeviceID());
capabilities.setCapability("app", "#Your App File#");

driver = new RemoteWebDriver(new URL(URL), capabilities);

So whenever a new mobile device is registered into your laptop, you don’t have to change a single line of code. Here is the library GitHub Link: https://github.com/Testinium/MobileDeviceInfo 

Thanks.
Canberk

4 thoughts on “How to Manage Appium Capabilities Dynamically”

  1. Hello,
    when i implement your Lib and test it, i get an ArrayoutofBound :1 . Can you look it up if it is on me or if you have a Bug in your Code.
    thank you in advance

    Reply
  2. Hello Frederic,

    Actually, I never had any problem like that. I tried several cases where my phone’s debug mode is turned-on/off. Never had that.

    What happens when you run “adb devices” on the command prompt?

    Reply
  3. Hello,

    I have same issue, when i using emulator device its working,
    but when i using real device i get this error :

    Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1
    at com.testinium.deviceinformation.device.AndroidDeviceFinder.readDeviceInfo(AndroidDeviceFinder.java:60)
    at com.testinium.deviceinformation.device.AndroidDeviceFinder.findDevices(AndroidDeviceFinder.java:26)
    at com.testinium.deviceinformation.device.DeviceFinder.findDevices(DeviceFinder.java:14)
    at com.testinium.deviceinformation.DeviceInfoImpl.getDevices(DeviceInfoImpl.java:36)
    at com.testinium.deviceinformation.DeviceInfo.getFirstDevice(DeviceInfo.java:16)
    at Lab.GetPhone.main(GetPhone.java:16)

    Thanks,
    Hazard

    Reply
  4. Hello,

    I actually never hd that problem on any environment. I haven’t tried the code since a month. I just followed all the instructions in the article and installed everything right now. I worked with my Android device.

    Could it be possible that you have some missing dependency?

    Can you post the code also?

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.