ios - 在 UI 测试中填充 TextField 失败

标签 ios xcode xcode7 xcode-ui-testing

我正在尝试在我的 iOS 应用程序中进行简单的 UI 测试。我想用一些文本填充textfield,但总是出现错误。

第一次尝试:

let searchField = app.textFields.elementBoundByIndex(0);
searchField.tap()
searchField.typeText("Holy Grail")

该字段突出显示,键盘出现,几秒钟后,随机出现以下之一:

- Timed out waiting for IDE barrier message to complete
- failed to get attributes within 15.0s ui test
- failed to get snaphots within 15.0s ui test

第二次尝试:

func setText(text: String, application: XCUIApplication) {
    //Instead of typing the text, it pastes the given text.
    UIPasteboard.generalPasteboard().string = text
    doubleTap()
    application.menuItems["Paste"].tap()
}

...

let searchField = app.textFields.elementBoundByIndex(0);
searchField.tap()
searchField.setText("Holy Grail")

相同的结果。 尝试打开和关闭连接硬件键盘。 尝试过 iPhone 6s 模拟器、iPad Retina 模拟器、iPad 2 模拟器。 仅尝试使用 Xcode 7(8 将破坏项目)

想法?提前致谢!

最佳答案

我不知道为什么会发生这种情况,但对于任何 future 遇到我同样问题的人来说,这是我正在研究的解决方法。

基本想法是让键盘出现,然后按下构建字符串所需的每个键。

func testPlayground() {

    let app = XCUIApplication()
    waitAndTap(app.textFields["MyTextField"])
    type(app, text: "hej")

}

func waitAndTap(elm: XCUIElement){
    let exists = NSPredicate(format: "exists == true")
    expectationForPredicate(exists, evaluatedWithObject: elm, handler: nil)
    waitForExpectationsWithTimeout(10.0, handler: nil)
    elm.tap()
}


func type(app: XCUIApplication, text : String){
    //Wait for the keyboard to appear.
    let k = app.keyboards;
    waitForContent(k, time: 10.0)

    //Capitalize the string.
    var s = text.lowercaseString;
    s.replaceRange(s.startIndex...s.startIndex, with: String(s[s.startIndex]).capitalizedString)

    //For each char I type the corrispondant key.
    var key: XCUIElement;

    for i in s.characters {
        if "0"..."9" ~= i ||  "a"..."z" ~= i || "A"..."Z" ~= i {
            // Then it's alphanumeric!
            key = app.keys[String(i)];
        }
        else {
            // Then is special character and is necessary re-map them.
            switch i {
            case "\n":
                key = app.keys["Next:"]; // This is to generalize A LOT.
            default:
                key = app.keys["space"];
            }

        }

        waitAndTap(key);
    }

所以:waitAndTap() 是我在测试中经常使用的函数。它等待一个元素的存在并点击它。如果十秒后仍未显示,则测试失败。

testPlayground() 点击我想要写入的文本字段,然后调用 type()

type() 是核心。基本上,查看字符串中的每个字符并点击它。问题:

  1. 如果我在未激活 Shift 的键盘中查找字符 H,它将永远找不到大写字母。我的解决方案是将字符串大写,但它仅在特定情况下有效。这是可以增强的

  2. 它不适用于特殊字符,您无法准确查找它,但可以查找键的名称(“空格”而不是“”)。好吧,除了那个可怕的开关盒之外,这里没有解决方案,但是好吧,对于我需要做的事情来说,它是有效的。

希望这可以帮助别人!

关于ios - 在 UI 测试中填充 TextField 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40332258/

相关文章:

ios - 如何自定义 UITableView 水平方向滚动?

ios - 解析 beforeSave 删除用户之前的帖子

ios - Xcode 构建错误,arm-apple-darwin11-gcc-4.2.1 execvp : No such file or directory

ios - 如何使用UIBezierpath获取用于绘图的View(Drawing View)的截图

ios - Xcode 7 : WatchOS storyboards do not support target device type 'iphone'

unit-testing - Xcode 7测试未运行,但报告成功

ios - react native 渲染 native 自定义 View

xcode - 使用OpenCV仅检测圆

ios - 尝试使用 FileManager 移动项目时出错

ios - @IBInspectable 的 UIColors 列表?