簡體   English   中英

處理位置服務警報的 UITest 案例

[英]UITest cases to handle with location services alert

我正在為我的項目編寫 UI 測試用例。

我的項目流程如下:

  • 登錄屏幕。 用戶輸入憑據並按登錄。
  • 主屏幕。 對於用戶的許可,系統有位置要求。 我允許。
  • 注銷。

因此,當我全新安裝應用程序時,此流程會記錄在測試用例中,並且如果我在新的全新構建上執行,則該流程會起作用。

但問題是當我在舊版本上進行測試時,沒有位置許可警報,並且測試失敗。 每次運行測試時,如何處理這種情況或請求用戶許可?

為了重置用戶的憑據,我將XCUIApplication()傳遞給XCUIApplication()並在 AppDelegate 中處理。

我已經實現了代碼讓我知道它是否正確

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

            return true
        }

無論警報是否出現,上面的代碼都適用於兩者。

使用中斷監視器是正確的方法。 但是,在與警報交互之前檢查顯示的警報是否是您期望的警報會更安全:

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
}

我使用以下代碼來允許用戶的位置:

    // 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 中點擊一個單元格。 然后,我需要在我的主視圖控制器中顯示它,所以我關閉了我的視圖控制器並且我希望有相同的標題。

快樂編碼!

試試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM