android 仿苹果滑动选择器 模拟滑动app

您所在的位置:网站首页 苹果手机左右滑动切换页面怎么有照片 android 仿苹果滑动选择器 模拟滑动app

android 仿苹果滑动选择器 模拟滑动app

2023-06-28 10:58| 来源: 网络整理| 查看: 265

1-10 driver和滑动函数封装结合

手机app全新安装,首次进入app,一般会显示一个可以左右滑动的,展示新功能的宣传页面。

在安卓手机的设计,原点位于左上角处,水平方向x 垂直方向为y

android 仿苹果滑动选择器 模拟滑动app_android 仿苹果滑动选择器

appium获取当前手机显示的宽和高

driver.get_window_size()['width']driver.get_window_size()['height']

使用python编程控制模拟手指滑动的函数如下

# 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:\Android\android-sdk\tools\uiautomatorviewer.bat

android 仿苹果滑动选择器 模拟滑动app_Appium_02

在模拟新安装app从右向左滑动4次后,需要点击图片进入立即体验

android 仿苹果滑动选择器 模拟滑动app_Appium_03

 

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()

 如果遇到强制升级更新,还需要注意验证点击升级操作

android 仿苹果滑动选择器 模拟滑动app_封装_04

time.sleep(2) #调用升级按钮 driver.find_elements_by_id('cn.com.open.mooc:id/upgrade_layout').click()

 

android 仿苹果滑动选择器 模拟滑动app_android_05

#点击未知源的安装按钮 time.sleep(2) driver.find_elements_by_id('com.android.packageinstaller:id/ok_button').click()1-14 层级定位思想分析

在虚拟机下载并安装最新版本8的慕课网app

 base_update_mooc.py

首页——>账号

android 仿苹果滑动选择器 模拟滑动app_android 仿苹果滑动选择器_06

 find_element_by_android_uiautomator('new UiSelector().index(4)')

1-15 层级定位和list定位结合

可以通过find_elements_by_class_name方法返回一个元素的列表

# -*- 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('1565223xxxx') 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】

android 仿苹果滑动选择器 模拟滑动app_封装_07

.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)')

1-18 原生app和H5进行相互切换代码

android 仿苹果滑动选择器 模拟滑动app_ooc_08

 

android 仿苹果滑动选择器 模拟滑动app_Appium_09



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3