老板叫我写个APP自动化--登录脚本--简单输出

简介: 老板叫我写个APP自动化--登录脚本--简单输出

前言

这里是清安,前面我们准备阶段基本上已经搞定了,环境也是OK了,那么接下来我们就开始正式阶段了。本章就来讲讲元素定位以及简单的登录脚本输出。

「软件嘛,各位可以自行随便下载一个试试问题不大」

元素定位

讲到元素定位,我们也不废话多说,先看一波官网:看到这里,嗯~全英文的,看不懂哎。问题不大,我们一起来看看几个比较常用的即可。例如XPATH,ID,Accessibility ID,CLASS NAME。

初始化

标题不重要,主要是要使用代码连接模拟器,跟Appium Inspector里面类似。

# ----清安—---
# 微信:qing_an_an
# 公众号:测个der
from appium import webdriver
desired_capas = {
    "deviceName": "emulator-5554",
    "platformName": "Android",
    "appPackage": "com.mxchip.project352",
    "appActivity": "com.mxchip.project352.activity.login.LoginActivity",
    "platformVersion": "7.1.2",
    "noReset": "True"
}
ap = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_capas)
print(ap.context)

首先我们要引入appium的包,其次就是配置参数信息了。desired_capas里面的内容是不是看着很眼熟,就是Appium Inspector所写的一些参数配置。ap是一个变量,随意自己命名。注意命名规范。webdriver.Remote()可以理解为实例化,并且传入连接的地址这些参数都可以在Appium Inspector里面看到。最后我们再次传入配置参数desired_capas,告诉appium我们需要通信的是个什么玩意。由他来帮我们完成接下来的一些列的操作。而我们只要通过ap这个变量调用方法,就可以完成点击等一些操作了。

Accessibility ID

看官网意思:Read a unique identifier for a UI element. For XCUITest it is the element's accessibility-id attribute. For Android it is the element's content-desc attribute.

大概就是说,根据content-desc这个属性来,那么这个属性在哪呢?真不凑巧,这里没有唉,不过没关系,后面会介绍另一个工具uiautomatorviewer,这是安卓自带的一个元素定位工具。这里面会有介绍。先看个截图了解一下:

看图说话,这个够明显了吧,摆明了要你去使用它。所以咱们就安排了他。

# ----清安—---
# 微信:qing_an_an
# 公众号:测个der
from appium import webdriver
desired_capas = {
    "deviceName": "emulator-5554",
    "platformName": "Android",
    "appPackage": "com.mxchip.project352",
    "appActivity": "com.mxchip.project352.activity.login.LoginActivity",
    "platformVersion": "7.1.2",
    "noReset": "True"
}
ap = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_capas)
ap.find_element_by_id('com.mxchip.project352:id/etPhone').send_keys('qing_an_an')

这里我们直接用find_element_by_id,使用过程中你可能会看到这中间花了一条横线。问题都不大,意思就是下个版本抛弃而已。

CLASS NAME

CLASS NAME还是少用的少,因为这个属性在app中极大的概率会出现重复的。看到了吧,所以咱们还是谨慎使用。那么出现重复的咋办?那么我们就用元素组定位。

# ----清安—---
# 微信:qing_an_an
# 公众号:测个der
from appium import webdriver
desired_capas = {
    "deviceName": "emulator-5554",
    "platformName": "Android",
    "appPackage": "com.mxchip.project352",
    "appActivity": "com.mxchip.project352.activity.login.LoginActivity",
    "platformVersion": "7.1.2",
    "noReset": "True"
}
ap = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_capas)
ap.find_element_by_id('com.mxchip.project352:id/etPhone').send_keys('qing_an_an')
ap.find_elements_by_class_name('android.widget.EditText')[1].send_keys('jx123456')

元素组定位,除了elements与element的区别外,元素组还需要添加下标来准确定位,例如上述所说,有两个重复的值,那么我们下标就从0开始,我们需要定位到输入密码框,它处于第二位,那么下标值就为1,以此类推。所以写法就是:find_elements_by_class_name('android.widget.EditText')[1]

「此外元素组不只CLASS NAME这一种定位方式,XPATH/ID等都要,用法类似」别看这里展示这么多,咱们用不到。

XPATH

至于XPTAH这个定位方式,有点特殊,我们可以手写,也能直接COPY。看这里就有现成的,但是,现成的太多了,博主本人是不打算复制,所以我选择手写。这里我选择的是text属性定位,也就是俗称文本定位。当然,这里的属性你都可以拿来写XPATH。点击search就能看到有几个元素了看看代码怎么写:

# ----清安—---
# 微信:qing_an_an
# 公众号:测个der
from appium import webdriver
desired_capas = {
    "deviceName": "emulator-5554",
    "platformName": "Android",
    "appPackage": "com.mxchip.project352",
    "appActivity": "com.mxchip.project352.activity.login.LoginActivity",
    "platformVersion": "7.1.2",
    "noReset": "True"
}
ap = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_capas)
ap.find_element_by_id('com.mxchip.project352:id/etPhone').send_keys('qing_an_an')
ap.find_elements_by_class_name('android.widget.EditText')[1].send_keys('jx123456')
ap.find_element_by_xpath('//*[@text="登录"]').click()

「好了,基本的就是这些了,一起来看看一个完整且简单的APP登录 脚本是怎么样的吧」

# ----清安—---
from appium import webdriver
from time import sleep
desired_capas = {
    "deviceName": "emulator-5554",
    "platformName": "Android",
    "appPackage": "com.mxchip.project352",
    "appActivity": "com.mxchip.project352.activity.login.LoginActivity",
    "platformVersion": "7.1.2",
    "noReset": "True"
}
ap = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_capas)
ap.find_element_by_id('com.mxchip.project352:id/etPhone').send_keys('qing_an_an')
ap.find_elements_by_class_name('android.widget.EditText')[1].send_keys('jx123456')
ap.find_element_by_id('com.mxchip.project352:id/cbAgree').click()
sleep(1)
ap.find_element_by_xpath('//*[@text="登录"]').click()

此处加了sleep,等待时间,防止定位程序运行起来点击过快可能会报错而添加的,后期用了其他的等待时间函数,这里就可以不用了。当然,你也可以直接不添加。

好了,本章就到了这里,下一章我们讲讲将selenium的定位方式引入进来,以及另一个类似XPTAH的定位方法。

目录
相关文章
|
10天前
|
Linux Shell Perl
自动化脚本之Debian 开机时运行通过expect自动执行串口命令
自动化脚本之Debian 开机时运行通过expect自动执行串口命令
15 0
|
1月前
|
测试技术 UED Python
App自动化测试:高级控件交互技巧
Appium 的 Actions 类支持在移动应用自动化测试中模拟用户手势,如滑动、长按等,增强交互性测试。ActionChains 是 Selenium 的概念,用于网页交互,而 Actions 专注于移动端。在Python中,通过ActionChains和W3C Actions可以定义手势路径,例如在手势解锁场景中,先点击设置,然后定义触点移动路径执行滑动解锁,最后验证解锁后的元素状态。此功能对于确保应用在复杂交互下的稳定性至关重要。
29 5
|
2月前
|
jenkins 持续交付
Jenkins自动化部署脚本
Jenkins自动化部署脚本
33 0
|
10天前
|
安全 Linux Android开发
自动化脚本之文件搜索显示
自动化脚本之文件搜索显示
16 0
|
2天前
|
弹性计算 运维 Shell
|
2天前
|
存储 弹性计算 运维
自动化收集员工信息的Shell脚本
【4月更文挑战第30天】
6 0
|
4天前
|
Java Maven
基于自动化脚本批量上传依赖到nexus内网私服
基于自动化脚本批量上传依赖到nexus内网私服
4 0
|
9天前
|
Shell
CentOS6.5自动化安装LAMP脚本
CentOS6.5自动化安装LAMP脚本
|
10天前
|
Linux 数据安全/隐私保护
自动化脚本之加密内容解密适用于Linux
自动化脚本之加密内容解密适用于Linux
10 1
|
10天前
自动化脚本之4G模块配置
自动化脚本之4G模块配置
11 0

热门文章

最新文章