Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I assert that a text field is empty?

I have an empty text field on my UI, though it has a placeholder text (whose value is foo) set in the storyboard. In my UI test, I am trying to check that its text value starts out empty, but when I query it's value, it seems to be giving me the placeholder value instead:

func testTextFieldInitiallyEmpty {
  let input = XCUIApplication().textFields["My Text Field"]
  XCTAssertEqual(input.value as! String, "")
}

as the test fails with this message:

XCTAssertEqual failed: ("foo") is not equal to ("")

Of course, foo is the placeholder value, but it's not the text value of that text field. I would have expected that error message if I had written:

XCTAssertEqual(input.placeholderValue as! String, "")

input is a XCUIElement, which implements XCUIElementAttributes, so I don't see anything else that would do the trick here.

How do I check (assert) that the text field is empty?

storyboardattributes inspector

Edit

After doing some further research and trying out the suggestions below for using the input's properties of accessibilityValue, label, and title, I have not found any solution that will give me the text field's text value when there is text, and an empty string when only the placeholder is visible.

This seems like either (a) a bug, or (b) a questionable design decision from the test framework to not provide that ability. At a minimum, the documentation for XCUIElementAttributes#value seems inadequate to me, as the only detail is:

The exact type of value varies based on the type of the element.

Still looking for a better solution...

like image 269
akivajgordon Avatar asked Nov 18 '22 04:11

akivajgordon


1 Answers

You can compare to the XCUIElementAttributes's placeholderValue variable in addition to checking for a blank string

extension XCUIElement {
    func noTextEntered() -> Bool {
        return self.value as? String != "" && self.value as? String != placeholderValue
    }
}

Then you can run XCAssert(input.noTextEntered(), "Unexpected text entered into field")

Just make sure your placeholder is not something a user would type in. This way you don't have to hardcode placeholder values to check against

like image 79
Jason Avatar answered Nov 19 '22 22:11

Jason