2016-12-31 52 views
1

下面是我记录XCTest无法访问编程方式添加的UIView在XCTest

let app = XCUIApplication() 
    let tablesQuery = app.tables 
    tablesQuery.staticTexts["Video"].tap() 
    tablesQuery.staticTexts["\tWindowed"].tap() 
    app.buttons["Launch"].tap() 
    app.buttons["Popout Video"].tap() 
    app.children(matching: .window).element(boundBy: 0).children(matching: .other).element(boundBy: 1).tap() 

当我试图运行测试的最后一部分就是:

app.children(matching: .window).element(boundBy: 0).children(matching: .other).element(boundBy: 1).tap() 

无法访问。它不会抛出任何错误,但不会执行最后一行代码。

我试图参照以下计算器问题解决问题: Xcode UI Tests can't find views that are added programatically

另外,我还提到了以下苹果之证件:https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/iPhoneAccessibility/Making_Application_Accessible/Making_Application_Accessible.html#//apple_ref/doc/uid/TP40008785-CH102-SW2

https://developer.apple.com/reference/uikit/uiview

但是,所有这些没有按”似乎解决了这个问题。任何帮助将不胜感激。

+0

我不知道答案,但是如果你看看报告日志,它会告诉你关于它为什么失败的细节。它甚至可能包括一个屏幕截图,显示当时的界面。 – matt

回答

1

这条线很脆弱。每次应用启动时,您的应用视图可能不会始终以相同的顺序显示,具体取决于竞赛条件,因此,记录会话中记录的内容不一定适用于应用的所有发布。您可能会发现代码实际上正在运行,但不会触及您所期望的元素。

为了让您的代码更加脆弱,请在应用程序代码中向UIView添加辅助功能标识符,然后使用otherElements查询来查找UIView。

// app code 
let view: UIView! 
view.accessibilityIdentifier = "myAccessibilityIdentifier" 

// UI test code 
app.otherElements["myAccessibilityIdentifier"].tap() 
+0

我也试过这个。它说: 未找到与“myAccessibilityIdentifier”匹配的项目其他 –

+0

此工作。其实,在我的情况下,我想要访问的UIView的父视图是将isAccessibilityElement设置为YES。所以它没有提前工作。 –