【问题标题】:UITest cases to handle with location services alert使用位置服务警报处理的 UITest 案例
【发布时间】:2018-07-03 03:58:58
【问题描述】:

我正在为我的项目编写 UI 测试用例。

我的项目流程如下:

  • 登录屏幕。用户输入凭据并按登录。
  • 主屏幕。有位置要求,因此系统为用户许可。我允许。
  • 退出。

因此,当我重新安装应用程序时,此流程会记录在测试用例中,并且如果我在新的全新构建上执行,则可以工作。

但问题是当我在旧版本上进行测试时,没有位置许可警报并且测试失败。 如何处理这种情况或每次运行测试时都向用户征求许可?

为了重置用户的凭据,我将 launchArguments 传递给 XCUIApplication() 并在 AppDelegate 中处理。

我已经实现了代码让我知道它是否正确

addUIInterruptionMonitor(withDescription: "Allow “APP” to access your location?") { (alert) -> Bool in
            alert.buttons["Only While Using the App"].tap()

            return true
        }

上面的代码不管有没有警报都适用。

【问题讨论】:

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


    【解决方案1】:

    试试这个

        let app2 = XCUIApplication(bundleIdentifier: "com.apple.springboard")
        let button = app2.alerts.firstMatch.buttons["Allow While Using App"]
        button.waitForExistence(timeout: 10)
        button.tap()
    

    【讨论】:

      【解决方案2】:

      我使用以下代码来允许用户的位置:

          // MARK: - Setup
          
          override func setUp() {
              super.setUp()
              continueAfterFailure = false
              app = XCUIApplication()
              app.launch()
              addUIInterruptionMonitor(withDescription: "System Dialog") { (alert) -> Bool in
                  alert.buttons["Allow Once"].tap()
                  return true
              }
          }
      

      在此设置中,我“注册”了用于点击允许按钮的中断监视器,因此在这种情况下我可以关闭该模式。现在,这是我的测试:

          // MARK: - Test change mall
          
          func testChangeMall() {
              let selectorChangeButton = app.buttons["change_mall_button"]
              XCTAssert(selectorChangeButton.exists, "Selector change button does not exist")
              selectorChangeButton.tap()
              app.navigationBars.firstMatch.tap()
              let cell = app.staticTexts["Shopping Centre"]
              XCTAssert(cell.exists, "There's no cell with this title")
              cell.tap()
              sleep(1)
              let label = app.staticTexts["Shopping Centre"]
              XCTAssert(label.exists, "Nothing changes")
          }
      

      在此测试中,我只需转到一个视图控制器,其中包含按位置排序的列表。首先,我需要关闭该位置的系统警报。所以,首先我关闭该模式,然后从我的 TableView 中点击一个单元格。然后,我需要在我的主视图控制器中显示它,所以我关闭了我的视图控制器,我希望得到相同的标题。

      编码愉快!

      【讨论】:

        【解决方案3】:

        使用中断监视器是正确的方法。但是,在与警报交互之前检查正在显示的警报是否是您期望的警报会更安全:

        addUIInterruptionMonitor(withDescription: "Allow “APP” to access your location?") { (alert) -> Bool in
            let button = alert.buttons["Only While Using the App"]
            if button.exists {
                button.tap()
                return true // The alert was handled
            }
        
            return false // The alert was not handled
        }
        

        【讨论】:

        • 直到我在此之后添加 app.tap() 才对我有用。然后它就像一个魅力。
        猜你喜欢
        • 1970-01-01
        • 2015-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-14
        • 1970-01-01
        • 2019-07-11
        • 1970-01-01
        相关资源
        最近更新 更多