How to write user interface tests regarding Facebook login in Xcode?

I would like to write a user interface test in Xcode covering a login with FBDSKLoginKit .

However, the Facebook iOS SDK uses the SFSafariViewController presented to the user for authentication and, unfortunately, there is no way to interact with the SFSafariViewController in user interface tests in Xcode 7 .

Any ideas on checking facebook login without interacting with SFSafariViewController ?

+5
source share
2 answers

Swift 3 Xcode 8 Solution

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

Expansion:

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

}

Tested and tested to work perfectly!

+3
source

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 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 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 my case, I also had to deal with a denial of approval: UI testing error - Could not find the hit point for Button error through forceTap () extension:

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

according to this wonderful blog post here

+1
source

Source: https://habr.com/ru/post/1247621/


All Articles