ADB summarized in automated test applications APP

## ADB simulation key operation

Analog Click
adb shell input tap posX posY

Analog sliding
adb shell input swipe posX1 posY1 posX2 posY2 scrollTime

Do swiping operation on the screen, the number of the first four coordinate points, followed by sliding the time (in milliseconds)

Enter the text
adb shell input text string

Analog button
adb shell input keyevent keycode

Wherein keycode correspondence table is as follows:

keycode num
KEYCODE_UNKNOWN 0
KEYCODE_MENU 1
KEYCODE_SOFT_RIGHT 2
KEYCODE_HOME 3
KEYCODE_BACK 4
KEYCODE_CALL 5
KEYCODE_ENDCALL 6
KEYCODE_0 7
KEYCODE_1 8
KEYCODE_2 9
KEYCODE_3 10
KEYCODE_4 11
KEYCODE_5 12
KEYCODE_6 13
KEYCODE_7 14
KEYCODE_8 15
KEYCODE_9 16
KEYCODE_STAR 17
KEYCODE_POUND 18
KEYCODE_DPAD_UP 19
KEYCODE_DPAD_DOWN 20
KEYCODE_DPAD_LEFT 21
KEYCODE_DPAD_RIGHT 22
KEYCODE_DPAD_CENTER 23
KEYCODE_VOLUME_UP 24
KEYCODE_VOLUME_DOWN 25
KEYCODE_POWER 26
KEYCODE_CAMERA 27
KEYCODE_CLEAR 28
KEYCODE_A 29
KEYCODE_B 30
KEYCODE_C 31
KEYCODE_D 32
KEYCODE_E 33
KEYCODE_F 34
KEYCODE_G 35
KEYCODE_H 36
KEYCODE_I 37
KEYCODE_J 38
KEYCODE_K 39
KEYCODE_L 40
KEYCODE_M 41
KEYCODE_N 42
KEYCODE_O 43
KEYCODE_P 44
KEYCODE_Q 45
KEYCODE_R 46
KEYCODE_S 47
KEYCODE_T 48
KEYCODE_U 49
KEYCODE_V 50
KEYCODE_W 51
KEYCODE_X 52
KEYCODE_Y 53
KEYCODE_Z 54
KEYCODE_COMMA 55
KEYCODE_PERIOD 56
KEYCODE_ALT_LEFT 57
KEYCODE_ALT_RIGHT 58
KEYCODE_SHIFT_LEFT 59
KEYCODE_SHIFT_RIGHT 60
KEYCODE_TAB 61
KEYCODE_SPACE 62
KEYCODE_SYM 63
KEYCODE_EXPLORER 64
KEYCODE_ENVELOPE 65
KEYCODE_ENTER 66
KEYCODE_DEL 67
KEYCODE_GRAVE 68
KEYCODE_MINUS 69
KEYCODE_EQUALS 70
KEYCODE_LEFT_BRACKET 71
KEYCODE_RIGHT_BRACKET 72
KEYCODE_BACKSLASH 73
KEYCODE_SEMICOLON 74
KEYCODE_APOSTROPHE 75
KEYCODE_SLASH 76
KEYCODE_AT 77
KEYCODE_NUM 78
KEYCODE_HEADSETHOOK 79
KEYCODE_FOCUS 80
KEYCODE_PLUS 81
KEYCODE_MENU 82
KEYCODE_NOTIFICATION 83
KEYCODE_SEARCH 84
TAG_LAST_KEYCODE
85 

 

android simulation press event in adb

Done software development people know that the majority of software development, there will be pressure test, android system is no exception, if you're doing the android system and restraint, even in the restraint of some of the key, then you might go to these stress tests keys are strong, you can not let a person have been repeated to press a key or a few keys, because usually measured pressure for 1 day to several days in duration, you can also write an apk or process to pressure measurement but such an extension is not flexible enough, reusability is not high, then think of shell scripts to simulate these buttons, if you want to replace the pressure measuring button just change the key value to them.

Ado, then I will explain three kinds of analog Press:

The first: use the input keyevent --longpress <key> at adb

For example, long press the home button can be used: input keyevent --longpress KEYCODE_HOME, KEYCODE_HOME android is defined as the home button key, it is a macro, in fact, is a number.

Of course, you can also use numbers: input keyevent --longpress 3 keys corresponding macro is KEYCODE_HOME 3, can simulate long-press the home button. android there are many keys, a numeric keys, letter keys, we can not remember so many numbers, so it is recommended to use the first macro way. These macros corresponding online with a number of key correspondence table I will not list them, this is more full, please refer to: Android KEYCODE key value Daquan .

This press that the way what does drawback: long press time is too short. For example, we press the power button to simulate exhaled off or press the menu button switch applications exhaled split screen (android7.0 new), a long time is too short in this way. Long press and drag to select only applies when the beginning of the case.

Second: the use adb input swipe <x1> <y1> <x2> <y2> [duration (ms)]

The above instruction is actually slidable this instruction, x1, y1 and x2, y2 are the two coordinates, duration is from x1, y1 to the sliding x2, y2 at a predetermined time. Why can simulate sliding long press it: As long as the difference between our two coordinates is small enough, still within the current scope of this button, android system will think we had a long operation to press a button, of course, this can only analog virtual button, because we are unable to obtain the entity key to its coordinates.

So what coordinate small enough to meet it, it seems to me just add 1 to the coordinate values ​​in the original:

input swipe <x1> <y1> <x1 + 1> <y1 + 1> [duration (ms)], since an account for the position in the screen is small, certainly not in general exceed the size of the button, so long as accurate positioning button coordinate position, this method can achieve a long press a virtual button. So how do you determine the coordinates of the location of a button of a key.

android provides a method for obtaining from the sender to the upper-driven input method --getevent event, this method to get the key and the coordinate value on the android input device file. We only need to run this command in adb, you can obtain the coordinate values ​​of our current click of a button. I suggest you run this feature will only need to run getevent & this instruction, each time any of us on the phone or tablet in the background a little, will play the corresponding coordinate values. I clicked the home button is shown in print coordinates situation, of course, if you press the key entities, will print out the key, rather than coordinates, then I will speak.


add device 1: / dev / input / event0: represents a device file for a flag input device, of course, such a file can have multiple input devices, each corresponding to a different device drivers, such as might androidTV remote control, a keyboard, etc. Wait. Since I used here is the simulator, only one device file.

The first column: / dev / input / event0 on behalf of a different device received event

Second row: 0001 represents a type

The third column: If you click on the virtual keys on the screen, this value is used to identify 014a clicked on the screen. If the value is 0 for x, 1 for y values ​​are in hexadecimal. Other values ​​represent key.

The fourth column: the representative value generally represents 00000001 Press, 0 represents release. Other values ​​representative of the coordinates (virtual key)

So we can see, click on a screen, click on the print out will coordinate value of the point, we will coordinate the conversion to decimal values, using the swipe commands can be achieved by a long, such as the image above long press the home button can be expressed as:

input swipe 546 1860 547 1861 2000 It's that simple, rush to try it.

Although this method can control the time of press, but can only simulate virtual keys. If you want to simulate physical keys, such entities will have a lot of android TV button on the remote control. At this time, necessary to use the third program.

Third: sendevent device type code value

Since there getevent get input event, then there will certainly be sendevent send events, the same token, this method will be sent to the device-specific key file, which can change the key value drivers reported to the application or system. As a simple example: sendevent / dev / input / event0 1 213 1 transmits the command is a key to the device file / dev / input / event0 is pressed (down) operation 213. sendevent / dev / input / event0 1 213 0 command is a command is sent to the device file, so that the key 213 release (up). Thus according to a place among us just let it sleep a few seconds to realize long-press. So the key is how to determine the keys keys, so this is a macro key programs and we mentioned a similar KEYCODE_HOME defined values ​​are not the same to be stated here, then I'll Why, so you will find that when you sendevent used when a program with the key mapping table android key does not work. So how do I find the key is the key.

1, you can use the second method getevent programs provide access to this key that you press a key entity will still print key entity keys. The following diagram is the key to print after I press the power button:

0074 is the key power button, the conversion to decimal is 116, so you can now implement a bold, long press the power button of your total energy, and only need to use the following script:

#! / bin / SH
sendEvent / dev / INPUT / event0. 1 1 16. 1
sendEvent / dev / INPUT / event0 0 0 0
echo "Down"
SLEEP. 3
sendEvent / dev / INPUT / event0. 1 1 16 0
sendEvent / dev / INPUT / event0 0 0 0
echo "up"

at this time to simulate the effect of long press the power button:

That if we did not have a cell phone or how to do it with this key, for example, I want to simulate a button on the gamepad, but this time I did not handle the game, how to obtain these keys do, which relates to the second get key way.

2, there will be twice as a key mapping android system, the first key is mapped to the drive system, the system then maps it to the key framework we use (i.e., a program that is mentioned in our those macro KEYCODE_HOME), as a programmer, we do not want the key every time we use are not the same, so the android system will throw the underlying key mappings do it again and make sure that the key used by the application framework consistent programmer when writing applications no longer have to worry about different keys from different manufacturers using a cell phone or TV, because the android system already defined the standard. For example, home button key 3 is, whether your applications this key 3 does not appear on any other platform becomes key.

However, different vendors define their own special keys, which is where to define it, android system provides a mapping file, you can use dumpsys input to find the file. Figure:

The figure shows two .kl document is the map file. They are:

/system/usr/keylayout/qwerty.kl and /system/usr/keylayout/Generic.kl these two documents, Generic.kl is the default file, if there are no other manufacturers of restraint .kl file, the system will use the default files, so we need to find a file in /system/usr/keylayout/qwerty.kl our key underlying driver to use. Figure:

Enter / system / usr / keylayout directory, you will see a lot .kl file, that is, different manufacturers restraint of key correspondence between the image above you can see my key device driver used in correspondence between the, we can see the POWER button to drive the corresponding keys is 116, so you understand why getevent obtained using hexadecimal key is 0066 now, because of the use of the driver or the device file is / system / usr / keylayout /qwerty.kl (different manufacturers are not the same, you can use dumpsys input specific view) in the key. This will not only be able to meet the needs of manufacturers of restraint, but also to meet the application requirements of the upper keys, it is not very reasonable.

If you are interested at this time to break the routine when a person, you can also change the CAMERA keys 116, then you can use the power button to take pictures, you can also change the lock screen, home button key, only that you can also add your own after some of the key you want, custom buttons, just your phone root, connected to the adb, to modify this file, you can restart the phone, it is not very cool, and quickly try it.

 

Android click, long press to obtain under the current contact coordinates (TextView) text characters


.... COM * * * utils Package;

Import android.graphics.Rect;
Import android.text.Layout;
Import android.widget.TextView;

public class TextViewUtils
{
/ **
Gets a character TextView a coordinate position
@return return the relative coordinates
@parms TV
@parms index character index
* /
public static Rect getTextViewSelectionRect (the TextView TV, int index) {
layout layout tv.getLayout = ();
Rect Rect new new bound = ();
int = layout.getLineForOffset Line ( index);
layout.getLineBounds (Line, bound);
int = yAxisBottom bound.bottom; // y coordinate of the bottom of the character
int yAxisTop = bound.top; // y coordinate of the top character
int xAxisLeft = (int) layout.getPrimaryHorizontal ( index) ; // characters left x-coordinate
int xAxisRight = (int) layout.getSecondaryHorizontal ( index); // x coordinate of the right character
// found equal to the character on the left x coordinate, please let us know reasons such as after obtaining xAxisRight position. Plus the character width temporarily to deal with.
IF (xAxisLeft == xAxisRight)
{
String tv.getText S = () toString () the substring (index, index +. 1);.. // current character
xAxisRight = xAxisRight + (int) tv.getPaint () measureText (s. // plus the character width;)
}
int tvTop tv.getScrollY = (); // TV absolute position
return new new Rect (xAxisLeft, yAxisTop + tvTop, xAxisRight, yAxisBottom tvTop +);

}

/ ** obtain the coordinates contacts TextView character
@param TV TV
@param x the x coordinate contact
@param y the y coordinate of the contact

@return current character
* /
public static String getTextViewSelectionByTouch (the TextView TV, int x, int y) {
String S = "";
for (int I = 0; I <tv.getText () length ();. I ++)
{
Rect = RECT getTextViewSelectionRect (TV, I);
IF (X <rect.right && X> Y rect.left && <RECT. Y && bottom> rect.top)
{
S = tv.getText () toString () the substring (I, I +. 1);.. // current character
BREAK;
}
}
return S;
}
}
in the click event acquired by the touch after the point coordinates calls TextViewUtils.getTextViewSelectionByTouch (tv, x, y) to get the current character.

Android automated testing to achieve long press and drag

Android Gets String parsing of Toast

Guess you like

Origin www.cnblogs.com/zgq123456/p/12587075.html