2015-11-03 14 views
7

Sto scrivendo UITests in xCode 7.1 e ho un problema nel testare gli avvisi (Consenti notifiche nel mio caso). Durante la creazione di xCode test scrive questo codice:UITest degli avvisi in xCode 7.1

app.alerts["\U201cAppName\U201d Would Like to Send You Notifications"].collectionViews.buttons["OK"].tap() 

che provoca immediatamente errore:

Invalid escape sequence in literal

Così ho sostituito il codice di xCode con:

app.alerts["\u{201c}AppName\u{201d} Would Like to Send You Notifications"].collectionViews.buttons["OK"].tap() 

Ma quando ho eseguito UITest fallisce con messaggio:

UI Testing Failure - No matches found for Alert

Lo stesso per il codice

app.alerts["“AppName” Would Like to Send You Notifications"].collectionViews.buttons["OK"].tap() 

Ho anche provato

app.alerts.collectionViews.buttons["OK"].tap() 

come persone consigliato here, ma stessa storia ...

Credo che molte persone di fronte ad una questione durante UITesting in xCode 7.1

Per favore, condividi la tua esperienza o suggerimenti per la soluzione. Grazie in anticipo!

+0

La registrazione "sbagliato" della Unicode caratteri è oggetto di rdar: // 23.493.343. Sentiti libero di duplicare questo problema. – Tobias

risposta

6

vedi esempio sotto

import XCTest 

let systemAlertHandlerDescription = "systemAlertHandlerDescription" 

class LoginPerformingTestCase: XCTestCase { 

var systemAlertMonitorToken: NSObjectProtocol? = nil 

override func setUp() { 
    continueAfterFailure = false 

    let app = XCUIApplication() 
    app.launchArguments = [TestingEnvironment.resetLaunchArgument, TestingEnvironment.testingEnvironmentArgument] 
    app.launch() 

    systemAlertMonitorToken = addUIInterruptionMonitorWithDescription(systemAlertHandlerDescription) { (alert) -> Bool in 
     if alert.buttons.matchingIdentifier("OK").count > 0 { 
      alert.buttons["OK"].tap() 
      return true 
     } else { 
      return false 
     } 
    } 
} 

override func tearDown() { 
    if let systemAlertMonitorToken = self.systemAlertMonitorToken { 
     removeUIInterruptionMonitor(systemAlertMonitorToken) 
    } 

    super.tearDown() 
} 

func loginWithApp(app: XCUIApplication) { 
    let signInButton = app.buttons["SIGN IN"] 
    signInButton.tap() 
    let emailAdressTextField = app.textFields.matchingIdentifier("EmailAddress").elementBoundByIndex(0) 
    emailAdressTextField.tap() 
    emailAdressTextField.typeText("[email protected]") 

    let passwordSecureTextField = app.secureTextFields["Password"] 
    passwordSecureTextField.tap() 
    passwordSecureTextField.typeText("1111") 
    signInButton.tap() 

    let exists = NSPredicate(format: "exists == 1") 
    let iconAlarmButton = app.buttons["icon alarm"] 

    let expectation = expectationForPredicate(exists, evaluatedWithObject: iconAlarmButton, handler: nil) 
    waitForExpectationsWithTimeout(60) { (error) -> Void in 
     if let _ = error { 
      expectation.fulfill() 
     } 
    } 

    app.tap()//workaround to hide system alert 
    let darkNavigaitonBar = app.otherElements.matchingIdentifier("darkNavigationView").elementBoundByIndex(0) 
    if darkNavigaitonBar.hittable == true { 
     app.tap() 
    } 

} 

} 
+0

Grazie per la risposta. Ha aiutato, buon approccio! Che cos'è TestingEnvironment in app.launchArguments []? Xcode non lo riconosce (errore: utilizzo dell'identificatore non risolto 'TestingEnvironment'). –

+0

Basta rimuoverlo. –

2

Ecco un esempio di come si fa con un app che richiede locale Accessibile permesso di notifica:

addUIInterruptionMonitorWithDescription("Local Dialog") { (alert) -> Bool in 
    if alert.collectionViews.buttons["OK"].exists { 
      alert.collectionViews.buttons["OK"].tap() 
      return true 
    } 
    return false 
} 
Problemi correlati