2017-10-23 15 views
0

UIオートメーション設定で特定の条件を待つためにXCTWaiterを使用しています。これは私のカスタムのwaitFor方法です:XCTWaiter予期せず発砲しない

// UITools.swift 
public class func waitFor(_ element:Any, timeout:UInt, clause:String) -> Bool 
{ 
    let predicate = NSPredicate(format: clause) 
    let expectation = testcase.expectation(for: predicate, evaluatedWith: element) 

    print("Waiting for \(element) to become \"\(clause)\" within \(timeout) seconds ...") 

    let result = XCTWaiter.wait(for: [expectation], timeout: TimeInterval(timeout)) 

    switch result 
    { 
     case .completed: 
      return true 
     case .invertedFulfillment: 
      print("waitFor result is in inverted fulfillment.") 
      return true 
     case .timedOut: 
      print("waitFor result is timed out.") 
     case .incorrectOrder: 
      print("waitFor result is in incorrect order.") 
     case .interrupted: 
      print("waitFor result is interrupted.") 
    } 

    return false 
} 

この方法は、私がXCUIElements待つが、私は私が完了するネットワーク要求を待つしたい場合がありますので、私はに設定されているフラグを使用する場合には正常に動作しますネットワーク要求が完了するとtrueになります。簡単な例を次に示します:

class Hub : NSObject 
{ 
    var isTestRailCasesRetrived = false 

    func retrieveTestRailCaseData() 
    { 
     isTestRailCasesRetrived = false 

     testrailClient.getTestCases() 
     { 
      (response:TestRailModel) in 
       // Do processing here ... 
       print("Found \(totalCases) TestRail cases for suite with ID \(suite.id).") 
       self.isTestRailCasesRetrived = true 
     } 

     UITools.waitFor(self, timeout: 30, clause: "isTestRailCasesRetrived == true") 
    } 
} 

ただし、XCTWaiterはタイムアウト後に完全なタイムアウトに達することはありません。この状況では、isTestRailCasesRetrivedは評価されないようです。なぜ誰かが知っていますか?

答えて

0

回避方法が見つかりました。このメソッドは、上記のコードがそうでなかったこの特定のケースで正常に機能します。この待機ループが終了するまで、XCUITestコードの実行をブロックします。

/// Ass-simple brute force wait method for when nothing else works. 
/// 
public class func waitUntil(timeout:Int = 30, evalblock:@escaping (() -> Bool)) 
{ 
    var count = 0 
    repeat 
    { 
     if count >= timeout || evalblock() == true 
     { 
      break 
     } 
     else 
     { 
      Darwin.sleep(1) 
     } 
     count += 1 
    } 
    while true 
} 
関連する問題