XCTest UI Testing

😻 and 🙀 and 😹

 

brandenbyers.com/slides/ui-testing/

What is XCTest?

import XCTest

class exampleTests: XCTestCase {

  func testEmojiTranslation() {
    let app = XCUIApplication()
    app.launch()
    emojiTextField = app.textFields["TranslateEmojiField"]
    emojiTextField.tap()
    emojiTextField.typeText("👋🌎\n")

    XCTAssert(app.tables.staticTexts["Hello World"].exists, "😞")
  }

}
Unit Tests Integration e2e

Stability

No longer crashes every other test

UIAutomation vs XCTest

  • UIAutomation == JavaScript
  • XCTest == Swift or Obj-c
  • UIAutomation Deprecated
  • UIAutomation vs XCTest

  • UIAutomation == JavaScript
  • XCTest == Swift or Obj-c
  • UIAutomation Deprecated
  • Alternatives

    • KIF
    • Calabash

     

    must wait for support of new iOS releases

    Limitations

    Can't background apps

        XCUIDevice.sharedDevice().pressButton(XCUIDeviceButton.Home)    XCUIDevice.sharedDevice().pressButton(XCUIDeviceButton.VolumeUp)
        XCUIDevice.sharedDevice().pressButton(XCUIDeviceButton.VolumeDown)

    Can't do everything a user can do

    Timing doesn't mirror user behavior

    Dependent on accessibility

    Random test order

    Slow

    Recording

    • Doesn't always work
    • Crashes semi-regularly
    • Good place to start

    Use accesibilityIdentifier

    
    let collectionViewsQuery = XCUIApplication().otherElements.
      containingType(.NavigationBar, identifier: "Home").
      childrenMatchingType(.Other).element.
      childrenMatchingType(.Other).element.
      childrenMatchingType(.Other).element.
      childrenMatchingType(.CollectionView).element.
      childrenMatchingType(.Cell).elementBoundByIndex(0).
      collectionViews
    
    collectionViewsQuery.staticTexts["Help!"].tap()
              

    Simulator Keyboard Bug

    Hardware > Keyboard > Connect Hardware Keyboard

    Detecting iPhone/iPad

    
    if (XCUIApplication().windows.elementBoundByIndex(0).
      horizontalSizeClass == .Compact || XCUIApplication().windows.
      elementBoundByIndex(0).verticalSizeClass == .Compact) {
      // iPhone
    } else {
      // iPad
    }

    launchArguments

    
              let app = XCUIApplication()
              app.launchArguments = [ "USE_MOCK_SERVER" ]
              app.launch()
              

    launchEnvironment

    
              let app = XCUIApplication()
              app.launchEnvironment = ["testEnv" : "value"]
              app.launch()
              

    po print(app.debugDescription) vs print(app.debugDescription)

    Resources

    Apple’s API Reference
    WWDC 2016 Session 409: Advanced Testing and Continuous Integration
    WWDC 2015 Session 406: UI Testing in Xcode
    Page Object Model
    UI Testing Stub Network Data
    AWS Device Farm
    Hands-On Mobile Testing by Daniel Knott