iOS 9 UI测试-由于目标控件不可用,测试失败(尚未) [英] iOS 9 UI Testing - Test Fails Because Target Control Isn't Available (yet)

查看:110
本文介绍了iOS 9 UI测试-由于目标控件不可用,测试失败(尚未)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试Xcode 7的新UI测试功能(已在WWDC 2015视频"使用Xcode进行UI测试).

I am testing the new UI testing functionality of Xcode 7 (introduced in the WWDC 2015 video "UI Testing in Xcode").

在启动时,我的应用通过更新其垂直面板"约束的值,将登录面板"设置为动画形式:面板从主视图下方(屏幕外)向上滑入视图.

On launch, my app animates a "login panel" into view, by updating the value of its vertical layout constraint: the panel slides up into view from beneath the main view (off screen).

该面板包含两个文本字段,分别是用户名密码.

The panel contains two text fields, for user name and password.

当您点击键盘上用户名文本字段的返回键(标记为"Next")时, password 文本字段将成为第一响应者.当您点击密码文本字段的重新运行键(标记为转到")时,面板关闭"(即,动画退出屏幕),活动指示器开始旋转,并且身份验证开始.

When you tap the return key on the keyboard for the user name text field (labeled "Next"), the password textfield becomes first responder. When you tap the retrun key for the password text field (labeled "Go"), the panel is "dismissed" (i.e., animated back off screen), an activity indicator starts to spin and authentication begins.

我记录了执行上述操作的测试,但是当我尝试再现"该测试时,它似乎失败了,因为文本字段无法立即用于输入:

I recorded a test performing the actions described above, but when I try to "reproduce" it, it fails seemingly because the text fields are not available for input right away:

测试套件选定的测试"始于2015-08-14 13:14:19.454测试 Suite'MyAppUITests'开始于2015-08-14 13:14:19.454测试案例 '-[[MyAppUITests.MyAppUITests testExample]"已启动.
t = 0.00s开始测试
t = 0.00s设置
t = 0.00s启动com.myCompany.MyApp 2015-08-14 13:14:19.953 XCTRunner [4667:451922]继续在 任务ID为1的背景
t = 2.64s等待加载的可访问性
t = 6.20s等待应用程序空闲
t = 6.49s轻按用户ID" TextField
t = 6.49s等待应用程序空闲
t = 6.57s找到用户ID" TextField
t = 6.5.s com.myCompany.MyApp的快照可访问性层次结构
t = 6.60s查找:匹配类型TextField
的后代 t = 6.60s查找:匹配谓词'"User ID" IN标识符的元素
t = 6.60s等待应用程序空闲
t = 6.67s综合事件
t = 6.74s将元素滚动到可见
t = 6.78s断言失败:UI测试失败-无法滚动到可见(通过AX操作)TextField 0x7fc90251a130: 特质:146029150208,{{107.0,807.0},{200.0,30.0}}, placeholderValue:'用户ID',值:fffffaaaa,错误:错误-25204 执行AXAction 2003:0:错误: -[MyAppUITests.MyAppUITests testExample]: UI测试失败-无法滚动到可见(通过AX操作)TextField 0x7fc90251a130:特征: 146029150208,{{107.0,807.0},{200.0,30.0}},占位符值:'User ID',值:fffffaaaa,错误:执行AXAction 2003的错误-25204
t = 6.78s拆除测试用例'-[MyAppUITests.MyAppUITests testExample]'失败(6.783秒).
测试套件'MyAppUITests'在2015-08-14 13:14:26.238失败.
在6.783(6.784)中执行了1个测试,其中1个失败(0个意外) 秒

Test Suite 'Selected tests' started at 2015-08-14 13:14:19.454 Test Suite 'MyAppUITests' started at 2015-08-14 13:14:19.454 Test Case '-[MyAppUITests.MyAppUITests testExample]' started.
t = 0.00s Start Test
t = 0.00s Set Up
t = 0.00s Launch com.myCompany.MyApp 2015-08-14 13:14:19.953 XCTRunner[4667:451922] Continuing to run tests in the background with task ID 1
t = 2.64s Waiting for accessibility to load
t = 6.20s Wait for app to idle
t = 6.49s Tap "User ID" TextField
t = 6.49s Wait for app to idle
t = 6.57s Find the "User ID" TextField
t = 6.57s Snapshot accessibility hierarchy for com.myCompany.MyApp
t = 6.60s Find: Descendants matching type TextField
t = 6.60s Find: Elements matching predicate '"User ID" IN identifiers'
t = 6.60s Wait for app to idle
t = 6.67s Synthesize event
t = 6.74s Scroll element to visible
t = 6.78s Assertion Failure: UI Testing Failure - Failed to scroll to visible (by AX action) TextField 0x7fc90251a130: traits: 146029150208, {{107.0, 807.0}, {200.0, 30.0}}, placeholderValue: 'User ID', value: fffffaaaa, error: Error -25204 performing AXAction 2003 :0: error: -[MyAppUITests.MyAppUITests testExample] : UI Testing Failure - Failed to scroll to visible (by AX action) TextField 0x7fc90251a130: traits: 146029150208, {{107.0, 807.0}, {200.0, 30.0}}, placeholderValue: 'User ID', value: fffffaaaa, error: Error -25204 performing AXAction 2003
t = 6.78s Tear Down Test Case '-[MyAppUITests.MyAppUITests testExample]' failed (6.783 seconds).
Test Suite 'MyAppUITests' failed at 2015-08-14 13:14:26.238.
Executed 1 test, with 1 failure (0 unexpected) in 6.783 (6.784) seconds

(重点是我.项目名称和捆绑包ID已更改为假值)

如何处理这样的UI? (即,测试代码必须等待特定的动画完成,然后目标控件才能开始运行)

How can I handle a UI like this? (i.e., test code has to wait for a certain animation to complete before the target control becomes operable)

我不记得视频中提到了这类时间"问题.

I can't remember the video mentioning these kind of 'timing' issues.

推荐答案

我建议反对勾选运行循环,而是使用内置的XCTest异步API.例如:

I would recommend against ticking the run loop and instead use the built in XCTest asynchronous API. For example:

let app = XCUIApplication()
app.launch()

let textField = app.textField["user name"]
let existsPredicate = NSPredicate(format: "exists == 1")

expectationForPredicate(existsPredicate, evaluatedWithObject: textField, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)

textField.tap()
textField.type("joemasilotti")

这将一直等到屏幕上实际存在带有可访问性标签/标识符用户名"的文本字段.然后,它将点击文本字段,使其处于活动状态,然后输入我的用户名.如果经过五秒钟才成为现实,则测试将失败.

This will wait until the text field with accessibility label/identifier "username" actually exists on the screen. Then it will tap on the text field, making it active, and type my username. If five seconds pass before this becomes true the test will fail.

我还在博客上的如何使用UI测试上写了一篇文章,并整理了<对于这种情况,请参见href ="https://github.com/joemasilotti/UI-Testing-Cheat-Sheet"> UI测试备忘单.

I have also written a post on how to use UI Testing on my blog and put together a UI Testing Cheat Sheet for scenarios like this.

这篇关于iOS 9 UI测试-由于目标控件不可用,测试失败(尚未)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆