“无法获得刷新的快照"使用 UIPickerView 进行 UI 测试时出错 [英] "Failed to get refreshed snapshot" error when UI testing with a UIPickerView

查看:22
本文介绍了“无法获得刷新的快照"使用 UIPickerView 进行 UI 测试时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 UIPickerView 的时间选择器视图,其中包含 6 个部分、小时、分钟、秒和它们之间的固定标签轮.

I have a time selector view that uses a UIPickerView with 6 sections, hours, minutes, seconds and fixed label wheels between them.

当我尝试在 XCode 7 中针对此视图运行 UI 测试时,模拟器冻结并最终看到错误:

When I try to run a UI test against this view in XCode 7, the simulator freezes and I eventually see the error:

无法获取刷新的快照"

我的测试目前只是尝试与视图中的取消按钮交互:

My test currently just attempts to interact with the cancel button in the view:

app.buttons["Cancel"].tap()

我也试过等待取消按钮存在.从 UIView 中移除选择器视图会停止测试冻结,所以我知道问题出在那里.

I've also tried waiting for the cancel button to exist. Removing the picker view from the UIView stops the test freezing, so I know the problem lies there.

UIPickerViewDelegateUIPickerViewDataSource 方法如下:

let HOUR_INDEX = 0
let HOUR_LABEL_INDEX = 1
let MINUTE_INDEX = 2
let MINUTE_LABEL_INDEX = 3
let SECOND_INDEX = 4
let SECOND_LABEL_INDEX = 5

let TOTAL_COMPONENTS = 6

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return TOTAL_COMPONENTS
}

// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if component == HOUR_INDEX {
        return 12 * 100
    } else if component == MINUTE_INDEX || component == SECOND_INDEX {
        return 60 * 100
    } else {
        return 1
    }
}

func pickerView(
    pickerView: UIPickerView,
    attributedTitleForRow row: Int,
    forComponent component: Int) -> NSAttributedString? {
        let paragraphStyle = NSMutableParagraphStyle()
        if component == HOUR_INDEX || component == MINUTE_INDEX || component == SECOND_INDEX {
            paragraphStyle.alignment = NSTextAlignment.Right
        } else {
            paragraphStyle.alignment = NSTextAlignment.Left
        }

        if let titleData = getPickerTitle(row, forComponent: component) {
            let myTitle = NSAttributedString(string: titleData,
                attributes: [
                    NSParagraphStyleAttributeName: paragraphStyle
            ])
            return myTitle
        }
        return nil
}

func getPickerTitle(row: Int, forComponent component: Int) -> String? {
    if component == HOUR_INDEX {
        return "\(row%12)"
    } else if component == MINUTE_INDEX || component == SECOND_INDEX {
        return "\(row%60)"
    } else {
        if component == HOUR_LABEL_INDEX {
            return "h"
        }
        if component == MINUTE_LABEL_INDEX {
            return "m"
        }
        if component == SECOND_LABEL_INDEX {
            return "s"
        }
        return nil
    }
}

推荐答案

与获取快照相关的错误很可能是由可访问性层次结构中的元素过多引起的.为了能够查询 UI,测试框架正在生成完整的层次结构.

The error relating to getting a snapshot may very well be caused by too many elements in the accessibility hierarchy. In order to be able to query the UI the testing framework is generating the complete hierarchy.

我看到您正在使用乘以小时、分钟和秒选择器中的行数的技巧来创建看似循环选择器项目的效果.使用三个轮子之间的当前实现,将在可访问性层次结构中生成 13200 个 UI 元素.

I see that you're using the trick of multiplying the number of rows in the hour, minute, and second picker to create the effect of seemingly looping picker items. With the current implementation between the three wheels 13200 UI elements will be generated in the accessibility hierarchy.

我正在使用相同的选择器视图技巧并看到相同的错误.通过在运行测试时将每个选择器轮的行数减少到一百以下,根据我的经验,获取可访问性层次结构的快照是没有问题的.

I am using the same picker view trick and have seen the same error. By reducing the number of rows to less than a hundred per picker wheel when running tests there's no problem getting a snapshot of the accessibility hierarchy in my experience.

这篇关于“无法获得刷新的快照"使用 UIPickerView 进行 UI 测试时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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