[英]How to check empty TextField in XCTest and XCUITest
I have to check if there is any UITextField
is empty or contains any value using XCUITest
or XCTest
.我必须使用
XCUITest
或XCTest
检查是否有任何UITextField
为空或包含任何值。 I was checking on many places and found textField.Value
can be used to find out the result.我检查了很多地方,发现
textField.Value
可以用来找出结果。 But for me the problem is textField.value
returns placeholder as value and hence it fails to detect empty field as empty.但对我来说,问题是
textField.value
返回占位符作为值,因此它无法将空字段检测为空。
You have two cases to figure out wheter the textfield is empty.您有两种情况可以确定文本字段是否为空。 First one - Use standart bool value of
yourTextField.text == nil
and second check for the instance to have string of ""
-> This should for instance occur when user would write some text and then delete it...第一个 - 使用
yourTextField.text == nil
标准 bool 值,然后第二次检查实例是否有字符串""
-> 例如,当用户写入一些文本然后将其删除时,就会发生这种情况...
So in code:所以在代码中:
// The second condition will occur only if the text is not nil, so
// it is okay to unwrap it like it.
XCTAssertTrue(textField.text == nil || textField.text!.isEmpty)
There is a solution which should work (apart from in one situation*).有一个应该可行的解决方案(除了在一种情况下*)。 XCUIElement has a property
placeholderValue
, which should contain the text field's placeholder text. XCUIElement 有一个属性
placeholderValue
,它应该包含文本字段的占位符文本。 So try this:所以试试这个:
let textfield = XCUIApplication().textFields["textFieldIdentifier"]
if let textFieldText = textField.value as? String,
textFieldText.isEmpty == false,
textFieldText != textField.placeholderValue {
// Do the thing that requires the text field *not* to be empty
}
* The one scenario where this won't work is if the text entered in the text field is the same as the placeholder's text. * 如果在文本字段中输入的文本与占位符的文本相同,则这将不起作用的一种情况。 Then we're in Failsville.
然后我们在Failsville。
However, as this is a UITest, I would think you should have some control over what gets entered into this text field.但是,由于这是一个 UITest,我认为您应该对输入到此文本字段中的内容进行一些控制。
I ran into this recently and as pointed out by @adamjansch for UITextField
the XCUIElement
will return the placeholder
value if it is set and there is no text in the text field.我最近遇到了这个问题,正如@adamjansch 针对
UITextField
指出的那样,如果 XCUIElement 已设置并且文本字段中没有文本,则XCUIElement
将返回placeholder
值。
Given that, I ended up doing something like this:鉴于此,我最终做了这样的事情:
let query = app.scrollViews.otherElements
let textField = query.textFields["textFieldIdentifier"]
if textField.value != nil,
let aValue = textField.value as? String,
let placeholderValue = textField.placeholderValue,
aValue != placeholderValue
{
// Do something with the text field; it has a value
// so perhaps it needs to be cleared before a new
// value is input.
textField.buttons["Clear text"].tap()
}
else
{
...
}
There is a workaround for your problem.您的问题有一个解决方法。 If you dont want to get the placeholder
如果你不想得到占位符
First click on the textfield首先点击文本框
textField.tap()
Get the value from the textfield element从 textfield 元素中获取值
let pageTitle = textField.value as? String var titleLength = pageTitle?.characters.count XCTAssertTrue(titleLength==0, "Title is empty")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.