我正在尋找文本視圖中引數中給出的文本。我確信它存在,因為我在螢屏上看到它,但是以某種方式比較它們失敗了。
這是一個方法:
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")
}
}
但它給出了一個錯誤,說文本不匹配,因為“這是一個測驗”。不等于“這是一個測驗”。
uj5u.com熱心網友回復:
當我轉換你的最后 3 個位元組時[239, 191, 188],使用
let array: [UInt8] = [239, 191, 188]
if let output = String(bytes: array, encoding: .utf8) {
print("-" output "-")
}
輸出是:
-?-
這意味著表明這些字串之一的最后一個字符代表一個額外的空格字符
也許您正在向文本添加不需要的空間,或者在您的XCUIElement值中添加它。這就是均衡操作回傳 false 的原因
uj5u.com熱心網友回復:
您的功能似乎正常作業,“。” 兩個文本之間不一樣(檢查https://www.diffchecker.com/diff)
uj5u.com熱心網友回復:
正如人們在評論中所寫,我的文本視圖在完成編輯后添加了“物件替換字符”或“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")
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/454096.html
標籤:IOS 迅速 uitextview 测试 xcuitest
