3

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
    }
}
Millie H.
  • 138
  • 1
  • 9

1 Answers1

1

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)

itsji10dra
  • 4,603
  • 3
  • 39
  • 59
Andrey
  • 1,186
  • 7
  • 11
  • So it doesn't have access to the simulator's global clipboard? UI tests don't run in the simulator? – Millie H. Nov 09 '15 at 08:25
  • Let's say it run over simulator. Just simulate user actions, like tap etc. it doesn't have access to global clipboard – Andrey Nov 09 '15 at 23:16
  • 1
    The pasteboard is accessible via `XCTest` UI testing. See [this question](https://stackoverflow.com/questions/36091937/testing-that-a-particular-image-was-copied-to-pasteboard) for an example of testing using an image. See [this documentation](https://developer.apple.com/documentation/uikit/uipasteboard) for details on how to use the global pasteboard and access data (available since iOS 3+). – leanne Aug 06 '18 at 16:11
  • @leanne It sounds a lot like we might not easy to set the clipboard from XCTest, as [this question](https://developer.apple.com/forums/thread/684548) describe the details. – alwaysday1 Dec 31 '21 at 01:30