【问题标题】:XCTest waitForExpectations(...) waiting multiple resultsXCTest waitForExpectations(...) 等待多个结果
【发布时间】:2017-08-14 22:27:27
【问题描述】:

在 UITest 中,点击登录按钮后,我需要等待两个事件,登录成功后是否显示更新或主屏幕并决定要做什么。 文档说,

只有一个 -waitForExpectationsWithTimeout:handler: 可以在任何给定时间处于活动状态,但是 * {期望 -> 等待}的多个离散序列可以链接在一起。

所以需要解决一些问题,但最终想通了。

【问题讨论】:

    标签: ios xctest xcode-ui-testing ui-testing


    【解决方案1】:

    您可以向谓词添加参数并在条件之间使用 OR。

    let skipUpdateButtonPredicate = NSPredicate(format: "%@.exists == 1 OR %@.exists == 1", skipUpdateButton, homeTabBar)
    self.expectation(for: skipUpdateButtonPredicate, evaluatedWith: [Any](), handler: nil)
    testCase.waitForExpectations(timeout: Constants.loginTimeout,
                                     handler: nil)
    

    然后你可以在这里分支,如果有家庭或更新:

    if skipUpdateButton.exists {
        skipUpdateButton.tap()
    }
    

    【讨论】:

      【解决方案2】:

      使用期望时,您可以设置需要实现的次数。

      let expectations = expectation(description: "AsyncExpectations")
      expectations.expectedFulfillmentCount = 4
      
      
          DispatchQueue.global(qos: .userInteractive).async {
          // do work here
          expectations.fulfill()
      }
      
      
      DispatchQueue.global(qos: .background).async {
          // do work here
          expectations.fulfill()
      }
      
      DispatchQueue.global(qos: .default).async {
          // do work here
          expectations.fulfill()
      }
      
      DispatchQueue.main.async {
          // do work here
          expectations.fulfill()
      }
      
      // wait for all 4 expectations
      waitForExpectations(timeout: 2, handler: nil)
      

      【讨论】:

        猜你喜欢
        • 2018-04-05
        • 2014-05-11
        • 2016-11-13
        • 2021-03-17
        • 2019-10-07
        • 1970-01-01
        • 2020-06-18
        • 2019-05-09
        • 1970-01-01
        相关资源
        最近更新 更多