How to test UITextField in Xcode UI Testing when the text field is empty?

雨燕双飞 提交于 2019-12-06 00:15:10

There are a few ways to do this.

  1. If the textField has placeholder text you can use the placeholder text to find the textField.

  2. You can use the elementBoundByIndex method to get a textField at an index. i.e. If that is the only textField in the app at the time you can use

    XCUIApplication().textFields.elementBoundByIndex(0) 
    

to get the textField. (or whatever query you need to get that particular textField at that time)

  1. You can set an accessibility identifier in code or in the storyboard to find the textField.

It is nice to use option 2 before setting up accessibility identifiers, so long as your textField is always going to be findable with the same query. Also, option 1 will fail if multiple textFields have the same placeholder text at the same time, so option 2 is even more appealing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!