169

これは私の場合です:

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

UI テストの失敗 - 要素も子孫もキーボード フォーカスを持っていません。エレメント:

なにが問題ですか?これは正常textFieldsに機能していますが、問題が発生するのは のみsecureTextFieldsです。回避策はありますか?

4

27 に答える 27

341

この問題は私に苦痛の世界をもたらしましたが、適切な解決策を見つけることができました. シミュレーターで、I/O -> Keyboard -> Connect hardware keyboardがオフになっていることを確認します。

于 2015-12-04T18:34:04.883 に答える
20

私は完璧に機能する小さな拡張機能 (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))
        }
    }
}

主なアイデアは、キーボードが表示される前に要素 (テキスト フィールド) をタップし続けることです。

于 2016-01-15T10:29:52.170 に答える
12

スタニスラフは正しい考えを持っています。

チーム環境では、自動的に機能するものが必要です。こちらのブログで解決策を見つけました。

基本的には貼り付けるだけです:

UIPasteboard.generalPasteboard().string = "Their password"
let passwordSecureTextField = app.secureTextFields["password"]
passwordSecureTextField.pressForDuration(1.1)
app.menuItems["Paste"].tap()
于 2016-01-13T16:40:57.607 に答える
7

それは何度も私に起こりました。シミュレーターでキーボードハードウェアとOSXと同じレイアウトを無効にする必要があります

ハードウェア/キーボード (すべて無効)

その後、キーボードソフトウェアは閉じられず、テストはテキストを入力できます

ハードウェアを無効にする

于 2016-05-05T13:36:52.927 に答える
5

次のように、アプリを起動してからテキストフィールドにデータを入力するまでの間にスリープを使用します。

sleep(2)

私の場合、毎回このエラーが発生し続けていましたが、この解決策だけが役に立ちました。

于 2016-08-21T02:54:59.317 に答える
5
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()
}
于 2015-11-05T05:08:27.560 に答える
4

これが役立つかもしれません。エラーの前に「タップ」アクションを追加するだけです。それで全部です :)

[app.textFields[@"theTitle"] tap];
[app.textFields[@"theTitle"] typeText:@"kk"];
于 2015-10-06T19:39:31.163 に答える
3

[問題が解決したため、Bartłomiej Semańczykのコメントを回答として再投稿]

これを機能させるには、シミュレーターのメニューバーで [シミュレーター] > [内容と設定をリセット] を実行する必要がありました。

于 2015-11-28T23:19:08.517 に答える
2

シミュレーターUIがフォアグラウンドにあるときにUIテストを実行すると、上記の回答で解決しました(シミュレーターウィンドウで変更を確認できる場合)。ただし、バックグラウンドで実行している場合は機能しませんでした (たとえば、fastlane が実行した方法です)。

私はに行かなければなりませんでしたSimulator > Device > Erase All Content and Settings...

明確にするために、シミュレーター設定でハードウェアキーボードを手動で無効-有効-無効に設定し、defaults write com.apple.iphonesimulator ConnectHardwareKeyboard 0コンテンツと設定を消去しました。

于 2021-06-22T20:34:25.950 に答える
2

私はこの問題に遭遇し、 @AlexDenisovによって投稿されたソリューションを取得し、それをrun & testの事前アクションに追加することで、シナリオで修正することができました。

アクション前の実行スクリプト

于 2020-06-09T19:24:44.913 に答える
1

テキスト フィールドがテキスト フィールドとして実装されていないか、別の UI 要素にラップされており、簡単にアクセスできない場合があります。回避策は次のとおりです。

//XCUIApplication().scrollViews.otherElements.staticTexts["Email"] the locator for the element
RegistrationScreenStep1of2.emailTextField.tap()
let keys = app.keys
 keys["p"].tap() //type the keys that you need
 
 //If you stored your data somewhere and need to access that string //you can cats your string to an array and then pass the index //number to key[]
 
 let newUserEmail = Array(newPatient.email())
 let password = Array(newPatient.password)
 
 //When you cast your string to an array the elements of the array //are Character so you would need to cast them into string otherwise //Xcode will compain. 
 
 let keys = app.keys
     keys[String(newUserEmail[0])].tap()
     keys[String(newUserEmail[1])].tap()
     keys[String(newUserEmail[2])].tap()
     keys[String(newUserEmail[3])].tap()
     keys[String(newUserEmail[4])].tap()
     keys[String(newUserEmail[5])].tap()       

于 2018-06-22T18:48:44.960 に答える
0

私の問題はテッドの問題と同じでした。実際には、ログイン フィールドとハードウェア KB がオンの後にパスワード フィールドがタップされると、ソフトウェア キーボードは 2 回目のフィールド タップで自動的に閉じます。これは UI テストに固有のものではありません。

AppleScriptをいじった後、私が思いついたのは次のとおりです(改善は大歓迎です):

tell application "Simulator" activate tell application "System Events" try tell process "Simulator" tell menu bar 1 tell menu bar item "Hardware" tell menu "Hardware" tell menu item "Keyboard" tell menu "Keyboard" set menuItem to menu item "Connect Hardware Keyboard" tell menu item "Connect Hardware Keyboard" set checkboxStatus to value of attribute "AXMenuItemMarkChar" of menuItem if checkboxStatus is equal to "✓" then click end if end tell end tell end tell end tell end tell end tell end tell on error tell application "System Preferences" activate set securityPane to pane id "com.apple.preference.security" tell securityPane to reveal anchor "Privacy_Accessibility" display dialog "Xcode needs Universal access to disable hardware keyboard during tests(otherwise tests may fail because of focus issues)" end tell end try end tell end tell

上記のコードでスクリプト ファイルを作成し、必要なターゲットに追加します (おそらく UI テスト ターゲットのみ。開発中に HW キーボードを再度有効にするために、同様のスクリプトを開発ターゲットに追加することをお勧めします)。Run Scriptビルド フェーズにフェーズを 追加し、次のように使用する必要があります。osascript Path/To/Script/script_name.applescript

于 2016-04-14T03:37:46.000 に答える
0

accessibilityIdentifierサブビューを含むカスタム ビュー (UIStackViewサブクラス)の値を設定すると、同じエラーが発生しましたUIControl。その場合、XCTest は子孫要素のキーボード フォーカスを取得できませんでした。

accessibilityIdentifier私たちの解決策は、親ビューからを削除し、accessibilityIdentifier専用のプロパティを介してサブビューに を設定するだけでした。

于 2017-07-25T14:29:55.473 に答える