简体   繁体   English

处理位置服务警报的 UITest 案例

[英]UITest cases to handle with location services alert

I am writing UI test cases for my project.我正在为我的项目编写 UI 测试用例。

My project flow is as below:我的项目流程如下:

  • Login Screen.登录屏幕。 User enters credentials and press login.用户输入凭据并按登录。
  • Home Screen.主屏幕。 There is location requirement so system as for user's permission.对于用户的许可,系统有位置要求。 I allow it.我允许。
  • Logout.注销。

So when I do fresh install of application this flow is recorded in test case and works if I perform on new fresh build.因此,当我全新安装应用程序时,此流程会记录在测试用例中,并且如果我在新的全新构建上执行,则该流程会起作用。

But problem is when I test on old build there is no alert for location permission and the test's gets fail.但问题是当我在旧版本上进行测试时,没有位置许可警报,并且测试失败。 How can I handle this cases or ask user for permission every time when I run tests?每次运行测试时,如何处理这种情况或请求用户许可?

For resetting credentials of user I am passing launchArguments to XCUIApplication() and handle in AppDelegate.为了重置用户的凭据,我将XCUIApplication()传递给XCUIApplication()并在 AppDelegate 中处理。

I have implemented code let me know if its correct way :我已经实现了代码让我知道它是否正确

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

            return true
        }

The above code works for both if alert comes or not.无论警报是否出现,上面的代码都适用于两者。

Using an interruption monitor is the correct way.使用中断监视器是正确的方法。 However, it's safer to check if the alert being displayed is the alert you're expecting before you interact with the alert:但是,在与警报交互之前检查显示的警报是否是您期望的警报会更安全:

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
}

I use the following code to allow user's location:我使用以下代码来允许用户的位置:

    // 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
        }
    }

In this setup, I "register" the interruption monitor for tapping the allow button, so in this case I can dismiss that modal.在这个设置中,我“注册”了点击允许按钮的中断监视器,所以在这种情况下我可以关闭该模式。 Now, there's my test:现在,这是我的测试:

    // 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")
    }

In this test, simply I go to a view controller with a list sorted by location.在这个测试中,我只需转到一个带有按位置排序的列表的视图控制器。 First, I need to dismiss the location's system alert.首先,我需要关闭该位置的系统警报。 So, first I dismiss that modal and then I tap a cell from my TableView.因此,首先我关闭该模式,然后从 TableView 中点击一个单元格。 Then, I need to show it in my main view controller so I dismiss my view controller and I expect the same title.然后,我需要在我的主视图控制器中显示它,所以我关闭了我的视图控制器并且我希望有相同的标题。

Happy Coding!快乐编码!

Try this试试这个

    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