掌握 UITesting 需要时间。在确保捕获所有边缘情况、系统警报以及底层开发的任何不常用 UI 解决方案的同时,Macchiato
允许您简化此过程。移除覆盖边缘情况、动画、网络和系统警报的负担,您只需在本地或远程的 JSON
文件中管理测试用例即可。
这是一个为希望将 UITesting 与自己的 CI 解决方案集成的开发者提供的完美解决方案。
目录
安装
target 'Project_Tests' do
inherit! :search_paths
pod 'Macchiato'
end
项目设置
第一步 - 设置测试工具
-
将一个新的
XCTestCase
添加到 UI 测试目标,并导入Macchiato
import XCTest import Macchiato class MacchiatoTests: XCTestCase { override func setUpWithError() throws { } override func tearDownWithError() throws { } }
-
让我们配置和启动 SDK
将此添加到
setUpWithError()
函数中let app = XCUIApplication() var testingManager: Macchiato.Manager! override func setUpWithError() throws { continueAfterFailure = false guard let path = Bundle(for: type(of: self)).url(forResource: "tests", withExtension: "json") else { XCTFail("No tests file found"); return } let configurations = Macchiato.Configurations(contentsOfFile: path, bundleIdentifier: "com.company.ios", app: app) testingManager = Macchiato.Manager(configurations: configurations, target: self) testingManager.launch() }
您还可以注入
contentsOfURL
。
let configurations = Macchiato.Configurations(contentsOfURL: url, bundleIdentifier: "com.company.ios", app: app)
-
现在我们已经准备好设置测试用例了
func testMacchiato(){ testingManager.runTests() }
testingManager.runTests()
将运行测试,如果有失败将报告。
步骤二 - 配置您的项目
概述
测试导航通过查询 XCUIElement
和 XCUIElementQuery
类型来实现。查看导航类型以获取完整列表 这里。UI 测试工具通过索引或键来识别每个元素,例如
// Index
buttons.element(boundBy: index)
// Key
buttons[key]
我们需要做什么?
我们需要为每个元素设置一个 accessibilityIdentifier
。这可以在 Xcode 的身份检查器下的工具面板中完成。
您可能开始觉得这可能需要太长时间!不必多说... 这已经为您处理好了,您只需遵循以下 3 个简单步骤
1) Add `pod 'StanwoodCore` to your podfile
2) import StanwoodCore in any .swift files that contains UI elements
3) When setting labels/text, you have two options:
a) Set the localised KEY in interface builder, i'e "MY_KEY_TITLE"
b) Set `.localisedText` instead of `.text`.
Note> It is required that you do not localise the key, rather then pass in the key. This will get handled by StanwoodCore
创建您的测试用例
概述
Macchiato 通过查询视图层次结构中的元素类型来工作,并且可以通过调用自定义键或索引来访问。例如,如果我们从 develop.apple.com 查看下面的图像,我们可以看到元素是如何布局的。
这是一个很好的例子,其中我们有一个顶级的 UIView
,可以用键来识别,以及一个 UICellectionView
,其单元格可以用索引来识别。
让我们创建第一个测试用例
-
首先,我们想要设置JSON格式的模式
{ "test_cases" : [ ] }
-
创建测试用例
{ "test_cases": [ "id" : "1", "title": "Images Test", "description": "Testing if the fifth image is tappable" ] }
-
设置
navigation
动作以支持测试用例导航是一组导航动作。我们需要将导航项设置为我们要测试的位置。例如,让我们根据上面的例子设置导航项。
假设这个视图处于打开状态
-
第二个标签可以通过
tabs
作为索引1访问 -
并且可以通过在标签的
rootView
中轻触具有标识符pierIdentifier
的按钮来访问图像视图 -
可以通过索引4在
cells
中访问第五张图像{ "test_cases": [ "id" : "1", "title": "Images Test", "description": "Testing if the fifth image is tappable", "navigation" : [ "tabs[1].action.tap", "buttons['pierIdentifier'].action.tap", "cells[4].action.tap" ] ] }
-
-
现在,假设我们想测试位置在11的图像,该图像无法在视图中访问,我们可以设置不同的动作,例如
swipeUp, swipeDown
。例如{ "test_cases": [ "id": "1", "title": "Images Test", "description": "Testing if the fifth image is tappable", "navigation": [ "tabs[1].action.tap", "buttons['pierIdentifier'].action.tap", "cells[4].action.swipeUp", "cells[10].action.tap" ] ] }
有关完整动作列表,请参阅此处
有关完整的导航类型,请参阅此处。UI 测试工具通过索引或键识别每个元素。
注意:元素标识符将列在每个项目文档下的 UI 测试标识符
处理系统警报
Macchiato
支持系统警报。为了监控系统警报,只需向任何导航处理中添加 .monitor
即可。
{
"test_cases": [
"id": "1",
"title": "Images Test",
"description": "Testing if the fifth image is tappable",
"navigation": [
"tabs[1].action.tap",
"buttons['pierIdentifier'].action.tap.monitor",
"cells[4].action.swipeUp",
"cells[10].action.tap.monitor"
]
]
}
截图
截图已集成到动作列表中。要获取截图,请添加 action.screenshot
。
要启用截图,请将环境变量添加到方案中 名称: SRCROOT,值: ${SRCROOT}
{
"test_cases": [
"id": "1",
"title": "Images Test",
"description": "Testing if the fifth image is tappable",
"navigation": [
"action.screenshot",
"tabs[1].action.tap",
"buttons['pierIdentifier'].action.tap.monitor",
"cells[4].action.swipeUp",
"action.screenshot",
"cells[10].action.tap.monitor"
]
]
}
发布说明
0.5
- 将框架重命名为 Macchiato
- 添加对文件或url内容的支持
0.1.5
- 添加对导航栏的支持
- 改进错误处理
- 移除一些未使用的密钥
作者
Tal Zion [email protected]
许可
Macchiato 在 MIT 许可下。更多信息请参阅 LICENSE 文件。