Tapping system alert using EarlGrey 2.0 & Swift

536 Views Asked by At

I'm trying to create UI tests for my app using EarlGrey 2.0 framework while using Swift language for those tests. However, I can't find a solution for tapping on a system alert, although EG 2.0 should support them. To be more specific, it is the native location iOS permission dialog when the app launches. Has anyone done this already? Any ideas? Thank you.

2

There are 2 best solutions below

0
redearz On BEST ANSWER

Here's the full code of a test accepting native system alert in Swift

func testExample() {
    let app = XCUIApplication()
    app.launch()
    XCTAssertTrue(grey_wait(forAlertVisibility: true, withTimeout: 2))
    XCTAssertTrue(grey_acceptSystemDialogWithError(nil))
    XCTAssertTrue(grey_wait(forAlertVisibility: false, withTimeout: 1))    
}
0
trishcode On

Here is an extension I used to check if a system alert is shown, and accept it if it is.

import EarlGrey

extension UITestCase {
    public func acceptSystemAlertIfPresent() {
        let systemAlertShown = grey_wait(forAlertVisibility: true, withTimeout: 2)
        if systemAlertShown {
            grey_acceptSystemDialogWithError(nil)
        }
    }
}