您的位置:首页技术文章
文章详情页

详解基于Android的Appium+Python自动化脚本编写

【字号: 日期:2022-07-13 14:36:04浏览:10作者:猪猪

1.Appium

Appium是一个开源测试自动化框架,可用于原生,混合和移动Web应用程序测试, 它使用WebDriver协议驱动iOS,Android和Windows应用程序。

通过Appium,我们可以模拟点击和屏幕的滑动,可以获取元素的id和classname,还可以根据操作生成相关的脚本代码。下面开始Appium的配置。

详解基于Android的Appium+Python自动化脚本编写

appPackage和APPActivity的获取

任意下载一个app解压

详解基于Android的Appium+Python自动化脚本编写

但是解压出来的xml文件可能是乱码,所以我们需要反编译文件。逆向AndroidManifest.xml下载AXMLPrinter2.jar文件,逆向xml文件:命令行输入以下命令:java -jar AXMLPrinter2.jar AndroidManifest.xml ->AndroidManifest.txt获得以下可以查看的TXT文件

详解基于Android的Appium+Python自动化脚本编写

寻找带有launcher 的Activity

详解基于Android的Appium+Python自动化脚本编写

寻找manifest里面的package

详解基于Android的Appium+Python自动化脚本编写

Devicename的获取

通过命令行输入 adb devices:

详解基于Android的Appium+Python自动化脚本编写

appium的功能介绍

详解基于Android的Appium+Python自动化脚本编写

详解基于Android的Appium+Python自动化脚本编写

下面将根据上图序号一一介绍功能:

选中界面元素,显示元素相关信息

详解基于Android的Appium+Python自动化脚本编写

模拟滑动屏幕,先点击一下代表触摸起始位置,在点击一下代表触摸结束为止

模拟点击屏幕

模拟手机的返回按钮

刷新左边的页面,使之与手机同步

记录模拟操作,生成相关脚本

详解基于Android的Appium+Python自动化脚本编写

根据元素的id或者其他相关信息查找元素

详解基于Android的Appium+Python自动化脚本编写

复制当前界面的xml布局

文件退出

2.Python的脚本

元素定位的使用

(1).xpath定位

xpath定位是一种路径定位方式,主要是依赖于元素绝对路径或者相关属性来定位,但是绝对路径xpath执行效率比较低(特别是元素路径比较深的时候),一般使用比较少。通常使用xpath相对路径和属性定位。by_xpath.py

from find_element.capability import driverdriver.find_element_by_xpath(’//android.widget.EditText[@text='请输入用户名']’).send_keys(’123456’)driver.find_element_by_xpath(’//*[@ and @index='3']’).send_keys(’123456’)driver.find_element_by_xpath(’//android.widget.Button’).click()driver.find_element_by_xpath(’//[@class='android.widget.Button']’).click()

(2).classname定位

classname定位是根据元素类型来进行定位,但是实际情况中很多元素的classname都是相同的,如用户名和密码都是clasName属性值都是:“android.widget.EditText” 因此只能定位第一个元素也就是用户名,而密码输入框就需要使用其他方式来定位,这样其实很鸡肋.一般情况下如果有id就不必使用classname定位。by_classname.py

from find_element.capability import driverdriver.find_element_by_class_name(’android.widget.EditText’).send_keys(’123565’)driver.find_element_by_class_name(’android.widget.EditText’).send_keys(’456879’)driver.find_element_by_class_name(’android.widget.Button’).click()

(3).id定位

日常生活中身边可能存在相同名字的人,但是每个人的身份证号码是唯一的,在app界面元素中也可以使用id值来区分不同的元素,然后进行定位操作。Appium中可以使用 find_element_by_id() 方法来进行id定位。

driver.find_element_by_id(’android:id/button2’).click()driver.find_element_by_id(’com.tal.kaoyan:id/tv_skip’).click()

3.示例:模拟软件的自动注册

首先配置连接属性

desired_caps={}# 所使用的平台desired_caps[’platformName’]=’Android’# 所使用的手机的名字 可以通过 adb devices 获得desired_caps[’deviceName’]=’127.0.0.1:62001’# ANDROID 的版本desired_caps[’platforVersion’]=’5.1.1’# app 的路径desired_caps[’app’]=r’D:extendkaoyanbang.apk’# app的包名desired_caps[’appPackage’]=’com.tal.kaoyan’# app 加载页面desired_caps[’appActivity’]=’com.tal.kaoyan.ui.activity.SplashActivity’# 设置每次是否清除数据desired_caps[’noReset’]=’False’# 是否使用unicode键盘输入,在输入中文字符和unicode字符时设置为truedesired_caps[’unicodeKeyboard’]='True'# 是否将键盘重置为初始状态,设置了unicodeKeyboard时,在测试完成后,设置为true,将键盘重置desired_caps[’resetKeyboard’]='True'# appium服务器的连接地址driver=webdriver.Remote(’http://localhost:4723/wd/hub’,desired_caps)driver.implicitly_wait(2)

编写操作脚本

import randomimport timedriver.find_element_by_id(’com.tal.kaoyan:id/login_register_text’).click()username=’zx2019’+’F2LY’+str(random.randint(1000,9000))print(’username: %s’ %username)driver.find_element_by_id(’com.tal.kaoyan:id/activity_register_username_edittext’).send_keys(username)password=’zxw2018’+str(random.randint(1000,9000))print(’password: %s’ %password)driver.find_element_by_id(’com.tal.kaoyan:id/activity_register_password_edittext’).send_keys(password)email=’51zxw’+str(random.randint(1000,9000))+’@163.com’print(’email: %s’ %email)driver.find_element_by_id(’com.tal.kaoyan:id/activity_register_email_edittext’).send_keys(email)#点击进入考研帮driver.find_element_by_id(’com.tal.kaoyan:id/activity_register_register_btn’).click()#专业选择driver.find_element_by_id(’com.tal.kaoyan:id/activity_perfectinfomation_major’).click()driver.find_elements_by_id(’com.tal.kaoyan:id/major_subject_title’)[1].click()driver.find_elements_by_id(’com.tal.kaoyan:id/major_group_title’)[2].click()driver.find_elements_by_id(’com.tal.kaoyan:id/major_search_item_name’)[1].click()#院校选择driver.find_element_by_id(’com.tal.kaoyan:id/activity_perfectinfomation_school’).click()driver.tap([(182,1557),])driver.find_element_by_xpath(’/hierarchy/android.widget.FrameLayout/’ ’android.widget.LinearLayout/android.widget.FrameLayout/’ ’android.widget.LinearLayout/android.widget.FrameLayout/android.widget.’ ’RelativeLayout/android.widget.ExpandableListView/android.widget.’ ’LinearLayout[1]/android.widget.TextView[1]’).click()driver.find_element_by_xpath(’/hierarchy/android.widget.FrameLayout/’ ’android.widget.LinearLayout/android.widget.FrameLayout/’ ’android.widget.LinearLayout/android.widget.FrameLayout/’ ’android.widget.RelativeLayout/android.widget.ExpandableListView/’ ’android.widget.LinearLayout[4]/android.widget.TextView’).click()time.sleep(2)driver.tap([(983,1354),])# driver.find_elements_by_id(’com.tal.kaoyan:id/more_forum_title’)[1].click()# driver.find_elements_by_id(’com.tal.kaoyan:id/university_search_item_name’)[1].click()driver.find_element_by_id(’com.tal.kaoyan:id/activity_perfectinfomation_goBtn’).click()print(’注册成功’)

到此这篇关于详解基于Android的Appium+Python自动化脚本编写的文章就介绍到这了,更多相关Android的Appium+Python自动化脚本内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!

标签: Python 编程
相关文章: