Basic Automation Operations¶
This chapter describes the basic automation functions provided by FIRERPA. Of course, there are more advanced uses described later. This chapter introduces some commonly used basic function interfaces.
Get Device Information¶
Get current device information such as device name, screen size, screen orientation, and current application.
d.device_info()
>>> d.device_info()
productName: "bumblebee"
sdkInt: 34
displayHeight: 2400
displaySizeDpX: 411
displaySizeDpY: 914
displayWidth: 1080
screenOn: true
naturalOrientation: true
currentPackageName: "com.android.launcher3"
>>> result = d.device_info()
>>> print (result.displayWidth)
1080
Get Service Information¶
Retrieve information such as the current service version, device unique ID, version, ABI, etc.
d.server_info()
>>> d.server_info( )
uniqueId: "673abbe0-ff7b-9d82-1792-8876cb72cf56"
version: "8.28"
architecture: "arm64-v8a"
uptime: 293
secure: True
>>> result = d.server_info()
>>> print (result.secure)
False
Turn Off Screen¶
The following interface will turn off the current phone screen, equivalent to pressing the power button once when the screen is on.
d.sleep()
Turn On Screen¶
The following interface will turn on the current phone screen, equivalent to pressing the power button once when the screen is off.
d.wake_up()
Check if Screen is On¶
You can check whether the current device’s screen is on through the following interface, to determine if operations can be performed.
d.is_screen_on()
Check if Screen is Locked¶
You can check whether the current device’s screen is unlocked through the following interface, to determine if operations can be performed.
d.is_screen_locked()
Show Toast¶
You can use the following interface to display a Toast message of Hello from Lamda!
on the phone’s interface.
d.show_toast("Hello from Lamda!")
Read Clipboard¶
The following interface is used to read the content of the phone’s current clipboard. Currently does not support Android 10+.
d.get_clipboard()
Write to Clipboard¶
The following interface is used to write content to the current device’s clipboard.
d.set_clipboard("Clipboard content")
Physical Keys¶
You can use the methods below to perform simulated keypress operations, supporting more than ten keys such as KEY_BACK, KEY_DOWN, KEY_HOME, etc.
d.press_key(Keys.KEY_BACK)
d.press_key(Keys.KEY_CAMERA)
d.press_key(Keys.KEY_CENTER)
d.press_key(Keys.KEY_DELETE)
d.press_key(Keys.KEY_DOWN)
d.press_key(Keys.KEY_ENTER)
d.press_key(Keys.KEY_HOME)
d.press_key(Keys.KEY_LEFT)
d.press_key(Keys.KEY_MENU)
d.press_key(Keys.KEY_POWER)
d.press_key(Keys.KEY_RECENT)
d.press_key(Keys.KEY_RIGHT)
d.press_key(Keys.KEY_SEARCH)
d.press_key(Keys.KEY_UP)
d.press_key(Keys.KEY_VOLUME_DOWN)
d.press_key(Keys.KEY_VOLUME_MUTE)
d.press_key(Keys.KEY_VOLUME_UP)
To support more keys, you can also use this method to simulate them. You can find all supported key names in the Android official documentation https://developer.android.com/reference/android/view/KeyEvent.
d.press_keycode(KeyCodes.KEYCODE_CALL)
Screen Screenshot¶
We provide a screen screenshot method so you can record processes or perform some image recognition operations. The following call is explained as: take an image with clarity of 60, and save it in the current directory as the screenshot.png file.
d.screenshot(60).save("screenshot.png")
>>> result = d.screenshot(60)
>>> result
<lamda.types.BytesIO object at 0x7a28361d60>
>>> result.save("screenshot.png")
52917
>>> result.getvalue( )
b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01....'
Currently, we also support regional and element screenshots. Element screenshots are not introduced in this chapter. Here we introduce regional screenshots. You need to understand the concept and definition of a region. In our interface, a region is defined by Bound
, which includes four parameters: top, left, right, and bottom. The Android screen is different from the conventional coordinate system. The top-left corner of the current device screen is the origin, with coordinates x=0, y=0. Let’s assume the current screen is in portrait mode with a resolution of 1080x1920. If you want to take a full-screen regional screenshot, you can do it like this:
d.screenshot(60, bound=Bound(top=0, left=0, right=1080, bottom=1920)).save("screenshot.png")
This will produce a full-screen screenshot, which is unnecessary because the default is already a full-screen screenshot. So if we want to take a 200x200 image sticking to the top-left corner, how do we do it? It’s also simple, you just need to do it like this:
d.screenshot(60, bound=Bound(top=0, left=0, right=200, bottom=200)).save("screenshot.png")
At this point, we should introduce what these parameters mean. In the parameters, top represents the number of pixels from the Y-axis down, bottom represents the number of pixels from the Y-axis down, left and right represent the number of pixels from the X-axis to the right. Among them, top is always less than bottom, and left is always less than right. Let’s demonstrate another one, taking a 200x200 image from the bottom-right corner of the screen. You just need to call the interface like this:
d.screenshot(60, bound=Bound(top=1920-200, bottom=1920, left=1080-200, right=1080)).save("screenshot.png")
Click on a Point on the Screen¶
You can simulate clicking on the screen through the following interface. In the example, we clicked on the point with coordinates 100,100 on the screen.
d.click(Point(x=100, y=100))
Press Point A and Drag it to Point B¶
The following interface can be used to drag something from position A to position B, such as dragging an icon to a folder.
A = Point(x=100, y=100)
B = Point(x=500, y=500)
d.drag(A, B)
Swipe from Point A to Point B¶
The following interface can be used to swipe from coordinates 100x100 to 500x500 on the screen.
A = Point(x=100, y=100)
B = Point(x=500, y=500)
d.swipe(A, B)
Slightly More Complex Multi-point Swipe¶
The following interface can simulate multi-point swipes. For example, the following call will swipe from 100x100 to 500x500 and back to 200x200. The demonstration below only swipes through three points, but in reality, you can provide more points, even implementing a nine-grid unlock.
p1 = Point(x=100, y=100)
p2 = Point(x=500, y=500)
p3 = Point(x=200, y=200)
d.swipe_points(p1, p2, p3)
Open Quick Settings¶
The following call can open the notification bar on the screen, but in a half-open state.
d.open_quick_settings()
Open Notification Bar¶
The following call can open the notification bar on the screen.
d.open_notification()
Get Page Layout¶
The following call can get the XML layout of the current page. You can also parse it yourself to support xpath automation.
d.dump_window_hierarchy()
>>> result = d.dump_window_hierarchy()
>>> result
<lamda.types.BytesIO object at 0x7add660680>
>>> result.getvalue()
b'<?xml version=\'1.0\' encoding=\'UTF-8\' standalone=\'yes\' ?>\r\n<hierarchy rotation="0">\r\n <node index="0" text="" resource-id="" class="android.widget.FrameLayout" package="com.android.systemui" content-desc="" checkable="false" checked="false" clickable="false"...
Wait for Window to Stabilize¶
You can execute the following call to wait for the current window to be in a stable state, similar to selenium’s implicitly_wait. The parameter unit is milliseconds, 5*1000 means waiting for 5 seconds, and the unit conversion from milliseconds to seconds is 1000.
d.wait_for_idle(5*1000)
Get Latest Toast¶
You can execute the following call to get the latest Toast message in the system. However, note that it only supports Toast messages sent by the system’s internal API. Third-party implemented Toast-like UI messages cannot be obtained.
d.get_last_toast()
>>> result = d.get_last_toast()
>>> print (result)
timestamp: 1700000000000
package: "com.android.settings"
message: "\346\202\250\345\267\262\345\244\204\344\272\216\345\274\200\345\217\221\350\200\205\346\250\241\345\274\217\357\274\214\346\227\240\351\234\200\350\277\233\350\241\214\346\255\244\346\223\215\344\275\234\343\200\202"
>>> print (result.message)
您已处于开发者模式,无需进行此操作。