2016-10-30 57 views
0

我想在iOS應用程序中進行簡單的UI測試。我想用一些文字填寫textfield,但總會出現錯誤。在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") 

同樣的結果。 試用Connect Hardware Keyboard開啓和關閉。 試用iPhone 6s模擬器,iPad Retina模擬器,iPad 2模擬器。 只用Xcode 7試過(8將打破該項目)

想法?提前致謝!

回答

0

我沒有答案,爲什麼會發生這種情況,但對於任何未來有同樣問題的人來說,這是我的工作區,我正在處理這個問題。

基本的想法是讓鍵盤出現,然後打我需要建立我的字符串的每一個鍵。

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. 它不適用於特殊字符,在那裏你不能準確的找到它,而是用鍵名(「空格」而不是「」)。那麼,除了這個可怕的開關外,這裏沒有解決辦法,但是,我需要做的工作。

希望這可以幫助別人!