如何在Xcode中编写覆盖Facebook登录的UI测试?

时间:2016-04-21 12:55:24

标签: ios xcode facebook xctest xcode-ui-testing

我想在Xcode中编写一个用FBDSKLoginKit登录的UI测试。

但是,Facebook iOS SDK uses SFSafariViewController提交给用户以便对她进行身份验证,不幸的是there's no way how to interact with SFSafariViewController in UI tests in Xcode 7

如何在不与SFSafariViewController互动的情况下测试Facebook登录信息的任何想法?

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

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
}

在我的情况下,我还必须处理断言失败:UI测试失败 - 无法通过forceTap()扩展名找到Button 错误的生效点:

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

根据这篇精彩的博文here