开源移动端测试工具Appium使用方法与举例说明

Appium是一个开源的移动端测试自动化框架,可用于测试原生、混合及移动端的Web项目,支持iOS、Android及Firefox OS平台。以下为你介绍其使用方法和示例:

使用方法

  1. 环境搭建

• 安装Appium:可以使用npm(Node.js包管理器)进行安装,在命令行中运行以下命令:

bash 复制代码
 npm install -g appium

• 安装Appium客户端库:根据你使用的编程语言,安装相应的Appium客户端库。以Python为例,使用pip安装Appium Python客户端:

bash 复制代码
 pip install Appium-Python-Client

• 配置移动设备:可以使用模拟器或真机进行测试。如果使用真机,需要开启设备的USB调试模式,并通过USB线将设备连接到电脑;若使用模拟器,需确保模拟器已正确安装和启动。

• 下载对应平台的驱动:对于Android,需要安装Android SDK并配置环境变量;对于iOS,需要安装Xcode和相关的开发工具。

  1. 启动Appium Server

• 可以通过命令行启动Appium Server,在命令行中输入:

bash 复制代码
appium

• 也可以使用Appium Desktop工具,它提供了图形化界面来启动和管理Appium Server。启动后,Appium Server默认监听4723端口。

  1. 编写测试脚本

以下是使用Python编写的Appium测试脚本示例,以Android应用为例:

python 复制代码
from appium import webdriver

# 配置Desired Capabilities

desired_caps = {

    "platformName": "Android", # 平台名称

    "platformVersion": "10", # 平台版本

    "deviceName": "your_device_name", # 设备名称,可以是任意字符串

    "appPackage": "com.example.app", # 应用包名

    "appActivity": "com.example.app.MainActivity", # 应用启动Activity

    "unicodeKeyboard": True, # 使用Unicode编码方式发送字符串

    "resetKeyboard": True # 隐藏键盘

}

# 连接Appium Server

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

try:

    # 定位元素并进行操作

    element = driver.find_element_by_id('com.example.app:id/button')

    element.click()

    # 输入文本

    input_box = driver.find_element_by_id('com.example.app:id/input_box')

    input_box.send_keys('Hello, Appium!')

    # 获取元素文本

    text_element = driver.find_element_by_id('com.example.app:id/text_view')

    text = text_element.text

    print(text)

except Exception as e:

    print(f"An error occurred: {e}")

finally:

    # 关闭应用

    driver.quit()
  1. 运行测试脚本

将上述脚本保存为一个Python文件(例如test_appium.py),在命令行中运行:

python 复制代码
 python test_appium.py

脚本运行时,Appium会根据配置的Desired Capabilities启动应用,并执行脚本中的操作。

使用举例

场景:测试一个简单的计算器应用

假设我们要测试一个计算器应用,实现两个数字相加的功能。以下是具体的测试步骤和脚本:

python 复制代码
 from appium import webdriver

# 配置Desired Capabilities

desired_caps = {

    "platformName": "Android",

    "platformVersion": "10",

    "deviceName": "your_device_name",

    "appPackage": "com.example.calculator",

    "appActivity": "com.example.calculator.MainActivity"

}


# 连接Appium Server

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)


try:

    # 输入第一个数字

    num1_button = driver.find_element_by_id('com.example.calculator:id/num1_button')

    num1_button.click()


    # 点击加号按钮

    plus_button = driver.find_element_by_id('com.example.calculator:id/plus_button')

    plus_button.click()


    # 输入第二个数字

    num2_button = driver.find_element_by_id('com.example.calculator:id/num2_button')

    num2_button.click()


    # 点击等于按钮

    equal_button = driver.find_element_by_id('com.example.calculator:id/equal_button')

    equal_button.click()


    # 获取计算结果

    result_element = driver.find_element_by_id('com.example.calculator:id/result_text')

    result = result_element.text

    print(f"计算结果: {result}")


except Exception as e:

    print(f"An error occurred: {e}")


finally:

    # 关闭应用

    driver.quit()

在这个示例中,我们使用Appium自动化测试计算器应用的加法功能。通过定位按钮元素并模拟点击操作,完成两个数字的输入和计算,最后获取计算结果并打印输出。

相关推荐
xie8810180233 分钟前
正则里的点号不匹配换行:多行模式的开关
运维·服务器·测试工具
东舟技术1 天前
从人工目检到AI视觉:智能座舱瞬态故障的自动化捕获与溯源
功能测试·测试工具·自动化
小猿A2 天前
【2026最新】Apifox下载、配置、实战教程(附官网安装包)
测试工具
泽众云测试3 天前
泽众软件与广州华南检验检测中心签约,PerformanceRunner性能测试工具助力第三方测评机构性能检测能力升级
软件测试·测试工具·性能测试
@zulnger3 天前
Charles抓包工具
测试工具
ck0135793 天前
视频添加水印的实践:文字叠加、透明 PNG 与时间区间设置
图像处理·测试工具·音视频·视频
EVE จุ๊บ4 天前
playwright和selenium的区别是什么
selenium·测试工具
Meta Luo6 天前
页面改版后自动化用例大量失败,应该怎么维护?
运维·功能测试·测试工具·自动化
释槐鸟~6 天前
还不知道RIDE工具怎么装吗
测试工具·自动化
有技巧搬砖7 天前
为什么锂电池组的电池老化测试很重要?
测试工具·锂电池·电池老化