如何编写UItesting覆盖在Xcode的Facebooklogin?

我想在Xcode中编写一个用FBDSKLoginKit进行login的UItesting。

然而, Facebook的iOS SDK使用SFSafariViewController呈现给用户来validation她,不幸的是, 没有办法如何在Xcode 7的UItesting中与SFSafariViewController进行交互 。

任何想法如何testingFacebooklogin而不与SFSafariViewController交互?

Swift 3 Xcode 8解决scheme

  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 XCUIElement { func forceTap() { if self.isHittable { self.tap() } else { let coordinate: XCUICoordinate = self.coordinate(withNormalizedOffset: .zero) coordinate.tap() } } 

}

经过testing和validation,工作好极了!

Swift 3 Xcode 8更新

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

在我的情况下,我也不得不处理断言失败:UItesting失败 – 无法通过forceTap()扩展findbutton错误的命中点

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

根据这个真棒博客post在这里