我知道自学Appium和Python还是一个比较费时间的过程。所以,希望我的学习经历能带刚入坑的小伙伴 少走一些弯路,尽快能用上这么牛逼的一个工具,足矣!
如何用Python语言写Appium测试脚本?我之前写过就是利用AppiumDesktop录制脚本,但是这个录制出 来的脚本可读性比较差,今天我所讲到的就是修改录制出来的脚本,再模仿这个脚本去编写其他的测试 用例。
今天讲到的测试App是我们公司正在开发的一款,使用的是iOS模拟器(当然真机也可以,配置一下真机 的信息就好了,因为我们公司没有iPhone测试机,需要使用个人的手机进行测试,对于手机就比较爱惜 了,主要还是因为穷,换个手机太难了。所以我就等在模拟器上验证以后再在真机上实践),实现的功 能为登录。
以下为录制出来的脚本:
# This sample code uses the Appium python client # pip install Appium-Python-Client # Then you can paste this into a file and simply run with Python from appium import webdriver
#配置模拟器信息 caps = {} caps["platformName"] = "ios" caps["deviceName"] = "iPhone 7 Plus" caps["app"] = "/Users/guxuecheng/Desktop/SmartLife.app" caps["platformVersion"] = "10.3" caps["moReset"] = True driver = webdriver.remote("http://0.0.0.0:4723/wd/hub", caps)
/#滑动屏幕(第一次安装App的时候会有引导页) TouchAction(driver) .press({x: 335, y: 365}) .moveTo({x: -209: y: 2}) .release()
TouchAction(driver) .press({x: 323, y: 387}) .moveTo({x: -213: y: 21}) .release()
#点击“立即使用”按钮进入登录页面 TouchAction(driver).tap([(212, 635)]) /#点击输入手机号textfield(这里是用xpath进行定位,但其实这种方法定位很不好,官方并不推荐,因为 能否定位成功需要看人品,如果没有accessibility_id,最好还是和开发说一下,让万能的开发工程师在代码里 给加上吧,我在后面修改好的脚本就是用的accessibility_id进行定位的) el1 = driver.find_element_by_xpath("//XCUIElementTypeApplication[@name=\"i+智慧社区 \"]/XCUIEleme ntTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther /XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeImage/XCUIElementTypeOther[2]/XCU IElementTypeOther/XCUIElementTypeOther[1]/XCUIElementTypeTextField")
el1.click() el1.send_keys("12212345678")
#点击输入密码textfield el2 = driver.find_element_by_xpath("//XCUIElementTypeApplication[@name="i+智慧社区"]/XCUIElemen tTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOt her/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeImage/XCUIElementTypeOther[2] /XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeSecureTextField")
el2.click() el2.send_keys("6666666")
/#点击登录按钮 TouchAction(driver).tap([(217, 377)])
/#登录以后立即退出(网络稍有延迟,你都看不到首页就被退出了,所以这里我们会设置等待时间) driver.quit() 看着上面这个脚本感觉咋样?你们有看着舒服的你们就这么搞,反正我看着不够清晰,然后就想办法对 这个代码进行修改。
修改后的代码如下:
# This sample code uses the Appium python client # pip install Appium-Python-Client # Then you can paste this into a file and simply run with Python # -*- coding: utf-8 -*- #!/usr/bin/env python3 from appium import webdriver import time time.sleep(3)
caps = {} caps["platformName"] = "ios" caps["deviceName"] = "iPhone 7 Plus" caps["app"] = "/Users/guxuecheng/Desktop/SmartLife.app" caps["platformVersion"] = "10.3" caps["moReset"] = True
driver = webdriver.Remote("http://0.0.0.0:4723/wd/hub", caps)
#滑动屏幕 driver.swipe ( 356, 598, -313, -6,3) #滑动屏幕暂停1s,确保滑动到下一页 time.sleep(1) driver.swipe ( 356, 598, -313, -6,3) time.sleep(1)
#立即体验---进入登录页面 driver.tap([(212,635)]) time.sleep(1)
#注意这里使用的是accessibility_id进行定位 el1 = driver.find_element_by_accessibility_id("_phoneNumT") el1.click() el1.send_keys("12212345678")
el2 = driver.find_element_by_accessibility_id("_passwordT") el2.click() el2.send_keys("6666666")
#登录 driver.find_element_by_accessibility_id("_loginButton").click()
time.sleep(10)
driver.quit() 我个人还是觉得修改后的代码稍微美观一些。以上脚本所用到的接口都是Appium Python Client 如果你确实是一个小白(高手请绕道),在这里请思考一个问题:这样一个脚本在实际工作中能用吗? 能满足测试要求吗?能替代手工吗?如果以上答案都为否定,那么该如何改进呢? 可爱的Python给我们提供了一个很好的框架 unittest. 利用这个框架我们就能写出更加完善的测试用例了。
下面就是用了unittest框架实现了登录功能的脚本
# This sample code uses the Appium python client # pip install Appium-Python-Client # Then you can paste this into a file and simply run with Python # -*- coding: utf-8 -*- #!/usr/bin/env python3 from appium import webdriver #from time import sleep import time #from unittest import TestCase import unittest #import sys #import os time.sleep(3)
caps = {} caps["platformName"] = "ios" caps["deviceName"] = "iPhone 7 Plus" caps["app"] = "/Users/guxuecheng/Desktop/SmartLife.app" caps["platformVersion"] = "10.3" caps["moReset"] = True
#TestCase类,所有测试用例类继承的基本类 class LoginTest(unittest.TestCase):
#setUp()方法用于测试用例执行前的初始化工作。如测试用例中需要访问数据库,可以在setUp中 建立数据库链接 #并进行初始化。如测试用例需要启动Appium服务,则需要在该方法内启动Appium服务。 def setUp(self): self.driver = webdriver.Remote("http://0.0.0.0:4723/wd/hub", caps)
#tearDown()方法用于测试用例执行之后的善后工作。如关闭数据库连接,退出应用。 #无论这个方法写在哪里,都是最后才执行 def tearDown(self): self.driver.quit()
#具体的测试用例,必须要以test开头 def test_start(self): self.driver.swipe ( 356, 598, -313, -6,3) time.sleep(1) self.driver.swipe ( 356, 598, -313, -6,3) time.sleep(1)
#立即体验---进入登录页面 self.driver.tap([(212,635)]) time.sleep(1)
el1 = self.driver.find_element_by_accessibility_id("_phoneNumT") el1.click() el1.send_keys("13012345678")
el2 = self.driver.find_element_by_accessibility_id("_passwordT") el2.click() el2.send_keys("6666666")
#点击登录按钮 self.driver.find_element_by_accessibility_id("_loginButton").click() time.sleep(3) self.assertIsNotNone(self.driver.find_element_by_accessibility_id('tab_button_home_normal'),['进 入到首页,登录成功,TRUE']) time.sleep(10)
if __name__ == '__main__':
#构造测试集 defaultTestLoader()即TestLoader()测试用例加载器,包括多个加载测试用例的 方法,返回一个测试套件 #loadTestsFromTestCase()根据给定的测试类,获取其中的所有测试方法,并返回一个测试套件 suite = unittest.TestLoader().loadTestsFromTestCase(LoginTest)
|
<ignore_js_op> -
1.png (11.8 KB, 下载次数: 0)
<ignore_js_op> -
2.png (61.88 KB, 下载次数: 0)
<ignore_js_op> -
3.png (40.51 KB, 下载次数: 0)
<ignore_js_op> -
4.png (55.25 KB, 下载次数: 0)
|
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!