【发布时间】:2015-07-20 16:43:55
【问题描述】:
我对苹果在其 XCode7 Beta 中发布的新 UI 单元测试方案感到有些困惑。我认为这是一个很棒的主意,但我有几个问题。
这是我有的一种测试方法...
func testMetricsProperties() {
// Used some of the metrics for testing for reference
let app = XCUIApplication()
app.scrollViews.descendantsMatchingType(.Unknown).containingType(.StaticText, identifier:"rim").childrenMatchingType(.Button).element.tap()
app.textFields["_XCUI:Secure"].typeText("")
app.typeText("\r")
app.buttons["dash metrics"].tap()
let element = app.descendantsMatchingType(.Unknown).containingType(.Image, identifier:"darkBackground.png").childrenMatchingType(.Unknown).element.childrenMatchingType(.Unknown).elementBoundByIndex(1).childrenMatchingType(.Unknown).element.childrenMatchingType(.Unknown).element
let offPlanRevenue = element.childrenMatchingType(.Unknown).elementBoundByIndex(0).staticTexts["OFF PLAN REVENUE"]
offPlanRevenue.tap()
XCTAssert(offPlanRevenue.exists);
XCTAssertEqual(offPlanRevenue.value as! String, "");
}
但是,在接下来的测试方法中,似乎我必须重新加载整个应用程序,
let app = XCUIApplication()
app.scrollViews.descendantsMatchingType(.Unknown).containingType(.StaticText, identifier:"im").childrenMatchingType(.Button).element.tap()
app.textFields["_XCUI:Secure"].typeText("")
app.typeText("\r")
app.buttons["dash metrics"].tap()
}
无论如何我可以避免这种情况吗?如果我尝试对整个套件进行完整测试,这可能会很麻烦。
【问题讨论】:
-
我打赌你也能解决这个问题stackoverflow.com/questions/31534903/…
-
我建议您使用方法,例如创建一个对象
mainController并添加方法openMetrics和tapRevenue。然后,您的代码将类似于mainController.openMetrics() mainController.tapRevenue() XCTAssert(...)。你会发现一切都会变得更易读、更简单。 -
有没有比在另一个类中创建辅助方法更有效的方法?
标签: ios xcode7 ui-testing xcode-ui-testing