2017-10-23 16 views
0

UI 자동화 설정에서 특정 조건을 기다리는 데 XCTWaiter를 사용하고 있습니다.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을 기다리는 경우에 잘 작동하지만 난 내가 설정하는 플래그를 사용하므로 완료하는 데 네트워크 요청을 대기하는 경우가 : 이것은 내 사용자 지정 WAITFOR 방법 네트워크 요청이 완료되면 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 
}