What is ADB?

Adb stands for Android Debug Bridge. It is a client server program which allows us to communicate with an Android device or an Emulator using command line.

A client, which runs on our machine. We can invoke a client by issuing an adb command.

A daemon, which runs as a background process on each emulator or device instance. It by default runs on port number 5037

A server, which runs as a background process on our machine. It manages communication between the client and the adb daemon running on an emulator or device.

Where is it located?

After successful download of android sdk, the adb tools comes with SDK inside the platform-tools folder. You can get the path using “which adb” command.

Pre-requisites:

Before running adb commands, ensure that

USB debugging is enabled and the device is connected to the system using USB.

List of ADB commands and usage.

adb devices : To find out list of connected devices.

Offline : The device is not connected or not responding

device : The device is connected to adb server

no device : There is no device connected to adb server

adb usb → To switch to USB mode.

adb tcpip [port no] → To start TCP IP Connection.

adb connect [device IP address] → To connect devices over wifi.

adb version → To check ADB version.

adb logcat → To display logs of connected devices.

adb logcat > path of the file\log.txt → To store adb logs in text file.

adb kill-server → To kill adb server.

adb start-server → To start adb server.

 

adb shell → To run the terminal of host Android device. Below command can be used with adb shell command.

getprop ro.product.model → This command get’s the attached device model no

getprop ro.build.version.release → This command get’s the android build release version i.e. 4.4.2/4.4.4

getprop ro.build.version.sdk → This command get’s the device api version i.e. 19,20,21,22

pm list packages –f → To get app package of installed apps.

How to install apk using abd command?

  1. Get the apk file from dev or any other source.
  2. Paste in your system location.
  3. Connect your device and your device should have usb debugging enabled.
  4. Open command prompt .
  5. Type below command (enter your apk file name without angular bracket).

Note: Make sure when you execute you have .apk extension mentioned.

adb install <YourApkName>.apk

If multiple device connected then how to installed into a particular device.

Solution
  1. Follow 1-4 steps from solution 1.
  2. Type <adb devices> in command prompt.
  3. This will list out the connected device.
  4. Note the device id and enter below command

adb -s <YourDeviceId> install <YourApkName>.apk

If Apk Installation failed due app is already exists,

Enter command prompt and enter below command.

adb install -r <YourApkName>.apk

adb uninstall [package name or apk’s path] → To uninstall apk of package.

adb install -r [apk] → To command to update the app.

Adb command for file sharing.

adb pull <source folder><destination folder>

Command to send files from computer to phone

adb push <source folder><destination folder>

Understanding DesiredCapabilities

DesiredCapabilities is a JSON object in the form of a key and value pair sent from client to server. It has basic set of information to create automation sessions. Let's look out some essential desired capabilities.

cap.setCapability(MobileCapabilityType.DEVICE_NAME, "0797818a87d4f5cb");

It shows the kind of mobile device or emulator used for automation. Such as “Samsung S10” , “Google Pixel”, “Android Emulator”, “iOS Simulator”.

cap.setCapability(MobileCapabilityType.VERSION, "9.0");

The device operating system such as 7.0, 9.0, 10, 11 etc

cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");

cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");

The device OS platform such as iOS or Android.

cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");

The automation engine, for android mention UIAutomator2 and for iOS it is XCUITest

cap.setCapability(MobileCapabilityType.UDID, "E5EB4A6D-32AC-4B2A-9AA8-3BC25D8FC0BF");

Unique device identifier of the connected physical device. We will later learn how to get this for android and iOS.

cap.setCapability(MobileCapabilityType.APP, “//Path to build file”));

The absolute or local path for build. For android it is .apk and for iOS .ipa and .app

cap.setCapability("noReset","true");

Don't reset the app before this session. It could be true or false.

cap.setCapability("fullReset","false");

It performs a complete reset, it could be true or false.

cap.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "io.appium.android.apis");

cap.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, ".ApiDemos");

The above capabilities are mainly used to invoke installed android app.  App package is the java package of an app on which you want to run. App activity is the android activity you want to launch from your package. Generally apps start from .splashActivity. We will see how we can get it later.

In the case of iOS if you want to invoke installed app then use budleID with app desired capabilities.

Example: cap.setCapability(MobileCapabilityType.APP,"com.example.apple-samplecode.UICatalog");

We will discuss how to get this later.

How to get the app package and activity of an app?

  1. Connect your device and make sure its detected by adb.
  2. Then open command prompt type adb shell
  3. Now type dumpsys window windows | grep -E 'mcurrentFocus|mFocusedApp' 
  4. "Do not press enter now". Launch the app in your device and at the same time execute the above command it will list out the package name and the current activity. Sample output:

C:\Users\Siddharth>adb shell

dreamlte:/ $ dumpsys window windows | grep -E 'mcurrentFocus|mFocusedApp'mFocusedApp=AppWindowToken{6ec47ae token=Token{e3aa829 ActivityRecord{59aa1b0d0 u0 io.appium.android.apis/.ApiDemos t2017}}}

dreamlte:/ $

From above output :

"io.appium.android.apis" is your app package.

".ApiDemos" in your app activity.

App package and app activity to be used in desired capabilities as:

DesiredCapabilities cap = new DesiredCapabilities();

 cap.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "io.appium.android.apis");

 cap.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, ".ApiDemos");

Official link: http://appium.io/docs/en/writing-running-appium/caps/

How to get bundleId (for iOS only)

Appium provides bundleID to identify the iOS application. Generally in organizations we can get it the same from developers. If the developer is not unknown and you are working on public application then refer below steps.

A . Using Command prompt.
  1. Get the file (.ipa or .app) file to your local. Here we will take example of UICatalog app.
  2. Run below command.
  3. osascript -e 'id of app "/path to the app/UIKitCatalog.app"'
  4. It will will give you the output as com.example.apple-samplecode.UICatalog.

B. From the apple store app store.
  1. Take the Udemy app.
  2. App store link for Udemy app is : https://apps.apple.com/us/app/udemy-online-video-courses/id562413829
  3. Note down the id of the app 562413829
  4. Create the link to get the bundle id as https://itunes.apple.com/lookup?id=562413829
  5. Open the above link in a browser that will download a text file. Open the file and search for bundleID bundle id is com.udemy
C. From Console.
  1. Launch the ios simulator from xcode or connect your iPhone to mac.
  2. From spotlight search , search for Console and launch.
  3. Select your service from the left side list then search for the app as shown below.

How to get the UDID of a device?

A UUID is a string of letters and digits that forms a unique pattern. Your Mac, iPhone, and iPad each have one UUID, and no other device shares it. It’s similar to a serial number in that regard, but whereas serial numbers are used to identify your device by Apple and your cellular carrier, developers usually use the UUID instead.

For Android
  1. Make sure the device has usb debugging enabled and connected via USB.
  2. From terminal enter command adb devices . This will give you the UDID.
For iOS devices using Xcode

Launch simulator from Xcode or connect an iPhone to mac.

Click to menu windows, select devices and simulators.

Click to the tab Devices or Simulators as per your device/Simulator.

Select the device and on the right side you will get the UDID as an identifier. Refer below screenshot.

CLU tool for iOS as like ADB for Android

CLU is nothing but the Command Line Utility tool. To save the space for the menu link name, I’ve used the short term CLU. You know very well ADB is nothing but the Android Debug Bridge used to connect and control the Android emulators or devices in the command prompt. I would like to share some of the important xcrun commands in this post, which will be useful for you to control the iOS simulator.

XCRUN is going to play a major role for iOS platform as like ADB in Android platform. XCRUN is a dedicated one for iOS and ADB is a dedicated one for Android, which cannot be used vice-versa.

What is XCRUN?

XCRUN is a binary file available in the XCODE tool in which simulators installed in the

device can be controlled. If you compare with the Android platform, it is almost equivalent to ADB(Android Debug Bridge). ADB can be used to control the Android Emulators and the real devices running in the Android platform. XCODE is an IDE with the iOS SDK’s which is used to develop the iOS applications in Mac machine. And the same XCODE should be used for iOS Automation to build the code, install the application, invoke the application etc,.

What is SIMCTL?

simctl will work with the xcrun binaries in which it is nothing but Command line utility to control the Simulator, which is again developed by using the Ruby. All the simulator controlled commands will work based on the combination of XCRUN and SIMCTL.

SIMCTL – Simulator Control

Run the following the commands in the terminal window and check the expected results for your reference;

How to list the device types, runtimes and devices;

$ xcrun simctl list

How to list the devices;

$ xcrun simctl list devices

How to list the available device types;

$ xcrun simctl list device types

How to list the runtimes available in the Mac;

$ xcrun simctl list runtimes

How to list the Known Devices;

$ instruments -s devices

How to get the real device UDID; Tried with iPhone 7 real device

$ idevice_id -l

How to install the .APP file in the simulator under the command line.

$ xcrun simctl install<space><device id><space><path of the .app build file>

$ xcrun simctl install 37CEC824-C20E-4D77-8A69-59DD778928AF /Users/gopikannan/Library/Developer/Xcode/DerivedData/UICatalog-alaibrcewphnhpbkacmggbnkdyim/Build/Products/Debug-iphonesimulator/UICatalog.app

How to Uninstall the .APP file in the simulator under the command prompt.

$ xcrun simctl uninstall<space><device id><space><application identifier>

$ xcrun simctl uninstall 37CEC824-C20E-4D77-8A69-59DD778928AF com.example.apple-samplecode.UICatalog

How to take screenshot.

$xcrun simctl io<space><device id><space>screenshot<space><filename.png>

$ xcrun simctl io 37CEC824-C20E-4D77-8A69-59DD778928AF screenshot testscreen.png

How to record the screen; Ctrl+C to stop in the command prompt.

$xcrun simctl io<space><device id><space>recordVideo<space><filename.mov>

$ xcrun simctl io 37CEC824-C20E-4D77-8A69-59DD778928AF recordVideo testfile.mov

How to create a simulator in command prompt.

$ xcrun simctl create “iPhone 6” “iPhone 6” 9.2

How to launch the simulator in command prompt.

$crun simctl launch<space><device id><space><application identifier>

$ xcrun simctl launch 37CEC824-C20E-4D77-8A69-59DD766628AF com.example.apple-samplecode.UICatalog

How to get help information about xcrun simctl? You could get all the command information.

$ xcrun simctl help

In the next post, we will discuss about UIAutomatorviewer.


Tags


You may also like

API Basics and Architecture

API Basics and Architecture
Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}