我想在 Xcode 中编写一个 UI 测试,涵盖登录FBDSKLoginKit https://cocoapods.org/pods/FBSDKLoginKit.
然而,Facebook iOS SDK 使用SFSafariViewController https://developers.facebook.com/blog/post/2015/10/29/Facebook-Login-iOS9/呈现给用户以验证她的身份,不幸的是,没有办法与之互动SFSafariViewController在 Xcode 7 的 UI 测试中 https://stackoverflow.com/questions/32763010/any-way-to-automate-sfsafariviewcontroller-in-ui-tests.
任何想法如何测试 Facebook 登录而不交互SFSafariViewController
?
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()
}
}
}
经过测试和验证,效果很好!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)