Search code examples
iosswiftui-testinguipasteboard

UI testing UIPasteboard


Why does this test fail?

Create a new swift iOS project in XCode 7 called Example with UI tests.

Example/ViewController.swift:

import UIKit
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        UIPasteboard.generalPasteboard().string = "test"
    }
}

ExampleUITests/ExampleUITests.swift:

import XCTest

class ExampleUITests: XCTestCase {

    override func setUp() {
        super.setUp()
        continueAfterFailure = false
        XCUIApplication().launch()
    }

    override func tearDown() {
        super.tearDown()
    }

    func testExample() {
        XCTAssertNotNil(UIPasteboard.generalPasteboard().string) //this fails
    }
}

Solution

  • You can't access to any kind of code from UITest

    UITest is separate process that is using accessibility to simulate user actions on simulator/device. So you just can check UI and make actions that available on UI (tap buttons etc)