在之前的文章中,我们探讨了如何利用 .bat 文件(批处理文件)来批量运行 AirTest 脚本。这种方法的主要优势在于:
-
简化操作流程:通过一个简单的命令即可启动多个测试脚本。
-
提高任务的可重复性:便于定期或按需重复执行相同的测试套件。
-
方便环境配置与错误处理:能够统一管理测试环境设置并集中处理可能出现的异常情况。
-
一定程度的自动化与日志记录支持:有助于实现测试过程的自动化,并记录测试结果以便后续分析。
尽管如此,批处理文件也存在一些局限性,例如功能受限、调试难度大、执行效率低等。此外,在不同操作系统之间使用时可能会遇到兼容性和维护成本问题。因此,在决定是否采用 .bat 文件时,需要根据实际应用场景和技术要求权衡利弊。
对于少量简单用例或不太复杂的调度场景,使用 .bat 文件可以提供一种快速便捷的解决方案。然而,在面对大量测试用例或更复杂的场景时,则可能需要采取更高级别的封装策略来优化执行流程。
接下来,本文将介绍批量执行 Airtest 测试脚本的设计思路。
设计思路
1、将测试脚本的实际业务代码都封装到脚本执行方法中
2、然后创建一个主控脚本,该脚本引入所有测试脚本,并按需调用它们的脚本执行方法
测试用例层级结构
为了更好地组织和管理测试用例,我们以打开百度网站为例,设计了三个测试用例脚本,并将它们划分为两个模块:页面加载模块和搜索模块。下面是具体的测试用例及其分层结构:
页面加载模块
-
测试用例1.1 (case_point_01_01):验证百度首页是否能正常加载并检查样式
-
-
步骤:
-
-
-
打开浏览器。
-
输入网址
https://www.baidu.com并访问。 -
等待页面完全加载。
-
检查页面样式是否符合预期。
-
-
-
预期结果:百度首页成功加载,页面样式正确,可以看到搜索框和其他导航元素。
-
-
测试用例1.2 (case_point_01_02):验证登录功能模块是否正常
-
-
步骤:
-
-
-
打开浏览器。
-
输入网址
https://www.baidu.com并访问。 -
查找并点击登录按钮。
-
输入正确的用户名和密码。
-
点击登录。
-
-
-
预期结果:用户成功登录,页面显示登录后的状态。
-
搜索模块
-
测试用例2.1 (case_point_02_01):验证搜索功能是否有效
-
-
步骤:
-
-
-
打开浏览器。
-
输入网址
https://www.baidu.com并访问。 -
在搜索框中输入关键词“天气”。
-
按下回车键或点击搜索按钮。
-
检查搜索结果页面。
-
-
-
预期结果:搜索结果页面显示与“天气”相关的网页链接和信息。
-
通过这种分层管理的方式,我们可以更有效地组织测试用例,确保每个模块的功能都能得到充分的测试,同时也有利于后期维护和扩展。
目录结构如下

测试用例1:case_point_01_01
-
# -*- encoding=utf8 -*- -
__author__ = "gmluo" -
from airtest.core.api import * -
from airtest.cli.parser import cli_setup -
from selenium import webdriver -
from selenium.webdriver.common.keys import Keys -
from airtest_selenium.proxy import WebChrome -
from airtest.report.report import simple_report -
def run_case_script(): -
if not cli_setup(): -
auto_setup(__file__, logdir=True, devices=["Windows:///", ]) -
driver = WebChrome(executable_path='D:/programs/chromedriver-win64/chromedriver.exe') -
driver.implicitly_wait(20) -
driver.maximize_window() -
driver.get("https://www.baidu.com/") -
driver.assert_template(Template(r"tpl1723541903681.png", record_pos=(0.06, -0.271), resolution=(2710, 2080)), -
"Please fill in the test point.") -
# 退出当前页面 -
driver.close() -
# generate html report -
simple_report(__file__, logpath=True) -
# run_case_script()
测试用例2:case_point_01_02
-
# -*- encoding=utf8 -*- -
__author__ = "gmluo" -
from airtest.core.api import * -
from airtest.cli.parser import cli_setup -
from selenium import webdriver -
from selenium.webdriver.common.keys import Keys -
from airtest_selenium.proxy import WebChrome -
from airtest.report.report import simple_report -
def run_case_script(): -
if not cli_setup(): -
auto_setup(__file__, logdir=True, devices=["Windows:///", ]) -
driver = WebChrome(executable_path='D:/programs/chromedriver-win64/chromedriver.exe') -
driver.implicitly_wait(20) -
driver.maximize_window() -
driver.get("https://www.baidu.com/") -
driver.assert_template(Template(r"tpl1723543008762.png", record_pos=(0.324, -0.044), resolution=(2710, 2080)), -
"Please fill in the test point.") -
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[4]/a").click() -
sleep(1.0) -
driver.assert_template(Template(r"tpl1723602062748.png", record_pos=(0.056, -0.176), resolution=(2710, 2080)), -
"Please fill in the test point.") -
# 退出当前页面 -
driver.close() -
# generate html report -
simple_report(__file__, logpath=True) -
# run_case_script()
测试用例3:case_point_02_01
-
# -*- encoding=utf8 -*- -
__author__ = "gmluo" -
from airtest.core.api import * -
from airtest.cli.parser import cli_setup -
from selenium import webdriver -
from selenium.webdriver.common.keys import Keys -
from airtest_selenium.proxy import WebChrome -
from airtest.report.report import simple_report -
def run_case_script(): -
if not cli_setup(): -
auto_setup(__file__, logdir=True, devices=["Windows:///", ]) -
driver = WebChrome(executable_path='D:/programs/chromedriver-win64/chromedriver.exe') -
driver.implicitly_wait(20) -
driver.maximize_window() -
driver.get("https://www.baidu.com/") -
driver.find_element_by_id("kw").send_keys("UI自动化测试") -
sleep(1.0) -
driver.assert_template(Template(r"tpl1723543454517.png", record_pos=(-0.046, -0.334), resolution=(2710, 2080)), -
"Please fill in the test point.") -
# 退出当前页面 -
driver.close() -
# generate html report -
simple_report(__file__, logpath=True) -
# run_case_script()
测试脚本批量执行
通过在py文件中,引入相应的测试脚本模块,然后通过执行每个脚本下对应的执行函数,就可以在一个py文件中实现多个测试脚本的批量运行
批量脚本执行:基础方法
-
# !/usr/bin/python -
# -*- coding: utf-8 -*- -
# @Author : gmluo1988 -
# @Software : PyCharm -
# @File : batch_runner_method_01.py -
# @Time : 2024/8/14 9:37 -
from case_script_method_01.module_01.case_point_01_01 import case_point_01_01 -
from case_script_method_01.module_01.case_point_01_02 import case_point_01_02 -
from case_script_method_01.module_02.case_point_02_01 import case_point_02_01 -
case_point_01_01.run_case_script() -
case_point_01_02.run_case_script() -
case_point_02_01.run_case_script()
也可以将不同脚本执行代码封装到一个方法函数中,在需要执行的时候加入对应的代码行即可
-
from case_script_method_01.module_01.case_point_01_01 import case_point_01_01 -
from case_script_method_01.module_01.case_point_01_02 import case_point_01_02 -
from case_script_method_01.module_02.case_point_02_01 import case_point_02_01 -
# case_point_01_01.run_case_script() -
# case_point_01_02.run_case_script() -
# case_point_02_01.run_case_script() -
def run_cases(): -
case_point_01_01.run_case_script() -
case_point_01_02.run_case_script() -
case_point_02_01.run_case_script() -
run_cases()
批量执行效果

总结
通过将测试脚本的实际业务代码都封装到脚本执行方法中,在主控脚本,该脚本引入所有测试脚本,并按需调用它们的脚本执行方法,即能够简单实现Pycharm中批量执行AirTest脚本的功能。
感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取

2522

被折叠的 条评论
为什么被折叠?



