软件测试课程设计—Appium的运用实战

本文介绍了如何配置Appium环境进行软件测试,包括启动模拟器、定位元素API(如隐式等待、显式等待)、元素操作(点击、输入、清空等),并提供了错误解决办法,如遇到com.android.ddmlib.SyncException可通过关闭Appium解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.配置环境前提(自行百度安装--基操)

默认已经有了JDK,Android Studio,Python环境,Pythoncharm

Appium_Python_Client(调用客户端库和 Appium Server 进行通信)

pip install Appium-Python-Client

测试是否连接成功,终端输入

adb devices -l

查看app的包名以及appActivity

adb shell dumpsys activity recents | find “intent={”

屏幕截图

1.adb shell screencap /sdcard/screen.png

2.adb pull /sdcard/screen.png C:\Users\Administrator\Desktop\大三下作业

4.Appium入门demo

步骤:

​ 1.打开模拟器

​ 2.打开Appium

​ 3.找到需要测试的包名和界面名(adb shell dumpsys window windows |findstr mFocusedApp)

​ 3.通过python编写代码,启动应用程序

效果:就是自动启动哔哩哔哩

# 导入webdriver
import time

from appium import webdriver
# 初始化参数  启动app的统一代码
desired_caps = {
    'platformName': 'Android',  # 被测手机是安卓
    'platformVersion': '7',  # 手机安卓版本
    'deviceName': 'xxx',  # 设备名,安卓手机可以随意填写
    'appPackage': 'tv.danmaku.bili',  # 启动APP Package名称
    'appActivity': '.ui.splash.SplashActivity',  # 启动Activity名称
    'unicodeKeyboard': True,  # 使用自带输入法,输入中文时填True
    'resetKeyboard': True,  # 执行完程序恢复原来输入法
    'noReset': True,  # 不要重置App,如果为False的话,执行完脚本后,app的数据会清空,比如你原本登录了,执行完脚本后就退出登录了
    'newCommandTimeout': 6000,
    'automationName': 'UiAutomator2'
}
# 连接Appium Server,初始化自动化环境
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
time.sleep(5)
driver.quit()

5.获取app元素信息

5.1小工具的运用

两种查找app元素信息的方式

(1)使用appium (2)使用uiautomatorviewer(Android SDK自带的元素定位工具)

方法二:

image.png

image.png

5.2定位元素API

5.2.1定位一个元素

find_element_by_id() find_element_by_class_name() find_element_by_xpath() find_element_by_css_selector()

5.2.2定位一组元素

find_elements_by_id() find_elements_by_class_name() find_elements_by_xpath() find_elements_by_css_selector()

5.2.3等待

隐式等待 (全局元素有效)

#隐式等待  在设置完超时间之后,后续所有定位元素的方法都在这个时间内等待元素出现,如果出去继续执行,若没有出现则报NoSuchElementException
driver.implicitly_wait(200)

显式等待 (单个元素有效)

关键类:WebDriverWait

关键方法:WebDriverWait对象中的until方法

作用:在设置了显示等待之后,可以等待一个超时时间,在这个超时时间之内进行查找,默认每0.5秒找一次

在这个时间内等待元素出现,如果出去继续执行,若没有出现则报TimeOutException

6.元素操作API

6.1点击

注意:如果要输入中文,记得在前置代码输入

'unicodeKeyboard': True,  # 使用自带输入法,输入中文时填True
'resetKeyboard': True,  # 执行完程序恢复原来输入法

``

element.click()

6.2 在输入框输入文字

element.send_keys(“内容”)

6.3 清空输入框输入内容

element.clear()

6.4 获取元素文本内容

element.text

6.5获取元素大小和位置(均为字典类型)

``

print("获取元素位置和大小")
print(button.size)
print(button.location)
print(button.location["x"])

测试demo

# 导入webdriver
from appium import webdriver
# 初始化参数
desired_caps = {
        'platformName': 'Android',  # 被测手机是安卓
    'platformVersion': '7',  # 手机安卓版本
    'deviceName': 'xxx',  # 设备名,安卓手机可以随意填写
    'appPackage': 'tv.danmaku.bili',  # 启动APP Package名称
    'appActivity': '.ui.splash.SplashActivity',  # 启动Activity名称
    'unicodeKeyboard': True,  # 使用自带输入法,输入中文时填True
    'resetKeyboard': True,  # 执行完程序恢复原来输入法
    'noReset': True,  # 不要重置App,如果为False的话,执行完脚本后,app的数据会清空,比如你原本登录了,执行完脚本后就退出登录了
    'newCommandTimeout': 6000,
    'automationName': 'UiAutomator2'
}
# 连接Appium Server,初始化自动化环境
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 设置等待时间,如果不给时间的话可能会找不到元素
driver.implicitly_wait(5)
# 点击搜索框
driver.find_element_by_id("expand_search").click()
# 输入“泰坦尼克号”
driver.find_element_by_id("search_src_text").send_keys("软件测试")
# 键盘回车
driver.keyevent(66)
# 因为它搜索完后就直接退出app了,看不到搜索结果页,所以我给了一个让他停下的方法
input('**********')
driver.quit()

相关截图

查看设备

image.png

在Appium中输入参数

image.png

image.png

通过id去找元素

image.png

错误解决办法

weditor

当我在点击这个的时候,报错了

Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncException: Remote object doesn’t exist! Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncException: Remote object doesn’t exist!

解决办法:把Appium给关闭/停止即可

如果觉得文章对你有帮助,麻烦 点赞、评论、收藏 你的支持是我最大的动力!!!

最后小编在学习过程中整理了一些学习资料,可以分享给做java的工程师朋友们,相互交流学习,需要的可以加入我的学习交流群 716055499 即可免费获取Java架构学习资料(里面有高可用、高并发、高性能及分布式、Jvm性能调优、Spring源码,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多个知识点的架构资料)

作者:dingyu002

来源:dinyu002

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值