How to Assert TextView value in XCode7 iOS9?
Asked Answered
C

2

6

I am doing an UI Unit Testing. In the picture.
They are UILabels except long description which is UITextView.
In the page I want to do assert test the value in the answer.

For the answer in the UILabels are fine. I can follow Xcode UI Test example
The method is pretty easy to follow just tap on the element and change method from .tap() to .exist() Then enclose it by assert();

My problem is UITextView is more complicated than UILabel.
How can I get the value of UITextView in order to do assert check?

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);
}

UI with scrollbar

Cycle answered 9/9, 2015 at 7:12 Comment(3)
Did you try to read the value? masilotti.com/xctest-documentation/Protocols/…Shovel
@dasdom. Thank you for your reply. I put the value according to your answer. Now I can do assertion by XCTAssertEqual(app.scrollViews.childrenMatchingType(.TextView).element.value as? String, text)Cycle
This should be added as an answer to this question. It worked for me.Kristakristal
H
3

In case anyone else finds this question, I am able to get a UITextView's text by using

app.textViews.element.value as? String

This assumes there's only one text view on the screen at the time.

Hepcat answered 2/3, 2016 at 21:28 Comment(0)
K
0
XCTAssert(app.scrollViews.staticTexts[text].exists, "Didn't find the text!")

Don't believe you can get the value here so you'll just have to assert that it's there. If it was a text field then yea getting the value would be a great way of doing it.

Kalli answered 26/10, 2015 at 22:7 Comment(2)
Wow. Thank you for your reply. It looks cleaner than mine. If I have found a problem like my question again. I will follow your answer.Cycle
I tried this way and it did not work for me. The answer in the comments above, however, does work. The only difference between the example above and my use case is that my UITextView is embedded in a UIContainerView.Kristakristal

© 2022 - 2024 — McMap. All rights reserved.