2016-04-21 9 views
4

Ich möchte einen UI-Test in Xcode für eine Anmeldung mit FBDSKLoginKit schreiben.Wie schreibe ich UI-Tests für die Anmeldung mit Facebook in Xcode?

Allerdings wird Facebook iOS SDK uses SFSafariViewController dem Benutzer angezeigt, um sie zu authentifizieren und leider there's no way how to interact with SFSafariViewController in UI tests in Xcode 7.

Haben Sie Ideen, wie Sie die Facebook-Anmeldung testen können, ohne mit SFSafariViewController zu interagieren?

+0

Sie können einen Blick auf meine Notizen https://github.com/onmyway133/blog/issues/44 – onmyway133

Antwort

1

Swift 3 Xcode 8 Update

func testFacebookLogin() { 
    let app = XCUIApplication() 

    // set up an expectation predicate to test whether elements exist 
    let exists = NSPredicate(format: "exists == true") 

    // as soon as your sign in button shows up, press it 
    let facebookSignInButton = app.buttons["Login With Facebook"] 
    expectation(for: exists, evaluatedWith: facebookSignInButton, handler: nil) 
    facebookSignInButton.tap() 

    // wait for the "Confirm" title at the top of facebook's sign in screen 
    let confirmTitle = app.staticTexts["Confirm"] 
    expectation(for: exists, evaluatedWith: confirmTitle, handler: nil) 

    // create a reference to the button through the webView and press it 
    let webView = app.descendants(matching: .webView) 
    webView.buttons["OK"].tap() 

    // wait for your app to return and test for expected behavior here 
    self.waitForExpectations(timeout: 10, handler: nil)   
} 

extension:

extension XCUIElement { 
    func forceTap() { 
     if self.isHittable { 
      self.tap() 
     } else { 
      let coordinate: XCUICoordinate = self.coordinate(withNormalizedOffset: .zero) 
      coordinate.tap() 
     } 
    } 

Swift 2

func testFacebookLogin() 
{ 
    let app = XCUIApplication() 

    // set up an expectation predicate to test whether elements exist 
    let exists = NSPredicate(format: "exists == true") 

    // as soon as your sign in button shows up, press it 
    let facebookSignInButton = app.buttons["Sign in with Facebook"] 
    expectationForPredicate(exists, evaluatedWithObject: facebookSignInButton, handler: nil) 
    facebookSignInButton.tap() 

    // wait for the "Confirm" title at the top of facebook's sign in screen 
    let confirmTitle = app.staticTexts["Confirm"] 
    expectationForPredicate(exists, evaluatedWithObject: confirmTitle, handler: nil) 

    // create a reference to the button through the webView and press it 
    var webView = app.descendantsMatchingType(.WebView) 
    webView.buttons["OK"].tap() 

    // wait for your app to return and test for expected behavior here 
} 

In mein Fall hatte ich auch mit dem Assertionsfehler umgehen: UI Testing Failure - nicht gefunden getroffen Punkt für Button Fehler über die forceTap() Erweiterung:

extension XCUIElement { 
    func forceTap() { 
     if self.hittable { 
      self.tap() 
     } else { 
      let coordinate: XCUICoordinate = self.coordinateWithNormalizedOffset(CGVectorMake(0.0, 0.0)) 
      coordinate.tap() 
     } 
    } 
} 

nach diesem fantastischen Blogeintrag here

3

Swift 3 Xcode 8 Lösung

func testFacebookLogin() { 
    let app = XCUIApplication() 

    // set up an expectation predicate to test whether elements exist 
    let exists = NSPredicate(format: "exists == true") 

    // as soon as your sign in button shows up, press it 
    let facebookSignInButton = app.buttons["Login With Facebook"] 
    expectation(for: exists, evaluatedWith: facebookSignInButton, handler: nil) 
    facebookSignInButton.tap() 

    // wait for the "Confirm" title at the top of facebook's sign in screen 
    let confirmTitle = app.staticTexts["Confirm"] 
    expectation(for: exists, evaluatedWith: confirmTitle, handler: nil) 

    // create a reference to the button through the webView and press it 
    let webView = app.descendants(matching: .webView) 
    webView.buttons["OK"].tap() 

    // wait for your app to return and test for expected behavior here 
    self.waitForExpectations(timeout: 10, handler: nil)   
} 

extension:

extension XCUIElement { 
func forceTap() { 
    if self.isHittable { 
     self.tap() 
    } else { 
     let coordinate: XCUICoordinate = self.coordinate(withNormalizedOffset: .zero) 
     coordinate.tap() 
    } 
} 

}

getestet und verifiziert sehr gut zu funktionieren!

+0

Für mich hat es nicht funktioniert, bis ich 'self.waitForExpectations (timeout: 10.0)' nach jeder Erwartung hinzugefügt Linie. Es ist nicht genug, um eine Erwartung zu schaffen, Sie müssen darauf warten ... –

+0

Sie haben Recht. Ich habe die erforderliche Zeile zu meinem Codebeispiel hinzugefügt. Gerade getestet, funktioniert alles wie erwartet. – thexande

Verwandte Themen