APP自动化测试总结三:放大和缩小
1 import time 2 3 from appium.webdriver import Remote 4 from selenium.webdriver import ActionChains 5 from selenium.webdriver.common.actions.mouse_button import MouseButton 6 7 8 def zoom(driver: Remote, step=0.5, duration=None): 9 actions = ActionChains(driver) 10 actions.w3c_actions.devices = [] 11 # 初始化两个手指 12 finger1 = actions.w3c_actions.add_pointer_input('touch', f'finger1') 13 finger2 = actions.w3c_actions.add_pointer_input('touch', f'finger2') 14 15 # 屏幕的宽度 16 width = driver.get_window_size()['width'] 17 height = driver.get_window_size()['height'] 18 19 # 两个手指都移动到屏幕的中间 20 for finger in [finger1, finger2]: 21 finger.create_pointer_move(x=width*0.5, y=height*0.5) 22 finger.create_pointer_down(MouseButton.LEFT) 23 if duration: 24 finger.create_pause(duration / 1000) 25 else: 26 finger.create_pause(0.1) 27 28 # 两个手指往反方向移动 29 finger1.create_pointer_move(x=width*0.5, y=height*(0.5+step/2)) 30 finger2.create_pointer_move(x=width * 0.5, y=height * (0.5-step/2)) 31 32 # 松开 33 finger1.create_pointer_up(MouseButton.LEFT) 34 finger2.create_pointer_up(MouseButton.LEFT) 35 36 # 释放操作 37 actions.perform() 38 39 40 def pitch(driver: Remote, step=0.5, duration=None): 41 actions = ActionChains(driver) 42 actions.w3c_actions.devices = [] 43 finger1 = actions.w3c_actions.add_pointer_input('touch', f'finger1') 44 finger2 = actions.w3c_actions.add_pointer_input('touch', f'finger2') 45 46 width = driver.get_window_size()['width'] 47 height = driver.get_window_size()['height'] 48 49 finger1.create_pointer_move(x=width * 0.5, y=height*(0.5+step/2)) 50 finger2.create_pointer_move(x=width * 0.5, y=height * (0.5-step/2)) 51 52 for finger in [finger1, finger2]: 53 finger.create_pointer_down(MouseButton.LEFT) 54 if duration: 55 finger.create_pause(duration / 1000) 56 else: 57 finger.create_pause(0.1) 58 finger.create_pointer_move(x=width * 0.5, y=height *0.5) 59 finger.create_pointer_up(MouseButton.LEFT) 60 61 actions.perform() 62 63 64 65 caps = { 66 'platformName': 'Android', 67 'udid': 'emulator-5554', 68 'appPackage': 'com.android.gallery3d', 69 'appActivity': '.app.GalleryActivity' 70 } 71 72 driver = Remote(desired_capabilities=caps, 73 command_executor='http://127.0.0.1:4723/wd/hub') 74 driver.implicitly_wait(8) 75 driver.find_element('id', 'com.android.gallery3d:id/gl_root_view').click() 76 77 zoom(driver,step=0.5) 78 time.sleep(4) 79 driver.quit()