【问题标题】:How to Assert TextView value in XCode7 iOS9?如何在 XCode7 iOS9 中断言 TextView 值?
【发布时间】:2015-09-09 07:12:00
【问题描述】:

我正在做一个 UI 单元测试。在图片里。
它们是 UILabel,除了长描述是 UITextView。
在我想要断言的页面中测试答案中的值。

对于 UILabels 中的答案很好。我可以关注Xcode UI Test example
该方法很容易理解,只需点击元素并将方法从.tap()更改为.exist(),然后用assert()括起来;

我的问题是UITextViewUILabel 更复杂。
如何获取UITextView 的值以便进行断言检查?

func testG(){

let app = XCUIApplication()
app.launch();
app.buttons["Enter"].tap()
app.tables.staticTexts["Bee"].tap()

assert(app.scrollViews.staticTexts["Name :"].exists);
assert(app.scrollViews.staticTexts["Age :"].exists);
assert(app.scrollViews.staticTexts["Specialty :"].exists);
assert(app.scrollViews.staticTexts["Description :"].exists);

assert(app.scrollViews.staticTexts["Bee"].exists);
assert(app.scrollViews.staticTexts["11"].exists);
assert(app.scrollViews.staticTexts["Sky Diver"].exists);
let text = "Bees are flying insects closely related to wasps and ants, known for their role in pollination and, in the case of the best-known bee species, the European honey bee, for producing honey and beeswax. Bees are a monophyletic lineage within the superfamily Apoidea, presently considered as a clade Anthophila. There are nearly 20,000 known species of bees in seven to nine recognized families,[1] though many are undescribed and the actual number is probably higher. They are found on every continent except Antarctica, in every habitat on the planet that contains insect-pollinated flowering plants.EOF";
assert(app.scrollViews.childrenMatchingType(.TextView).element.exists);
}

【问题讨论】:

  • 您是否尝试阅读valuemasilotti.com/xctest-documentation/Protocols/…
  • @dasdom。感谢你的回复。我根据您的回答设置了价值。现在我可以通过XCTAssertEqual(app.scrollViews.childrenMatchingType(.TextView).element.value as? String, text) 进行断言
  • 这应该被添加为这个问题的答案。它对我有用。

标签: ios xcode swift unit-testing xcode-ui-testing


【解决方案1】:

如果其他人发现这个问题,我可以通过使用获得UITextView 的文本

app.textViews.element.value as? String

这假设当时屏幕上只有一个文本视图。

【讨论】:

    【解决方案2】:
    XCTAssert(app.scrollViews.staticTexts[text].exists, "Didn't find the text!")
    

    不要相信你可以在这里获得价值,所以你只需要断言它就在那里。如果它是一个文本字段,那么获得value 将是一个很好的方法。

    【讨论】:

    • 哇。感谢你的回复。它看起来比我的干净。如果我再次发现类似我的问题的问题。我会按照你的回答。
    • 我试过这种方式,但对我不起作用。然而,上面 cmets 中的答案确实有效。上面的示例和我的用例之间的唯一区别是我的 UITextView 嵌入在 UIContainerView 中。
    猜你喜欢
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多