Topics of this chapter
Basic equipment operation
Shake it
Perform shaking action on the device
import time from appium import webdriver from appium.webdriver.webdriver import By from appium.webdriver.connectiontype import ConnectionType des = { "platformName": "Android", "platformVersion": "9", "deviceName": "mac virtual machine", "appPackage": "com.wondertek.paper", "appActivity": "cn.thepaper.paper.ui.main.MainActivity", "udid": "192.168.56.104:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) # shake up self.driver.shake();
Lock screen
Lock screen operation. seconds sets the lock screen duration
# Lock screen 3s = = "cross event" driver.lock(3) print(driver.is_locked()) # Judge whether to lock the screen. If yes, return false
Unlock
Unlock operation
import time from appium import webdriver from appium.webdriver.webdriver import By from appium.webdriver.connectiontype import ConnectionType des = { "platformName": "Android", "platformVersion": "9", "deviceName": "mac virtual machine", "appPackage": "com.wondertek.paper", "appActivity": "cn.thepaper.paper.ui.main.MainActivity", "udid": "192.168.56.104:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) time.sleep(2) # Lock screen 3s = = "cross event" driver.lock(3) print(driver.is_locked()) # Judge whether to lock the screen. If yes, return false # Unlock driver.unlock() time.sleep(3) print(driver.is_locked())
- Combining small tips
import time from appium import webdriver from appium.webdriver.webdriver import By from appium.webdriver.connectiontype import ConnectionType des = { "platformName": "Android", "platformVersion": "9", "deviceName": "mac virtual machine", "appPackage": "com.wondertek.paper", "appActivity": "cn.thepaper.paper.ui.main.MainActivity", "udid": "192.168.56.104:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) time.sleep(2) # Lock screen 3s = = "cross event" driver.lock(3) print(driver.is_locked()) # Judge whether to lock the screen. If yes, it returns false # Unlock driver.unlock() time.sleep(3) print(driver.is_locked())
Judge whether to lock the screen
Judge whether to lock the screen, and the lock screen status returns True
import time from appium import webdriver from appium.webdriver.webdriver import By from appium.webdriver.connectiontype import ConnectionType des = { "platformName": "Android", "platformVersion": "9", "deviceName": "mac virtual machine", "appPackage": "com.wondertek.paper", "appActivity": "cn.thepaper.paper.ui.main.MainActivity", "udid": "192.168.56.104:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) time.sleep(2) driver.lock(3) print(driver.is_locked()) # Judge whether to lock the screen. If yes, return false
View screen status
driver.orientation = "LANDSCAPE": switch the horizontal screen operation
driver.orientation = 'transport': switch vertical screen operation
You can use driver Orientation gets the current screen status
import time from appium import webdriver from appium.webdriver.webdriver import By from appium.webdriver.connectiontype import ConnectionType des = { "platformName": "Android", "platformVersion": "9", "deviceName": "mac virtual machine", "appPackage": "com.wondertek.paper", "appActivity": "cn.thepaper.paper.ui.main.MainActivity", "udid": "192.168.56.104:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) # Horizontal and vertical screen settings driver.orientation = "LANDSCAPE" # Set horizontal screen time.sleep(3) driver.orientation = "PORTRAIT" # Set vertical screen
Open notification bar
Open notification bar
import time from appium import webdriver from appium.webdriver.webdriver import By from appium.webdriver.connectiontype import ConnectionType des = { "platformName": "Android", "platformVersion": "9", "deviceName": "mac virtual machine", "appPackage": "com.wondertek.paper", "appActivity": "cn.thepaper.paper.ui.main.MainActivity", "udid": "192.168.56.104:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) # Open notification bar driver.open_notifications() print(driver.orientation)
Get network status
network_connection: get the network status and return an integer number
0(None),1(AirplaneMode),2(Wifionly),4(Dataonly),6(Allnetworkon)
network_connection: Get the network status and return the integer number 0(None),1(AirplaneMode),2(Wifionly),4(Dataonly),6(Allnetworkon) # network configuration print(driver.network_connection) # View current network status driver.set_network_connection(1) # Flight mode 0 1 2 4 6 time.sleep(5) # set_network_connection(self,connection_type): set the network status, and set the parameters by using numbers or importing the ConnectionType class driver.set_network_connection(ConnectionType.ALL_NETWORK_ON) # Fully open
get SysTime
Get time on device
from appium import webdriver from appium.webdriver.webdriver import By from appium.webdriver.connectiontype import ConnectionType des = { "platformName": "Android", "platformVersion": "9", "deviceName": "mac virtual machine", "appPackage": "com.wondertek.paper", "appActivity": "cn.thepaper.paper.ui.main.MainActivity", "udid": "192.168.56.104:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) time = self.driver.device_time time = self.driver.get_device_time() time = self.driver.get_device_time("YYYY-MM-DD")
Get screen resolution
get_ window_ Size (self, windowshandle = 'current'): obtain the resolution of the current screen by default, and return the dictionary {width ':,' height ':}
from appium import webdriver from appium.webdriver.webdriver import By from appium.webdriver.connectiontype import ConnectionType des = { "platformName": "Android", "platformVersion": "9", "deviceName": "mac virtual machine", "appPackage": "com.wondertek.paper", "appActivity": "cn.thepaper.paper.ui.main.MainActivity", "udid": "192.168.56.104:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) window = driver.get_window_size() print(window)
Save Screenshot
save_screenshot(self,filename): screen capture operation. Filename is the saved file path, and the image format is set to png format
from appium import webdriver from appium.webdriver.webdriver import By from appium.webdriver.connectiontype import ConnectionType des = { "platformName": "Android", "platformVersion": "9", "deviceName": "mac virtual machine", "appPackage": "com.wondertek.paper", "appActivity": "cn.thepaper.paper.ui.main.MainActivity", "udid": "192.168.56.104:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) driver.save_screenshot('./pengpai.png') # Incoming storage path
Basic operation of key class
Key operations are used to simulate key operations on mobile devices
Analog key input
press_keycode(self,keycode,metastate=None,flags=None): simulate key input
Of which:
Keycode: the key value code sent to the device. You can query the corresponding value through Android keycode
metastate: meta information to be sent
flags: set the key event flag
import time from appium import webdriver from appium.webdriver.webdriver import By from appium.webdriver.connectiontype import ConnectionType des = { "platformName": "Android", "platformVersion": "9", "deviceName": "mac virtual machine", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.56.104:5555", "noReset": True } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) time.sleep(2) driver.find_element(By.XPATH, '//android.widget.TextView[@text = "search in settings"]) click() time.sleep(1) driver.press_keycode(29, 64, 59) # A turn on the switch of the left shift key and press the left shift key = = a combination key time.sleep(1) driver.press_keycode(29, 128, 60) # A turn on the right shift key switch and press the right shift key = = a time.sleep(1) driver.press_keycode(29, 1048576) # a turn on the case switch time.sleep(1) driver.press_keycode(29, 1) # 1 open the shift key
Analog key input 2
keyevent(self,keycode,metastate=None): simulate key input
Of which:
Keycode: the key value code sent to the device. You can query the corresponding value through Android KeyCode
metastate: the meta information to be sent, the method and press_ The keycode () method is similar
Press is recommended_ keycode
import time from appium import webdriver from appium.webdriver.webdriver import By from appium.webdriver.connectiontype import ConnectionType des = { "platformName": "Android", "platformVersion": "9", "deviceName": "mac virtual machine", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.56.104:5555", "noReset": True } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) time.sleep(2) driver.keyevent(29) #Enter a driver.keyevent(30) #Enter b
Simulate long press event
long_press_keycode(self,keycode,metastate=None,flags=None): simulate long press key operation
Parameters are not described and have the same meaning as before. It is generally used to simulate long pressing a button on the mobile phone, such as long pressing the power key and long pressing the home key
import time from appium import webdriver from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.webdriver import By des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos virtual machine", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) driver.implicitly_wait(10) touch.long_press_keycode(10)
View current keyboard status
Show soft keyboard
If displayed, return to true
import time from appium import webdriver from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.webdriver import By des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos virtual machine", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) driver.implicitly_wait(10) # Determine whether the keyboard is displayed driver.is_keyboard_shown()
Hide keyboard
Hide soft keyboard
import time from appium import webdriver from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.webdriver import By des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos virtual machine", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) driver.implicitly_wait(10) # I disappeared... self.driver.hide_keyboard()
Click / slide operation
During app automation, click or slide operations are often performed, such as clicking coordinates, sliding left and right, sliding up and down, etc. Appium provides solutions accordingly
Tap the device
Tap the screen to enable the device
- demo1
import time from appium import webdriver from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.webdriver import By des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos virtual machine", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) actions = TouchAction(driver) actions.tap(element) actions.perform()
- demo2
import time from appium import webdriver from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.webdriver import By des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos virtual machine", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) driver.implicitly_wait(10) actions = TouchAction(driver) actions.tap(x=720, y=1910).perform() # Click to specify the coordinates
- Illustration
Simulate sliding operation
scroll(self,origin_el,destination_el,duration=None): simulate from element origin_ El scroll to the element destination_ el
Move from element1 to element2
# -*- coding: utf-8 -*- # @Time : 2022/1/17 9:33 # @Author : Limusen # @File : demo_look_11 import time from appium import webdriver from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.webdriver import By des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos virtual machine", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) web_element_e1 = driver.find_element(By.XPATH, '//android.widget.TextView[@text = "account"] ') web_element_e2 = driver.find_element(By.XPATH, '//android.widget.TextView[@text = "store"] ') #Move from e1 to e2 driver.scroll(web_element_e1,web_element_e2)
- Illustration
Drag and drop elements
drag_and_drop(self,origin_el,destination_el): set the element origin_ El drag to the destination element destination_ el
Drag element1 to element2
# -*- coding: utf-8 -*- # @Time : 2022/1/17 9:33 # @Author : Limusen # @File : demo_look_11 import time from appium import webdriver from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.webdriver import By des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos virtual machine", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) web_element_e1 = driver.find_element(By.XPATH, '//android.widget.TextView[@text = "account"] ') web_element_e2 = driver.find_element(By.XPATH, '//android.widget.TextView[@text = "store"] ') driver.drag_and_drop(web_element_e1,web_element_e2)
- Illustration
Slide flick
flick(self,start_x,start_y,end_x,end_y): press and hold point A and quickly slide to point B. the coordinate of point A is start_x,start_y. The coordinate of point B is end_x,end_y
# -*- coding: utf-8 -*- # @Time : 2022/1/17 9:33 # @Author : Limusen # @File : demo_look_11 import time from appium import webdriver from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.webdriver import By des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos virtual machine", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) # x coordinate 720 and Y coordinate 1910 of element a move to element b, x coordinate 720 and Y coordinate 1000 driver.flick(720, 1910, 720, 1000)
- Illustration
Sliding swipe
Swing (self, start_x,start_y, end_x,end_y, duration = none): slide from point A to point B, the sliding time is milliseconds, and the coordinate of point A is start_x,start_y. The coordinate of point B is end_x,end_y
# -*- coding: utf-8 -*- # @Time : 2022/1/17 9:33 # @Author : Limusen # @File : demo_look_11 import time from appium import webdriver from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.webdriver import By des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos virtual machine", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) driver.swipe(720, 1910, 860, 1000,3000)
- Illustration
TouchAction helper class
TouchAction operation explanation: Appium's auxiliary class is mainly used for gesture operations, such as sliding, long pressing, dragging, etc. The principle is to put a series of actions in a chain, and then transfer the chain to the server. After receiving the chain, the server parses each action and executes it one by one.
The TouchAction class supports many actions. The common APIs are as follows:
1. press(self,el=None,x=None,y=None): press an element or coordinate, le is the element to be clicked, and x/y is the coordinate
2,long_press(self,el=None,x=None,y=None,duration=1000): press an element or coordinate for a long time. The default long press time is 1000ms
3. tap(self,element=None,x=None,y=None,count=1): click on an element or control
4,move_to(self,el=None,x=None,y=None): moves the pointer from the previous point to the specified element or point
5. wait(self,ms=0): wait time, in milliseconds
6. release(self): release, which ends the command operation of a series of actions on the screen
7. perform(self): execute the command that sends the operation to be executed to the server
TouchAction – press
import time from appium import webdriver from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.webdriver import By des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos virtual machine", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) driver.implicitly_wait(10) touch = TouchAction(driver) # You need to define a TouchAction object first touch.press(x=720, y=1910).perform()
- Illustration
TouchAction – long_press
import time from appium import webdriver from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.webdriver import By des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos virtual machine", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) driver.implicitly_wait(10) touch = TouchAction(driver) #touch.press(x=720, y=1910).perform() touch.long_press(x=720, y=1910).perform()
- Illustration
TouchAction – tap the specified element
import time from appium import webdriver from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.webdriver import By des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos virtual machine", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) driver.implicitly_wait(10) touch = TouchAction(driver) touch.tap(x=720, y=1910).perform() # Click to specify the coordinates
- Illustration
TouchAction – move_to
import time from appium import webdriver from appium.webdriver.webdriver import By from appium.webdriver.common.touch_action import TouchAction des = { "platformName": "Android", "platformVersion": "9", "deviceName": "windwos virtual machine", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.0.101:5555", "noReset": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) driver.implicitly_wait(10) element_01 = driver.find_element(By.XPATH, '//android.widget.TextView[@text = "screen lock"] ') TouchAction(driver).press(element_01).release().perform() time.sleep(2) element_02 = driver.find_element(By.XPATH, '//android.widget.TextView[@text = "screen lock"] ') TouchAction(driver).press(element_02).release().perform() time.sleep(2) element_03 = driver.find_element(By.XPATH, '//android.widget.TextView[@text = "pattern"] ') TouchAction(driver).press(element_03).release().perform() """ 339 1287 721 1287 721 1662 721 2043 339 1665 1101 1667 """ touch_action = TouchAction(driver) touch_action.press(x=339, y=1297).wait(1000).move_to(x=721, y=1287).wait(1000).move_to(x=721, y=1662).wait( 1000).move_to(x=721, y=2043).wait(1000).move_to(x=339, y=1665).wait(1000).move_to(x=1101, y=1667).wait( 1000).release().perform()
- Illustration
summary
-
Summary of this chapter
This chapter mainly describes how to perform some common operations on the equipment, such as keyboard, click, touch, etc., so as to facilitate our subsequent operation practice... If it is helpful to you, you may wish to praise it. Thank you
Code address
https://gitee.com/todayisgoodday/PythonAppnium
Blog Park Address
https://www.cnblogs.com/yushengaqingzhijiao/category/2024559.html