当文本字段为空时,如何在 Xcode UI 测试中测试 UITextField? [英] How to test UITextField in Xcode UI Testing when the text field is empty?

查看:77
本文介绍了当文本字段为空时,如何在 Xcode UI 测试中测试 UITextField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Xcode 中制作一个 UITest.当我记录我的测试时,它工作正常,但当我运行测试时,它给出了错误.

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.

这些是发生错误的代码行:

XCUIElement *clearTextTextField = [app.textFields containingType:XCUIElementTypeButton 
                                                      identifier:@"Clear text"].element;
[clearTextTextField typeText:@"sheffield"];

错误说

UI 测试失败 - 找不到文本字段的匹配项

UI Testing Failure - No matches found for textfield

我想这是因为我的文本字段没有任何初始字符串,并且测试人员找不到带有初始字符串 "Clear Text" 的文本框.

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".

我在 youtube 上观看了以下 教程.

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?

推荐答案

有几种方法可以做到这一点.

There are a few ways to do this.

  1. 如果 textField 有占位符文本,您可以使用占位符文本查找 textField.

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

您可以使用 elementBoundByIndex 方法在索引处获取 textField.即如果这是当时应用程序中唯一的 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 use

XCUIApplication().textFields.elementBoundByIndex(0) 

获取文本字段.(或您当时需要获取该特定文本字段的任何查询)

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

  1. 您可以在代码或故事板中设置可访问性标识符以查找 textField.

在设置可访问性标识符之前使用选项 2 很好,只要您的 textField 始终可以使用相同的查询找到.此外,如果多个 textField 同时具有相同的占位符文本,则选项 1 将失败,因此选项 2 更具吸引力.

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.

这篇关于当文本字段为空时,如何在 Xcode UI 测试中测试 UITextField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆