XCTests are not working properly on M1 MacBook

Viewed 445

I purchased M1 MacBook recently and I am not able to run XC UI tests. The app constantly hanging, best case scenario it is able to run 2-3 tests out of 50.

I've tried multiple combinations of excluding arm64 architecture, running simulator/Xcode on Rosetta, nothing works.

Same project works just fine on my older intel Mac 100% of the time.

Does anyone have any suggestions? Any kind of help is highly appreciated.

1 Answers

This is the issue with M1 MacBooks, Pasteboard is having this issue in latest chip and latest simulators(iOS 14 and above).

As of now, no updates from apple for this. So we can try alternative something

•   Save what you want to paste into a textedit file 
•   Drag and drop the .txt file into the simulator window
•   The simulator will prompt you to Save the file in the Files App
•   From the Simulator Open the file and copy the text you want
•   Paste into your App

EXAMPLE: - Make sure to add yourfile.txt in your bundle. and here in textfield data will come from the file of your bundle. Even you can use file from document directory as well if you don't want to add file in bundle.

func testExample() throws {
    
    let testBundle = Bundle(for: type(of: self))
    guard let filePath = testBundle.path(forResource: "yourfile", ofType: "txt") else {
        return
    }
    
    let fileURL = URL(fileURLWithPath: filePath)
    let result = try String(contentsOf: fileURL, encoding: .utf8)
    
    let app = XCUIApplication()
    app.launch()
    let textField = app.textFields["textFieldId"]
    textField.tap()
    textField.typeText(result)
    let resultLabel = app.staticTexts["resultId"]
    app.buttons["buttonId"].tap()
    XCTAssertEqual(textField.value as! String, resultLabel.label)
 }
Related