appium获取当前手机显示的宽和高
driver.get_window_size()['width']
driver.get_window_size()['height']
# coding=utf-8 # 模拟移动端首次安装手指滑动操作 from appium import webdriver def get_driver(): capabilities = { "platformName": "Android", "platformVersion": "6.0.1", # "deviceName": "bd619e047cf3", "deviceName": "127.0.0.1:7555", "app": "D:\Android\open_mooc_701_.apk", # "appPackage":新版Appium1.19.1无需手动配置 # "appActivity":新版Appium1.19.1无需手动配置 "appPackage":"cn.com.open.mooc", "appWaitActivity": "com.imooc.component.imoocmain.splash.GuideActivity" } driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", capabilities) return driver # driver.swipe(x,y,x1,y1) # driver.swipe(500,400,50,400) #水平从右向左滑动 # 获取屏幕像素的宽和高 # size = driver.get_window_size() # width = size['width'] # height = size['height'] # 获取屏幕像素宽和高,方法封装 def get_size(): # driver = get_driver() size = driver.get_window_size() width = size['width'] height = size['height'] return width, height # 手指向左滑动,方法封装 def swipe_left(): x = get_size()[0] / 10 * 9 y1 = get_size()[1] / 2 x1 = get_size()[0] / 10 # driver = get_driver() driver.swipe(x, y1, x1, y1) # 手指向右滑动,方法封装 def swipe_right(): x = get_size()[0] / 10 y1 = get_size()[1] / 2 x1 = get_size()[0] / 10 * 9 # driver = get_driver() driver.swipe(x, y1, x1, y1) # 手指向上滑动,方法封装 def swipe_up(): x = get_size()[0] / 2 y = get_size()[1] / 10 * 9 y1 = get_size()[1] / 10 x1 = get_size()[0] / 2 # driver = get_driver() driver.swipe(x, y, x1, y1) # 手指向下滑动,方法封装 def swipe_down(): x = get_size()[0] / 2 y = get_size()[1] / 10 y1 = get_size()[1] / 10 * 9 x1 = get_size()[0] / 2 # driver = get_driver() driver.swipe(x, y, x1, y1) # 进一步封装,滑动的方法 def swipe_on(direction): if direction == 'up': swipe_up() elif direction == 'down': swipe_down() elif direction == 'left': swipe_left() else: swipe_right() if __name__ == '__main__': driver = get_driver() #获取一个全局变量 swipe_on('left') swipe_on('left')
获取当前打开着的appActivity
adb shell dumpsys window|findstr mCurrentFocus
连接mumu模拟器
adb connect 127.0.0.1:7555
1-12 id定位进行登录操作
使用uiautomatorviewer.bat定位页面元素
D:Androidandroid-sdk oolsuiautomatorviewer.bat
在模拟新安装app从右向左滑动4次后,需要点击图片进入立即体验
if __name__ == '__main__': driver = get_driver() #获取一个全局变量driver swipe_on('left') time.sleep(2) swipe_on('left') time.sleep(2) swipe_on('left') time.sleep(2) swipe_on('left') time.sleep(2) #点击图片进入“立即体验” driver.find_element_by_class_name('android.widget.ImageView').click()
如果遇到强制升级更新,还需要注意验证点击升级操作
time.sleep(2) #调用升级按钮 driver.find_elements_by_id('cn.com.open.mooc:id/upgrade_layout').click()
#点击未知源的安装按钮 time.sleep(2) driver.find_elements_by_id('com.android.packageinstaller:id/ok_button').click()
base_update_mooc.py
首页——>账号
find_element_by_android_uiautomator('new UiSelector().index(4)')
# -*- coding:utf-8 -*- # Author:Marlon Kang # 模拟手机已经更新app后,进入后的登录操作 # 需要先手动点击,把初次进页面引导提示取消掉 import time from appium import webdriver #mCurrentFocus = "Window{3c696c4u0cn.com.open.mooc / com.imooc.component.imoocmain.index.MCMainActivity}" def get_driver(): capabilities = { "platformName": "Android", "platformVersion": "6.0.1", "deviceName": "127.0.0.1:7555", "appPackage":'cn.com.open.mooc', "appActivity": "com.imooc.component.imoocmain.index.MCMainActivity", "noReset": 'true', } driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", capabilities) return driver def login_by_node(): element1 = driver.find_element_by_id('cn.com.open.mooc:id/tab_layout') #通过层级关系和index定位页面元素 element2 = element1.find_element_by_android_uiautomator('new UiSelector().index(0)') element3 = element2.find_element_by_android_uiautomator('new UiSelector().index(4)') element3.click() #点击登录 login_by_password() return print("login_by_node()点击账号") def login_by_index(): element1 = driver.find_element_by_id('cn.com.open.mooc:id/tab_layout') #通过层级关系和index定位页面元素 element2 = element1.find_element_by_android_uiautomator('new UiSelector().index(0)') elements = element2.find_elements_by_class_name('androidx.appcompat.app.ActionBar$Tab') #账号的索引4 elements[4].click() # 点击登录 login_by_password() return print("login_by_index()点击账号") def login_by_password(): time.sleep(1) driver.find_element_by_id('cn.com.open.mooc:id/header_line').click() time.sleep(1) driver.find_element_by_id('cn.com.open.mooc:id/tvPassLogin').click() #填入信息 driver.find_element_by_id('cn.com.open.mooc:id/accountEditChannel2').send_keys('15652236641') time.sleep(1) driver.find_element_by_id('cn.com.open.mooc:id/passwordEditChannel2').send_keys('KYH0403a') #点击登录 time.sleep(1) driver.find_element_by_id('cn.com.open.mooc:id/login').click() def get_web_view(): time.sleep(8) webview = driver.contexts print(webview) if __name__ == '__main__': driver = get_driver() time.sleep(3) #切换到“账号”底边选项 login_by_node()
Appium在底层是调用【UiAutomator2】
.find_element_by_android_uiautomator('new UiSelector().index(0)')
.find_element_by_android_uiautomator('new UiSelector().text("abcd")')
.find_element_by_android_uiautomator('new UiSelector().resourceId(110111)')