This site is in read only mode. Please continue to browse, but replying, likes, and other actions are disabled for now.
1 / 14
Feb 2016

Hello,

Is there a way to get the device id and run few scripts. for example
if (Deviceid == 4100951cf20c1215")) {
runthismethod();
}
else
{
runAnothermetod();
}

Please help me to get the device id and store it in a variable.

  • created

    Feb '16
  • last reply

    Jul '18
  • 13

    replies

  • 11.2k

    views

  • 8

    users

  • 3

    likes

  • 3

    links

Do you mean the uuid? You don't mention what platform so I'll give you iOS:

'instruments -s devices'

In my test framework I call this, parse the results and match the device name with the one the user put in settings.

Find Android device's model name:

adb devices -l

Find Android device's version no:

adb shell getprop ro.build.version.release

But how do I add this in the appium code. This I run from the terminal right?

In my framework we run a command like this in the setup, and then start the server using this value. You can make a system call from whatever language you are using to adb. You will probably need to parse the results and store the uuid in a variable.

Connect your android mobile to your computer using USB cable.
Then type below command in the terminal
adb devices -l

It will display all the connected device to your comaputer with device IDs.

And you can pass the device ID to your code using DesiredCapabilities
eg: if your device id is f23a5A83,

DesiredCapabilities Androidcapability=new DesiredCapabilities();
Androidcapability.setCapability("udid", "f23a5A83");

Below is the code I tried and works fine

java.lang.Runtime rt = java.lang.Runtime.getRuntime();
// Start a new process: UNIX command ls
java.lang.Process p = rt.exec("adb devices");
// Get process' output: its InputStream
java.io.InputStream is = p.getInputStream();
java.io.BufferedReader reader = new java.io.BufferedReader(new InputStreamReader(is));
// And print each line
String s= (reader.readLine());

	while ((s = reader.readLine()) != null) {
	System.out.println(s);
	if(s.contains("4100951cf20c1215"))
	{
		System.out.println("+++++++++++++++00000000000000");
	}
	if(s.contains("LGH815e29d29f9"))
	{
		System.out.println("___________________0000000000000000000");
	}
	if(s.contains("TA99301VXY"))
	{
		System.out.println("xxxxxxxxxxxxxxx0000000000000000000");
	}

	}
	is.close();

There are a couple known ways to retrieve Android device IDs. The first, most straightforward method is already listed in this thread: Spawn a child process that runs "adb devices" and then parse the results that are returned on the child's standard out. Although this will work, I feel that it's a bit shaky since your parent process will need a PATH environment variable that contains adb, and parsing the results is often frustrating to set up.

For Java, I think a better option is to include the same device library the Android SDK uses into your own test framework. Check out ddmlib41 and a ddmlib demo28 to get a feel for how to use it to retrieve devices. The downside is that you still have to be able to find adb, but you won't have to deal with parsing the results.

2 years later
3 months later

Hi, Can you show me Example how you do it ? call ‘instruments -s devices’ from framwork (Im using JAVA) and, parse the results (UDID) to String Array List ?

Thanks !!! :slight_smile: