[英]XCTest fails to compare two strings
我正在寻找文本视图中参数中给出的文本。 我确信它存在,因为我在屏幕上看到了它,但不知何故比较它们失败了。
这是一个方法:
func checkText(_ text: String) {
for tv in app.textViews.allElementsBoundByIndex {
if let typedText = tv.value as? String {
print(typedText)
print(text)
print(typedText == text)
}
}
}
控制台输出:
This is a test.
This is a test.
false
我不知道这怎么可能。
我也尝试过这种方式:
if myTextView.exists {
if let typedText = myTextView.value as? String {
XCTAssertEqual(typedText, text, "The texts are not matching")
}
}
但它给出了一个错误,说文本不匹配,因为“这是一个测试”。 不等于“这是一个测试”。
当我转换你的最后 3 个字节时[239, 191, 188]
,使用
let array: [UInt8] = [239, 191, 188]
if let output = String(bytes: array, encoding: .utf8) {
print("-" + output + "-")
}
output 是:
--
这意味着表示这些字符串之一的最后一个字符代表一个额外的空格字符
也许您正在向文本添加不需要的空间,或者在您的XCUIElement
值中添加它。 这就是均衡操作返回 false 的原因
您的 function 似乎工作正常,“.” 两个文本之间不相同(检查https://www.diffchecker.com/diff )
正如大家在评论中所写,我的 textview 在完成编辑后添加了“对象替换字符”或“U+FFFC”。
为了使字符串相等,我需要这样做:
let trimmedTypedText = typedText.replacingOccurrences(of: "\u{fffc}", with: "")
所以现在这将成功:
if let typedText = myTextView.value as? String {
let trimmedTypedText = typedText.replacingOccurrences(of: "\u{fffc}", with: "")
XCTAssertEqual(trimmedTypedText, text, "The texts are not matching")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.