ios UI 测试失败 - 元素和任何后代都没有将键盘焦点放在 secureTextField 上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32184837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:23:13  来源:igfitidea点击:

UI Testing Failure - Neither element nor any descendant has keyboard focus on secureTextField

iosxcodeswiftios9xcode-ui-testing

提问by Bart?omiej Semańczyk

This is my case:

这是我的情况:

let passwordSecureTextField = app.secureTextFields["password"]
passwordSecureTextField.tap()
passwordSecureTextField.typeText("wrong_password") //here is an error

UI Testing Failure - Neither element nor any descendant has keyboard focus. Element:

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

What is wrong? This is working nice for normal textFields, but problem arise only with secureTextFields. Any workarounds?

怎么了?这对正常工作很好textFields,但问题只出现在secureTextFields. 任何解决方法?

回答by Ted Kaminski

This issue caused me a world of pain, but I've managed to figure out a proper solution. In the Simulator, make sure 'Hardware -> Keyboard -> Connect hardware keyboard' is off.

这个问题让我痛苦不堪,但我已经设法找到了一个合适的解决方案。在模拟器中,确保“硬件 -> 键盘 -> 连接硬件键盘”已关闭。

回答by AlexDenisov

Recently we found a hack to make solution from accepted answer persistent. To disable Simulator setting: 'Hardware -> Keyboard -> Connect hardware keyboard' from command line one should write:

最近,我们发现了一个 hack,可以使接受的答案持久化。要禁用模拟器设置:从命令行中的“硬件 -> 键盘 -> 连接硬件键盘”应该写:

defaults write com.apple.iphonesimulator ConnectHardwareKeyboard 0

It will not affect a simulator which is running - you need to restart simulator or start a new one to make that setting have its effect.

它不会影响正在运行的模拟器 - 您需要重新启动模拟器或启动一个新模拟器才能使该设置生效。

回答by berezhnyi oleksandr

I have written a small extension (Swift) which works perfect for me. Here is the code:

我写了一个小扩展(Swift),它非常适合我。这是代码:

extension XCTestCase {

    func tapElementAndWaitForKeyboardToAppear(element: XCUIElement) {
        let keyboard = XCUIApplication().keyboards.element
        while (true) {
            element.tap()
            if keyboard.exists {
                break;
            }
            NSRunLoop.currentRunLoop().runUntilDate(NSDate(timeIntervalSinceNow: 0.5))
        }
    }
}

The main idea is to keep tapping an element (text field) before the keyboard is presented.

主要思想是在键盘出现之前继续点击元素(文本字段)。

回答by RyanPliske

Stanislav has the right idea.

斯坦尼斯拉夫的想法是正确的。

In a team environment, you need something that will automatically work. I've come up with a fix hereon my blog.

在团队环境中,您需要一些可以自动工作的东西。我想出了一个修复这里在我的博客。

Basically you just paste:

基本上你只需粘贴:

UIPasteboard.generalPasteboard().string = "Their password"
let passwordSecureTextField = app.secureTextFields["password"]
passwordSecureTextField.pressForDuration(1.1)
app.menuItems["Paste"].tap()

回答by user3847320

Another cause of this error is if there is a parent view of the text field in which you are trying to enter text that is set as an accessibility element (view.isAccessibilityElement = true). In this case, XCTest is not able to get a handle on the subview to enter the text and returns the error.

此错误的另一个原因是,如果您尝试输入设置为辅助功能元素 ( view.isAccessibilityElement = true)的文本的文本字段的父视图。在这种情况下,XCTest 无法获取子视图的句柄以输入文本并返回错误。

UI Testing Failure - Neither element nor any descendant has keyboard focus.

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

It isn't that no element has focus (as you can often see the keyboard up and blinking cursor in the UITextField), it is just that no element it can reachhas focus. I ran into this when attempting to enter text in a UISearchBar. The search bar itself is not the text field, when setting it as an accessibility element, access to the underlying UITextField was blocked. To resolve this, searchBar.accessibilityIdentifier = "My Identifier"was set on the UISearchBarhowever the isAccessibilityElementwas not set to true. After this, test code of the form:

并不是没有元素具有焦点(因为您经常可以在 UITextField 中看到键盘向上和闪烁的光标),只是它可以到达的任何元素没有焦点。我在尝试在 UISearchBar 中输入文本时遇到了这个问题。搜索栏本身不是文本字段,当将其设置为辅助功能元素时,对底层 UITextField 的访问被阻止。为了解决这个问题,searchBar.accessibilityIdentifier = "My Identifier"是在设置UISearchBar但是isAccessibilityElement未设置为true。在此之后,测试表单的代码:

app.otherElements["My Identifier"].tap()
app.otherElements["My Identifier"].typeText("sample text")

Works

作品

回答by Fernando Martínez

It occurred with me for many times. You have to disable Keyboard Hardware and Same Layout as OSX in your Simulator

它在我身上发生了很多次。您必须在模拟器中禁用键盘硬件和与 OSX 相同的布局

Hardware/Keyboard (disable all)

硬件/键盘(全部禁用)

After that keyboard software won't dismiss and your tests can type text

之后键盘软件不会关闭,您的测试可以输入文本

Disable Hardware

禁用硬件

回答by Kingalione

Use a sleep between launching the app and typing in data in textfields like this:

在启动应用程序和在文本字段中输入数据之间使用睡眠,如下所示:

sleep(2)

In my case I was keeping getting this error every time and only this solution helped my out.

在我的情况下,我每次都不断收到此错误,只有这个解决方案帮助了我。

回答by Carlos Peralta

This maybe help: I just add a "tap" action before the error; that′s all :)

这可能有帮助:我只是在错误之前添加了一个“点击”操作;就这样 :)

[app.textFields[@"theTitle"] tap];
[app.textFields[@"theTitle"] typeText:@"kk"];

回答by Anthony

func pasteTextFieldText(app:XCUIApplication, element:XCUIElement, value:String, clearText:Bool) {
    // Get the password into the pasteboard buffer
    UIPasteboard.generalPasteboard().string = value

    // Bring up the popup menu on the password field
    element.tap()

    if clearText {
        element.buttons["Clear text"].tap()
    }

    element.doubleTap()

    // Tap the Paste button to input the password
    app.menuItems["Paste"].tap()
}

回答by rogueleaderr

[Reposting Bart?omiej Semańczyk's comment as an answer because it solved the problem for me]

[转发 Bart?omiej Semańczyk的评论作为答案,因为它为我解决了问题]

I needed to do Simulator > Reset Contents and Settings in the simulator menu bar to make this start working for me.

我需要在模拟器菜单栏中执行 Simulator > Reset Contents and Settings 以使其开始为我工作。