ios - 在 Swift 3 UI 测试中访问自定义 View 组件中的元素

标签 ios iphone unit-testing swift3 xcode-ui-testing

我是任何类型的 iOS 编程的新手。我正在尝试为我的场景之一编写 UI 测试用例。

以下是我使用重新编码方法并点击自定义组件时得到的代码。

let button = XCUIApplication().children(matching: .window).element(boundBy: 0).children(matching: .other).element.children(matching: .button).element

在这个自定义组件中有两个按钮。我想知道选择了哪个按钮。为此,我需要识别按钮。但无论我点击自定义 View ,我都会得到相同的代码。

如何访问自定义 View 中的每个组件。任何帮助都会很棒。

最佳答案

在应用程序代码中向您的自定义 View 添加辅助功能标识符。

let customView: UIView!
customView.accessibilityIdentifier = "myCustomView"

然后像这样访问内容:

let app = XCUIApplication()
let customView = app.otherElements["myCustomView"]
let button1 = customView.buttons.element(boundBy: 0)
let button2 = customView.buttons.element(boundBy: 1)
XCTAssertTrue(button1.isSelected)
XCTAssertFalse(button2.isSelected)

请注意,要使您的测试具有确定性,您应该已经知道应该选择哪个按钮。这可确保您的测试每次运行时都测试相同的内容。

关于ios - 在 Swift 3 UI 测试中访问自定义 View 组件中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43658903/

相关文章:

ios - "Ghost" View 出现

iphone - 有什么方法可以防止用户删除已安装的应用程序?

angular - Angular 测试中的 SpyOn

c# - 如何使用 Moq 为异步函数抛出异常

ruby-on-rails - Ruby on Rails 单表继承(STI)和单元测试问题(使用 PostgreSQL)

c# - 从网站将联系人保存到手机

ios - 导航栏在 iPhone 4 (iOS 7.1.2) 中消失

objective-c - Objective C 在两个字符串连接之间插入变量

iphone - Xcode 如何知道应用程序属于哪个配置文件?

iphone - 创建一个对象和 NIL 值