问题
I am making a UITest
in Xcode. When I record my test it works fine but when I run the test, it gives an error.
These are the lines of code where the error occurs:
XCUIElement *clearTextTextField = [app.textFields containingType:XCUIElementTypeButton
identifier:@"Clear text"].element;
[clearTextTextField typeText:@"sheffield"];
The error says
UI Testing Failure - No matches found for textfield
I imagine this is because my Text Field does not have any initial string and the tester cannot find a text box with initial string "Clear Text"
.
I watched the following tutorial on youtube.
The demonstrators code turns out fine because his textfield has an initial string. (At least that is what I think the problem is)
Is there a way to make the test work for empty text fields?
回答1:
There are a few ways to do this.
If the textField has placeholder text you can use the placeholder text to find the textField.
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 useXCUIApplication().textFields.elementBoundByIndex(0)
to get the textField. (or whatever query you need to get that particular textField at that time)
- 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.
来源:https://stackoverflow.com/questions/34658482/how-to-test-uitextfield-in-xcode-ui-testing-when-the-text-field-is-empty