ios - 使用 Xcode 中的 UI 测试在 UIWebView 中加载的网页中键入文本时出现问题

标签 ios swift xctest xcode-ui-testing

我正在努力在嵌入 native UIViewController 中的 UIWebView 中键入文本。我在 WebView 中加载了谷歌,我只是想输入“Hello”。录音建议使用 app.typeText("Hello") 但我不断收到错误

UI 测试失败 - 元素和任何后代都没有键盘焦点。

有什么建议吗?

显示问题的项目,请通过从终端运行来检查此源来测试您可能的答案: git clone <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="94f3fde0d4f6fde0f6e1f7fff1e0bafbe6f3" rel="noreferrer noopener nofollow">[email protected]</a>:Smaljaars/uitestingexample.git

更新我找到了一种在 Web View 内的文本字段中输入文本的解决方法:

func testWorkAroundForProblem() {

    let app = XCUIApplication()
    app.otherElements["Zoek"].tap() //this is language dependent! (no accessibility identifiers within webview)

    // UGLY WORKAROUND FOR THE PROBLEM
    let textToType = "XCTest slow search Google"
    for character in textToType.characters {
        if character == " " {
            app.descendants(matching: .any)["space"].tap()
            continue
        }
        app.descendants(matching: .any)[character.description.lowercased()].tap()
    }

    app.webViews.buttons["Google zoeken"].tap() //this is language dependent! (no accessibility identifiers within webview)
}

最佳答案

首先确保搜索框已聚焦。辅助功能检查器显示您应该能够通过“搜索”访问它。奇怪的是,Google 将该字段标记为组合框。

let app = XCUIApplication()
app.comboBoxes["Search"].tap()
app.typeText("Hello")

Accessibility Inspector for Google.com

关于ios - 使用 Xcode 中的 UI 测试在 UIWebView 中加载的网页中键入文本时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37349190/

相关文章:

ios - 如何在 SpriteKit/Objective C 中添加 "Mute Sound"按钮?

ios - UITableViewCell 的条件背景色

Swift 闭包 "Type ' Int ?' is not optional, value can never be nil"

ios - 我如何断言在我的单元测试中调用了委托(delegate)

ios - XCUITest 测试执行顺序

ios - 如何在 SCLAlertView showWaiting 警报后重定向到主页?

ios - Visual Studio 需要凭据才能在 Mac 上访问 pkgs.dev.azure.com

swift - 如何将 xib 中的自定义 View 添加到 UIScrollView?

iOS XCTestCase 启动、执行一些操作、终止应用程序并重新启动应用程序

ios - "XXX-Swift.h"测试项目中找不到文件