温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-08-21 04:20:36 来源:脚本之家 阅读:309 作者:zx 栏目:开发技术

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 driver

driver.find_element_by_xpath('//android.widget.EditText[@text="请输入用户名"]').send_keys('123456')

driver.find_element_by_xpath('//*[@class="android.widget.EditText" 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 driver
driver.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:\extend\kaoyanbang.apk'
# app的包名
desired_caps['appPackage']='com.tal.kaoyan'
# app 加载页面
desired_caps['appActivity']='com.tal.kaoyan.ui.activity.SplashActivity'
# 设置每次是否清除数据
desired_caps['noReset']='False'
# 是否使用unicode键盘输入,在输入中文字符和unicode字符时设置为true
desired_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 random
import time
driver.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自动化脚本内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI