我有一个应用程序,其主要目的是将数据输入HealthKit。我想编写一些Xcode UI测试来验证它是否成功编写了这些数据,但我在Health应用程序中验证数据时遇到了一些困难。
当我最初录制我的测试时,它跳过我的模拟主页按钮按下,但是当我滑到第一个主屏幕并导航到Health应用程序以显示数据点时,它正在录制。
我搜索了如何按下主页按钮,找到了这个(有效):
XCUIDevice.shared.press(.home)
但是,它记录的其他所有呼叫实际上都不适用于应用程序之外的导航。用于在主屏幕上滑动的录制代码显然看起来不对,当我用tap()
或swipeRight()
替换swipeLeft()
时,也无法正常工作:
app.childrenMatchingType(.Window).elementBoundByIndex(1).childrenMatchingType(.Other).elementBoundByIndex(1).childrenMatchingType(.Other).element.childrenMatchingType(.Other).element.childrenMatchingType(.Other).elementBoundByIndex(0).childrenMatchingType(.ScrollView).element.tap()
接下来的几行,用于在主屏幕上启动应用,甚至不适用于当前可见页面上的应用图标:
let elementsQuery = app.scrollViews.otherElements
elementsQuery.icons["Health"].tap()
有没有办法实现我想要做的事情,或者我需要等待验证端到端测试,直到我添加从HealthKit读取到我的应用程序的能力?
答案 0 :(得分:9)
以下是使用Xcode 9的解决方案
Timestamp('2015-12-15 00:00:00')
您可以在this post
中找到系统应用的捆绑包标识符列表对于Xcode 8来说,它有点复杂 要从Springboard启动应用程序,您需要导入以下标题
https://github.com/facebook/WebDriverAgent/blob/master/PrivateHeaders/XCTest/XCUIElement.h https://github.com/facebook/WebDriverAgent/blob/master/PrivateHeaders/XCTest/XCUIApplication.h
然后使用以下内容(例如使用Health)
<强>目标C 强>
let messageApp = XCUIApplication(bundleIdentifier: "com.apple.MobileSMS")
messageApp.activate()
<强>夫特强>
@interface Springboard : NSObject
+ (void)launchHealth;
@end
@implementation Springboard
+ (void)launchHealth
{
XCUIApplication *springboard = [[XCUIApplication alloc] initPrivateWithPath:nil bundleID:@"com.apple.springboard"];
[springboard resolve];
XCUIElement *icon = springboard.icons[@"Health"];
if (icon.exists) {
[icon tap];
// To query elements in the Health app
XCUIApplication *health = [[XCUIApplication alloc] initPrivateWithPath:nil bundleID:@"com.apple.Health"];
}
}
@end
答案 1 :(得分:2)
Swift 4
let app = XCUIApplication(bundleIdentifier: "com.apple.springboard")
答案 2 :(得分:0)
您使用应用程序限制了UI测试,当您按下主页按钮并离开应用程序UI时,您无法执行UI操作,因为代码中的app变量指向您的应用程序。
你可能有像
这样的代码let app = XCUIApplication()
所以你应该修改那条XCUIApplication()行。
let app = XCUIApplication(privateWithPath: nil, bundleID: "com.apple.springboard")
现在你可以退出申请。
根据我的知识,拥有独立的UITestAgent应用程序并使用跳板包ID启动其UiTestcases是很好的,这样您就可以在该应用程序的帮助下测试任何应用程序,而不是像我在产品XYZ代码库中编写一些测试用例并且下一个产品ABC我将在ABC产品的代码库中编写测试!