“一篇长文教你进行全方位的使用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

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

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

然后,我们创建一个driver:

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

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

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'

最后,我们需要关闭driver:

driver.quit()

通过这个简单的例子,你可以看到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()

这个示例中,我们创建了一个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()

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()

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")

4. 使用 Appium 的日志和报告

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

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

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

pytest --alluredir=/your/allure/result/directory
allure serve /your/allure/result/directory

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()

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

import asyncio

async def test_one():
    ...

async def test_two():
    ...

await asyncio.gather(test_one(), test_two())

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

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

在这里插入图片描述

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

猜你喜欢

转载自blog.csdn.net/NHB456789/article/details/132918929
今日推荐