Tencent cloud technology community – Gold digging home page continues to present cloud computing technology articles, welcome your attention!


Author: Fan Lin

takeaway

Android developers and testers will be familiar with ADB commands. Adb tools are a great way to get started with Android testing, including installation, uninstall, log capture, screenshots, and so on. Many automation and performance tools rely to some extent on ADB tools.

When I first started iOS, I always hoped that iOS would have something like ADB tools. Instead, I was told to use graphical interface tools like ITools and ITUNS. Later, after research, it is theoretically no problem to implement a set of ADB tools on iOS, there are many open source libraries to use, so I made a set of IDB (currently applicable to Windows platform), as the basic tool for iOS testing, mainly to achieve two functions.

1. Users can use IDB to perform basic operations on iOS phones, just like using ADB to operate Android phones;

2. Encapsulate the way of grabbing network package (generate PCAP file) and log, and improve the corresponding efficiency.

Complete idb tools download address (only for exchange learning) : share.weiyun.com/f28c0e8f6fb… .

The principle of

How does iTunes connect to your phone?

Step 1: Establish a connection with the mobile phone through THE USBMUX technology. Usbmux is a system that can establish multiplexing connections over A USB pipe (that is, a USB port). This is a TCP-like system through which the host port number can establish a connection and communicate with a specific port number in the mobile phone.

Step 2: Connect to the mobile lockdown service (available in June) using port 62708 and start the lockdown by transferring data to the lockdown service. This service can usually only establish a TCP connection via a USB port (the USBMUX system described in step 1). The Lockdown service can be used for many things, from installing applications, viewing files, restarting and more.

Communication protocols from low to high are as follows:

1. USB protocol: transmit multiple connected data through USB ports, similar to the data link layer in the network;

2. Usbmuxd protocol: open the TCP port connection on the device, which is in the transport layer of the network;

3. Lockdownd protocol: Used to transfer data with the lockdown service, similar to the application layer in the network.

This section describes idB principles

After learning the principle of iTunes, many people began to develop some libraries to achieve the same functions. The most famous one is LibiMobileDevice, which is based on C language, which is difficult to compile and configure and costs a lot to learn. A corresponding Python library, PyMobileDevice, was later published. Idb is implemented based on this Python library.

Idb architecture is divided into five layers:

1. Usbmux layer: USB driver layer;

2, PlistService layer: connection establishment layer, call usBMUx layer, complete the connection establishment, example code is as follows:

Self. s = mux.connect(dev, 62708);

3. Locdown encapsulation layer: encapsulate the basic Lockdwon service and complete the pairing verification with the mobile phone;

4. Function implementation layer: Lockdown is used to implement the corresponding functions. The core code is shown below:

Self installation functions. Lockdown. StartService (” com. Apple. Mobile. Installation_proxy “)

Implementation file related features self. Lockdown. StartService (” com. Apple. Mobile. House_arrest “)

5. Command line tool packaging layer: The original intention of the tool is to develop a set of tools similar to Android ADB. This layer is mainly modeled after ADB to realize the parsing of corresponding command line parameters and call the function implementation layer to complete corresponding functions.

Problems encountered and solutions

The choice of tool interaction mode and how to achieve it?

1. Imitate the interaction mode of ADB: the convenience and efficiency of ADB have been widely recognized by everyone, so the usage of IDB should be consistent with adb, so that the corresponding functions can be realized by directly entering idB + in the command line;

2. Specific implementation: The tool is developed and implemented in Python language, and the most common way of running Python is python+ script file name. It is necessary to directly enter IDB in the command line to realize the operation of the corresponding script Python script and realize the transfer of parameters;

The initial solution: package Python into an executable EXE file, which was heavy, cumbersome to change later, and incompatible with the Python scripting language, so it was abandoned.

Final solution: succinct BAT encapsulation (BAT is a batch language that comes with Windows).

A. Create an idb.bat file, add the directory where the idB. bat file resides to the Path environment variable, and enter IDb in the command line interface (CLI) to run the idB. bat file.

B. Python script path setting: Finally, you need to call the corresponding Python script to realize the corresponding function, in order to ensure normal operation in any directory.

In idb.bat, set the absolute path of the python script: The Python script and the bat script are in the same directory. Therefore, you can use %~dp0%mypy% to indicate the absolute path of the Python script. The mypy variable indicates the name of the corresponding Python script.

C, Bat parameter passing to Python: Bat scripts can use “%*” to represent arguments received on the command line, which can be passed to Python.

The specific implementation is as follows:

Encapsulation of complex functions

In iOS daily testing, there will be some requirements for packet capture and log capture, which are also troublesome in Android testing. Therefore, during IDB development, we hope to simplify network packet capture and log capture operations as much as possible.

1, interaction design: a command to start crawling, Ctrl+C end, the current directory to generate pCAP files and log files.

2. Concrete implementation:

A. Command encapsulation: IDB PCAP.

B, network package and log crawl to achieve universal encapsulation: core start and stop two functions.

Start: Start the worker thread to realize the corresponding function, and detect the corresponding flag bit in the worker thread at any time to see whether to end;

Stop: change the flag bit to end, trigger the stop of the worker thread; Process and store the generated files.

Catch Ctrl+C: When you run a Python program, Ctrl+C on the keyboard will throw a KeyboardInterrupt exception. You can catch the exception to achieve this function.

D. For easy viewing, the corresponding files are stored in the path of the command line at the current time.

Core code implementation:

Tool is introduced

Download address (only for exchange learning) : share.weiyun.com/f28c0e8f6fb… .

Directory structure:

Download the corresponding ZIP package and decompress it.

“.” You can import eclipse directly as a Pydev project for communication purposes only.

Idb. py Is a script that implements all idB functions.

The idb.bat entry function enables you to invoke related functions using the IDb command on the CLI.

The library called “PyMobileDevice_sheldonFan”, where all the required Python libraries are already placed.

Main functions:

1. Support basic operations of mobile phone: restart, lock screen, sleep;

2, support application installation, uninstall, view;

3, support sandbox directory view and basic file operation (iOS10 after restrictions, temporarily unable to perfect support);

4, support log, network PCAP package one key crawl.

Environment depends on

1. Install python2.7 (32-bit version) and set environment variables. The setting method is as follows:

Open: Computer — right click — Properties — top left corner “Advanced System Settings” — bottom right corner “Environment Variables” — System Variables — “Select” Path “– click” Edit “– add python’s directory with”;” Split, do not change — “click OK.

The Python directory refers to the Python installation directory, which is installed in C:\Python27 by default. The Python directory needs to contain Python executables, as shown below:

2. Windows 7, iPhone driver installed (check standard, various iPhone assistant can identify and connect the phone);

3, after downloading, you can add the directory to the PC environment variable, convenient to call anywhere.

Usage,

If you have added environment variables, simply open CMD.

1. Restart the mobile phone by running the idb reboot command.


2. Shutdown: IDB shutdown.

3. Put the phone to sleep: idb sleep // This command will put the phone into a deep sleep and the USB connection will die; Press the power button to light up the screen and it will connect normally again.

4. Install APP (IPA file) : IDB install IPA file path.

5. Uninstall app: IDB uninstall “APP ID” (similar to the package name in Android)

6, display all apps: idb listapps // Default display all apps.

7, display the user installed APP: IDB listapps user.

8. Display the built-in APP idB Listapps sys.

Idb shell //ios uses the sandbox environment. By default, images, books and music of the system are entered, and only these files can be displayed:


10. Obtain system files on the local computer: IDB pull Mobile phone directory PC directory The mobile phone directory can be viewed by using the IDB shell command (the previous command).

Example: IDB pull /DCIM.

Capture the DCIM directory on the mobile phone to the current directory. Said).


You can pull a single file or an entire directory.

11. Push file to system directory IDB push “PC file path” “mobile phone directory”

The mobile phone directory is also determined by IDB shell (mobile phone cannot recognize the image after push, and will continue to optimize later). 12. Obtain the files in APP sandbox: idb -p “APP ID” pull “Mobile directory” “PC directory”.

The following is an example:

idb -p com.tencent.mlife.dailybuild pull Documents . Will apply id for the com. Tencent. Mlife. Dailybuild application Documents directory, to the current directory (using the “.” Said)


13, Push file to app sandbox: IDb -p “app ID” push “PC file path” “phone directory”

14. Idb logcat captures system logs or redirects them to files using idB logcat >mylog. TXT.

15, IDB screenshot, this need mobile phone installed developer functions, if not installed, you can consider pressing the power button and the home button on the phone, and then pull to the computer.

16. Idb PCAP packet capture, start packet capture, press Ctrl+C to end, the corresponding PCAP file and log file will be generated in the directory where the current CMD resides.

Press Ctrl+C to end the capture, prompt the total traffic and generate pCAP files and log files (system logs) in the current directory.

Matters needing attention

1, the driver must be installed first, if the computer can not recognize the phone, all the commands can not be executed;

2. Sometimes the phone cannot be connected, which may be a driver loading problem. You can try to restart iTunes to reinstall the driver, restart the computer, and restart the phone.

3. The sandbox environment of some apps is not accessible, which is related to the APP Settings. The average daily beta APP (unofficially distributed) allows access to the sandbox directory for easy access to files. But iOS10 adds restrictions on sandbox directories, which don’t work perfectly;

Python2.7 32-bit is currently supported. Other versions are not currently supported (due to some limitations of reference libraries).

For more test related dry goods, please pay attention to our wechat public account — Tencent Mobile Quality Center TMQ:

How to deal with being thirty, programmer?Click for details

ApacheBench (AB), a Web performance measurement tool, summarizes why your phone gets hot


Has been authorized by the author tencent cloud community released, reproduced please indicate the article source The original link: cloud.tencent.com/community/a… Get more Tencent mass technology practice dry goods, welcome to Tencent cloud technology community