当前位置: 首页 > article >正文

“一篇长文教你进行全方位的使用appium“

随着移动应用的日益普及,移动应用的测试成为了软件开发的重要组成部分。Python,作为一种易于学习,功能强大的编程语言,特别适合进行这种测试。本文将详细介绍如何使用Python进行APP测试,并附带一个实例。

Python 和 Appium:一种强大的组合

在进行APP测试时,我们通常使用自动化测试工具。其中,Appium是一种非常流行的开源移动应用自动化测试工具,可以支持Android和iOS的应用测试。它的一个主要优点是,你可以使用任何你喜欢的编程语言来编写测试脚本,只要这种语言可以创建HTTP请求。其中,Python是Appium社区广泛推荐的一种语言。

准备工作

在开始之前,你需要安装一些必要的工具:

Python:你可以从Python官方网站下载并安装。

Appium:你可以从Appium官方网站下载并安装。

Appium-Python-Client:这是一个Python库,让你可以用Python来控制Appium。你可以使用pip安装:pip install Appium-Python-Client。

示例:使用Python和Appium进行APP测试
这是一个基础的测试实例,我们将测试一个简单的计算器APP。首先,我们需要导入必要的库:

from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
  • 1
  • 2

接着,我们需要设置启动参数:

desired_caps = {}
desired_caps['platformName'] = 'Android'  # 你的设备系统
desired_caps['platformVersion'] = '8.1.0'  # 你的设备系统版本
desired_caps['deviceName'] = 'emulator-5554'  # 你的设备名称
desired_caps['appPackage'] = 'com.android.calculator2'  # 你的APP的包名
desired_caps['appActivity'] = 'com.android.calculator2.Calculator'  # 你的APP的主Activity
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

然后,我们创建一个driver:

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

现在我们就可以开始编写我们的测试脚本了。比如,我们可以测试一下计算器的加法功能:

driver.find_element(MobileBy.ID, "com.android.calculator2:id/digit_2").click()
driver.find_element(MobileBy.ID, "com.android.calculator2:id/op_add").click()
driver.find_element(MobileBy.ID, "com.android.calculator2:id/digit_3").click()
driver.find_element(MobileBy.ID, "com.android.calculator2:id/eq").click()

result = driver.find_element(MobileBy.ID, "com.android.calculator2:id/result").text
assert result == '5'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

最后,我们需要关闭driver:

driver.quit()
  • 1

通过这个简单的例子,你可以看到Python和Appium的强大之处。你可以很方便地定位元素,进行点击等操作,并检查结果是否符合预期。

高级用法:自动化测试

以上我们介绍的是一个简单的测试例子,但在实际开发中,我们可能需要进行大量的自动化测试。为了实现这个目标,我们可以使用一些高级的技术,比如Python的unittest模块。

unittest是Python的标准库之一,提供了丰富的断言方法和组织测试的方式。这是一个使用unittest进行APP自动化测试的基本示例:

import unittest
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy


class TestCalculator(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '8.1.0'
        desired_caps['deviceName'] = 'emulator-5554'
        desired_caps['appPackage'] = 'com.android.calculator2'
        desired_caps['appActivity'] = 'com.android.calculator2.Calculator'
        cls.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

    def test_addition(self):
        self.driver.find_element(MobileBy.ID, "com.android.calculator2:id/digit_2").click()
        self.driver.find_element(MobileBy.ID, "com.android.calculator2:id/op_add").click()
        self.driver.find_element(MobileBy.ID, "com.android.calculator2:id/digit_3").click()
        self.driver.find_element(MobileBy.ID, "com.android.calculator2:id/eq").click()

        result = self.driver.find_element(MobileBy.ID, "com.android.calculator2:id/result").text
        self.assertEqual(result, '5')

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()


if __name__ == '__main__':
    unittest.main()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

这个示例中,我们创建了一个TestCalculator的类,它继承自unittest.TestCase。在这个类中,我们可以编写多个测试方法,每个方法都是一个完整的测试,可以独立运行。setUpClass和tearDownClass方法会在所有测试开始前和所有测试结束后运行,通常我们在这里进行一些初始化和清理工作。

1. 使用 Page Object Model(POM)

POM 是一种设计模式,用于创建更可读、更可维护的自动化测试代码。在这个模式中,你会为每个APP页面创建一个 Python 类,这个类包含了所有与这个页面相关的操作。这样,你的测试代码就不再直接和页面元素打交道,而是和这些抽象的 Page Object 打交道,这让你的代码更容易理解和修改。

class LoginPage:
    def __init__(self, driver):
        self.driver = driver

    def enter_username(self, username):
        self.driver.find_element(MobileBy.ID, "username_field").send_keys(username)

    def enter_password(self, password):
        self.driver.find_element(MobileBy.ID, "password_field").send_keys(password)

    def click_login_button(self):
        self.driver.find_element(MobileBy.ID, "login_button").click()

# 在测试中使用
login_page = LoginPage(driver)
login_page.enter_username('testuser')
login_page.enter_password('testpass')
login_page.click_login_button()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

2. 使用 Appium 的 Touch Action 和 Multi Touch

Appium 提供了 Touch Action 和 Multi Touch API,让你可以模拟各种复杂的用户手势,比如滑动、拖拽、多点触控等。这些功能让你可以更真实地模拟用户的操作,测试更多的使用场景。

from appium.webdriver.common.touch_action import TouchAction

# 执行一个滑动操作
action = TouchAction(driver)
action.press(x=100, y=100).move_to(x=200, y=200).release().perform()

# 多点触控示例
from appium.webdriver.common.multi_action import MultiAction
action1 = TouchAction(driver).press(x=100, y=100).move_to(x=200, y=200).release()
action2 = TouchAction(driver).press(x=200, y=200).move_to(x=100, y=100).release()

multi_action = MultiAction(driver)
multi_action.add(action1, action2)
multi_action.perform()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3. 使用 Appium 的定位策略

除了基本的 id、class name 和 xpath 定位策略,Appium 还提供了许多高级的定位策略。比如,你可以使用 image 定位策略来定位那些没有明确 id 或 class name 的元素,你还可以使用 accessibility id 来定位那些为了提高无障碍访问性而添加了特殊标签的元素。

# 使用 image 定位
driver.find_element(MobileBy.IMAGE, "path_to_your_image.png")

# 使用 accessibility id 定位
driver.find_element(MobileBy.ACCESSIBILITY_ID, "your_accessibility_id")
  • 1
  • 2
  • 3
  • 4
  • 5

4. 使用 Appium 的日志和报告

Appium 提供了丰富的日志和报告功能,让你可以更方便地调试你的测试代码,了解测试的执行情况。你可以使用 Appium 的 server log 来查看测试的详细执行过程,你还可以使用各种第三方的报告工具,比如 Allure,生成更美观、更详细的测试报告。

# 启动 Appium server 时设置 log 参数
appium --log /path/to/your/logfile.txt
  • 1
  • 2
# 使用 pytest 和 Allure 生成测试报告
def test_example():
    driver.find_element(MobileBy.ID, "some_id").click()
    assert driver.find_element(MobileBy.ID, "another_id").text == 'expected_text'
  • 1
  • 2
  • 3
  • 4

然后,运行 pytest 并使用 Allure 插件:

pytest --alluredir=/your/allure/result/directory
allure serve /your/allure/result/directory
  • 1
  • 2

5. 使用 Python 的并行和异步功能

Python 提供了丰富的并行和异步功能,让你可以更高效地运行你的测试。比如,你可以使用 threading 或 multiprocessing 模块来并行运行多个测试,你还可以使用 asyncio 模块来编写异步的测试代码,这样你的测试就可以同时处理多个任务,提高测试效率。

# 使用 threading 并行运行测试
import threading

def test_one():
    ...

def test_two():
    ...

thread_one = threading.Thread(target=test_one)
thread_two = threading.Thread(target=test_two)

thread_one.start()
thread_two.start()

thread_one.join()
thread_two.join()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

异步编程就更复杂了,可能需要更深入的学习。这是一个简单的例子:

import asyncio

async def test_one():
    ...

async def test_two():
    ...

await asyncio.gather(test_one(), test_two())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

这些示例都很简单,只是为了给你一个大概的概念。在实际使用中,你可能需要根据你的实际情况进行更复杂的操作。

总结

使用Python进行APP测试是一种高效,强大的方法。无论你是一个软件开发者,还是一个测试工程师,都可以通过学习Python和Appium,提升你的工作效率,提高你的产品质量。希望这篇文章对你有所帮助。


http://www.kler.cn/news/356686.html

相关文章:

  • 使用开源的 Vue 移动端表单设计器创建表单
  • Flink Kubernetes Operator
  • 【实战指南】Vue.js 介绍组件数据绑定路由构建高效前端应用
  • JDK 1.5主要特性
  • v-model双向绑定组件通信
  • 【Python爬虫实战】从文件到数据库:全面掌握Python爬虫数据存储技巧
  • Leecode刷题之路第25天之K个一组翻转链表
  • Bootstrapping、Bagging 和 Boosting
  • 一个mmcv库与chamfer库不兼容的问题
  • OpenCV高级图形用户界面(11)检查是否有键盘事件发生而不阻塞当前线程函数pollKey()的使用
  • 推荐一个处理数据非常好用的在线工具
  • 2024软考网络工程师笔记 - 第3章.广域通信网
  • React 探秘(二): 双缓存技术
  • RHCE —— 笔记
  • 解决一个android service启动无法开文件的问题
  • 总结:SQL查询变慢,常见原因分析!
  • HarmonyOS 应用级状态管理(LocalStorage、AppStorage、PersistentStorage)
  • 美​团​一​面​-​3​​宁​德​时​代​一​面
  • MySQL-20.多表设计-一对一多对多
  • 【windows】win10提示‘adb‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。