Python calls android adb command

Source: Internet
Author: User

1.1. Overview

Today is the 11th article in the Python Automation test application, which will continue with the tenth ADB command combined with those pits actually encountered during the Pyapp framework writing process. The following is not a simple list of commands (because the network of a lot of people concerned about what can be their own search), but the continuation of the usual style to combat-oriented. Okay, everybody, sit tight, I want to start, haha.

1.2. The ADB commands that need attention 1.2.1, "adb shell input ..."

This command, in the test process is also often used, it can be followed by Tap,text,swipe, click on the screen, input text, slide screen operation, specific in Python using the format of the command will not have any problems. But what if we're going to hold on to an element and do it for specific functions? The implementation code is given below.

def long_press (dev,data,hold_time):

action= ' adb-s ' +dev+ ' shell input touchscreen swipe ' + '%d '%data[0][0]+ ' + '%d '% (data[0][1]) + ' + '%d '%data[-1][0]+ ' + '% d '% (data[-1][1]) + ' +hold_time

Print action

Pi= subprocess. Popen (action,shell=true,stdout=subprocess. PIPE)

Long_press (' 4d0041b1be98b01f ', [[540,716],[545,718]], ' 1000 ')

can see my definition of the Long_press method, the action is still the swipe command, you know swipe is the operation of the sliding screen, then if we pass the sliding range coordinates, set the sliding range is very small, Then is the indirect reach of a long press for the purpose of an area, and then with an entire command of the execution time parameters, is not the perfect solution to the long press this action.

Let's take a look at the full adb command:

Adb-s 4d0041b1be98b01f Shell input touchscreen swipe 540 716 545 718 1000

Explanation:-S followed by the device number, swipe to move the coordinate range "540 716 545 718", then 1000 is the long-press time, 1000 of the Unit is milliseconds (note).

Let us not think that this order is over, not yet. In the sense that the source behind the input in the swipe command is the default is touchscreen, so generally we write this command can be omitted touchscreen, But actually when I write Pyapp framework (because Pyapp design need to support more than one device), take the different models of brand (resolution) of the mobile phone to do the adaptation, after all, the framework to take care of the scope must be broad to have a certain universality. I found that in Pyapp's wingman mode (that is, one master device controls multiple attached devices to get the latest Pyswat and Pyapp programs), the master device's long-press operation was unsuccessful on some of the secondary devices, and I also omitted the touchscreen in the command at first. Can not begin to think of any reason, because the command is not complex, there is no error, after all, some of the devices can still respond to the action correctly, and then I put the shell Input command help print out the results of a study (see the previous article), In the parameter passing of the swipe command, it is stated that touchscreen is the default property, so it is normal that we do not explicitly specify the value of the parameter in the command, but I am also a blind cat encountered a dead mouse, try to add the touchscreen in order, Final execution results Each sub-device succeeds on the execution of the long-press command.

1.2.2 and adb command How to enter Chinese?

The DB command input text input ' adb-s device number shell input text input content '. Chinese input is not supported in the native ADB command, so we can only enter English characters when testing. However, we do not need to input Chinese characters when we do the application test, here we introduce a way to save the nation from the curve. Using the "Adbkeyboard" Input method to enter Chinese, by broadcasting the way to reach the input characters, the specific command: ADB shell am broadcast-a adb_input_text--es msg "content."

1.2.3, starting the application

Only one app can be opened at a time in the Appium app, and if the ADB command is a lot more flexible, enter: ADB shell am start-n package name/.activity name, The package name and activity name are the same as those configured in Appium. For example, we start the calculator program, the corresponding command is "adb shell am start-n com.android.calculator2/." Calculator ". Here to remind you a little bit, the app's package name and activity name must find the right, especially the activity name, specifically, you can see my "Python Automation test Application-2nd (app test)--appium first Knowledge article" in the inside explains the specific search method. Of course, everyone can also be based on the ADB command to find, in the Pyapp test framework I was based on the ADB command name lookup started the application. Here I explain the idea, using the command adb shell pm list packages-3, to list the third-party apps installed in the phone, and then to find the new installed app to determine the name by time. In fact, as long as we have the idea of Baidu can find the corresponding command.

1.2.4, will only send text messages, then look at text messages? No, you're out!

online you may find how to send text messages, call the relevant ADB command introduction, in fact, the principle is described in 1.2.3, or start the corresponding application to implement. For example, short interest: adb shell am start-a android.intent.action.sendto-d smsto: Send a number--es sms_body SMS content. So how to read a text message content?

In the process of writing Pyapp framework, in order to realize the application of automatic verification code filling function, it really took a bit of effort. At present, most of the registered login can be used dynamic SMS verification code to operate, of course, the app developer itself can be achieved after receiving a text message to read the verification code to automatically fill out, encountered without this feature of the app we can of course follow this principle to achieve. Since all SMS messages are stored in the database, we simply find the database file of the text message, and we can easily read the text message through the Python database operation. The "mmssms.db" file in the '/data/data/com.android.providers.telephony/databases/mmssms.db ' path is the database file that holds the SMS content. Then the rest of the work is naturally the database read operation, with regular expression matching to the verification code, and finally through the ADB input command written to the app. It is also important to note that the operation "mmssms.db" file requires root permission, so you want to use this feature in the Pyapp framework must be a mobile device using root.

How does the 1.2.5, adb command su permissions work?

What is the SU permission for the ADB command, give a simple example, such as a file that you want to access in the memory/data/system path of the phone. In the CMD command window you need to perform three steps:

(1) Enter the command in the cmd form: adb shell enter


(2) Enter SU return


For more information, please click here to download:

Http://www.51testing.com/html/39/n-3723839.html

Copyright NOTICE: This article is from the "51 Test World" phase 48th. 51Testing Software testing network and related content provider owns all copyright of 51testing.com content, no person or entity may copy, reprint or mirror the contents of this website without express written permission, otherwise it will be held liable.


Python calls the android adb command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.