2016-04-22 99 views
2

在我的項目中使用Xcode啓動UI測試,碰到路障。Xcode UI測試如何在自定義類文本字段中輸入文本

有一個叫RSCustomTextFieldUITextField子類,有一些UI自定義。它被添加到RSSearchHeadView,其中包含文本字段和按鈕。

所以我不是能夠查詢文本框爲app.textfields["Search field"]因爲它的自定義類,它顯示爲app.otherElements["Search field"]

它崩潰的searchField.typeText("abc")顯示錯誤無論元素也沒有任何後代具有鍵盤焦點。

即使在tap()的元素。請注意,我也嘗試在子類和文本框上設置輔助功能特性爲UIAccessibilityTraitSearchField,但沒有區別。

func testSearchIcon() { 

    let searchField = app.otherElements["Search field"]  
    searchField.tap() 

    searchField.typeText("abc") 
    app.keys["delete"].tap() 

    XCTAssertFalse(searchDubizzleElement.hittable) 
} 

印刷debugDescription上的應用程序提供了以下的輸出:

Element subtree: 
→Application 0x7fc1b3f3aa00: {{0.0, 0.0}, {375.0, 667.0}}, label: 'myappname-masked' 
    Window 0x7fc1b3f439b0: Main Window, {{0.0, 0.0}, {375.0, 667.0}} 
     Other 0x7fc1b3f55c20: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}} 
     Other 0x7fc1b3f43440: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}} 
      Other 0x7fc1b3f31830: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}} 
      Other 0x7fc1b3f61ff0: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}} 
       Other 0x7fc1b3f52820: traits: 8589934592, {{0.0, 0.0}, {375.0, 64.0}} 
       Other 0x7fc1b3f4e190: traits: 8589934592, {{0.0, 0.0}, {375.0, 64.0}} 
        Button 0x7fc1b3f3a250: traits: 8589934593, {{5.0, 20.0}, {44.0, 44.0}}, label: 'search icon right' 
        Other 0x7fc1b3f349b0: traits: 8589935616, {{15.0, 20.0}, {345.0, 39.0}}, label: 'Search field' 
        Image 0x7fc1b3f3b3b0: traits: 8589934596, {{139.0, 22.0}, {97.0, 30.0}}, identifier: 'logo' 
        Button 0x7fc1b3f537a0: traits: 8589934593, {{326.0, 20.0}, {44.0, 44.0}}, label: 'your account icon' 
       CollectionView 0x7fc1b3f32450: traits: 35192962023424, {{0.0, 64.0}, {375.0, 603.0}} 
       Cell 0x7fc1b3f1e230: traits: 8589934592, {{0.0, 64.0}, {375.0, 50.0}} 

...... 
+0

我只是試圖子類的UITextField將其添加到故事板和'debugDescription'顯示元素是一個文本字段。 RSCustomTextField真的只是UITextField的一個子類嗎? –

+0

你嘗試過「app.searchFields.element」嗎? – Che

+0

@TomasCamin你是對的。它沒有直接添加到視圖控制器。它位於另一個名爲'RSSearchHeadView'的自定義視圖內,該視圖只保存'textField'自定義類的按鈕。我已經更新了這個問題。我的錯。任何幫助? – carbonr

回答

0

我跑進與的UITextField的自定義子類相同的錯誤。問題的根源竟然是在UITextField上插入leftView。關於使用這些子視圖屬性的某些內容會遺漏自動UI測試的可訪問性訪問。

要解決這個問題,我避免在UITextFields使用leftViewrightView。相反,我感到作爲一個答案(目前是第二個最upvoted)描述修改textRect方法實現所期望的壓痕位置:Set padding for UITextField with UITextBorderStyleNone

我無法從你的代碼告訴你的問題是否是相同的,但思想我會分享這個小小的發現。