如何在Xcode中编写覆盖Facebook登录的UI测试? [英] How to write UI tests covering login with Facebook in Xcode?

查看:137
本文介绍了如何在Xcode中编写覆盖Facebook登录的UI测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Xcode中编写一个UI测试,用 FBDSKLoginKit

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

然而, Facebook iOS SDK使用 SFSafariViewController 呈现给用户以便对她进行身份验证,不幸的是, 无法与 SFSafariViewController交互在Xcode 7的UI测试中

However, Facebook iOS SDK uses SFSafariViewController presented to the user in order to authenticate her and unfortunately, there's no way how to interact with SFSafariViewController in UI tests in Xcode 7.

如何在不与 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()
    }
}

}

经过测试和验证,效果很好!

Tested and verified to work great!

这篇关于如何在Xcode中编写覆盖Facebook登录的UI测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆