【问题标题】:XCTest in Xcode 7: how to verify some text exists in the viewXcode 7 中的 XCTest:如何验证视图中是否存在某些文本
【发布时间】:2016-07-07 18:43:07
【问题描述】:

我在 El Capitan (10.11) 上运行 Xcode 7。我正在为 XCUITest 使用新的记录和回放功能。我使用的是 Swift 而不是 Objective-C。这是我到目前为止生成的代码:

    func testButton2() {

    let app = XCUIApplication()
    app.tabBars.buttons["Location"].tap() //tab bar tap
    app.buttons["Button"].tap()  // button tap
    //now verify some text appears in this tab view

}

点击按钮后,我想验证一些文本是否出现在 UILabel 的同一视图中。

有什么想法吗?

我知道这里列出了一堆断言:

http://iosunittesting.com/xctest-assertions/

【问题讨论】:

  • 验证书本在哪里?在 UILabel 中?其他观点?

标签: xcode swift xctest


【解决方案1】:
func testButton2() {

   let app = XCUIApplication()
   app.tabBars.buttons["Location"].tap() //tab bar tap
   app.buttons["Button"].tap()  // button tap
   //now verify some text appears in this tab view
   XCTAssertEqual(app.staticTexts.elementBoundByIndex(0).label, "some text", "should be equal") }

【讨论】:

  • app.staticTexts.matchingIdentifier(<#T##identifier: String##String#>)
【解决方案2】:

假设您已正确设置标签参考:

func testButton2() {

    let app = XCUIApplication()
    app.tabBars.buttons["Location"].tap() //tab bar tap
    app.buttons["Button"].tap()  // button tap
    //now verify some text appears in this tab view
    XCTAssertEqual(myLabel.text, "some text", "should be equal")

}

【讨论】:

  • 我可以问一个愚蠢的问题吗?如何从 XCTestCase 类访问 ViewController 的 UILabel?
  • @lilredindy 由于时间关系,我现在无法给出完整的解释,所以我将带着介绍 UI 测试的 WWDC 视频离开。今天晚上我会试着回到这个话题。 developer.apple.com/videos/play/wwdc2015/406
猜你喜欢
  • 2015-11-28
  • 2018-03-04
  • 2019-11-18
  • 1970-01-01
  • 2013-04-20
  • 2020-09-24
  • 2021-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多