This is what I have so far and I am simply trying to get the result that is output in a label and test it against set results:
func testExample() {
let app = XCUIApplication()
let enterRomanNumeralsHereTextField = app.textFields["Enter roman numerals here"]
enterRomanNumeralsHereTextField.tap()
let convertButton = app.buttons["Convert"]
//1
enterRomanNumeralsHereTextField.typeText("LL")
convertButton.tap()
XCTAssertTrue(app.staticTexts.element(matching:.any, identifier: "Roman Numerals").label == "NotValidRomanNumber")
//2
enterRomanNumeralsHereTextField.typeText("LXXXIX")
convertButton.tap()
XCTAssertEqual(app.otherElements.containing(.staticText, identifier:"Roman Numerals").element.value as! String, "89")
//3
enterRomanNumeralsHereTextField.typeText("")
enterRomanNumeralsHereTextField.typeText("LXXVIII")
convertButton.tap()
XCTAssertEqual(app.otherElements.containing(.staticText, identifier:"Roman Numerals").element.value as! String, "")
// Use recording to get started writing UI tests.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
The first attempt(labeled 1) gives me a build error saying "cannot call value of non function type XCUIElement. The second attempt builds but the test fails because even though the label does indeed produce the right value, the test is reading the label as blank which brings us to my third attempt which passes the test because I compared it to a blank label, which is what its showing.
So as I said above I'm just wondering how exactly to retrieve the label value that is a result of a "calculation" or button press.
Unfortunately when a UILabel
is accessed in a UITest the XCUIElement
's value
property is not set with the text
property of the UILabel
. It is always empty.
XCUIElement
also has a label
property which you can use for your purpose. You only have to make sure that you are not setting the accessibilityLabel
on your UILabel
. Use accessibilityIdentifier
instead:
In your app:
label.accessibilityIdentifier = "Roman Numerals"
In your UITest:
XCTAssertEqual(app.staticTexts.element(matching:.any, identifier: "Roman Numerals").label, "89")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With