Hello!
Another novelty in the list of our courses:
“iOS Developer” , which means that the time has come for interesting pieces that we found during the preparation of the course. In this note, the author examines how to record and play back API requests for the operation of UI tests.
Go.
I recently integrated
Embassy and
Succulent into my UI tests. If you need to run UI tests for an application using these APIs, this guide may offer an alternative to mock / stub.
Problems:
- The application uses the API data to populate the UI;
- Using stubs may require writing and maintaining a large number of files;
- When using mocks, the application logic may differ from the actual network call;
- The use of this connection API is COMPLETELY EXCLUDED, too many variables and failures
Solution with Embassy + SucculentThe solution is to create a local server to which your application is directed (using Embassy), and to record / answer network calls (using Succulent).
When you first run the test, standard network calls will be made and recorded in the trace file.
Next time, the same network calls will be answered automatically. Cool, is not it? No need to write mock'i, you can simulate lags and errors, and it all starts inside the build machine, inside XCtest!
How to use it?1. Download and install under Succulent. At the time of this writing, there was no submission to cocoapods.com, so you need to download the source and add it to your sub-file as follows:
target “UI Tests” do inherit! :search_paths pod 'Succulent', :path => 'Succulent/' end
Succulent is required by Embassy and it will be installed automatically.
2. Create a new test UI file and copy the instruction from the
Succulent GitHub . This should result in the following file:
import Succulent @testable import TestAppUITests class SucculentTestUITest: XCTestCase { private var succulent: Succulent! var session: URLSession! var baseURL: URL! /// The name of the trace file for the current test private var traceName: String { return self.description.trimmingCharacters(in: CharacterSet(charactersIn: "-[] ")).replacingOccurrences(of: " ", with: "_") } /// The URL to the trace file for the current test when running tests private var traceUrl: URL? { let bundle = Bundle(for: type(of: self)) return bundle.url(forResource: self.traceName, withExtension: "trace", subdirectory: "Traces") } /// The URL to the trace file for the current test when recording private var recordUrl: URL { let bundle = Bundle(for: type(of: self)) let recordPath = bundle.infoDictionary!["TraceRecordPath"] as! String return URL(fileURLWithPath: "\(recordPath)/\(self.traceName).trace") } override func setUp() { super.setUp() continueAfterFailure = false if let traceUrl = self.traceUrl { // Replay using an existing trace file succulent = Succulent(traceUrl: traceUrl) } else { // Record to a new trace file succulent = Succulent(recordUrl: self.recordUrl, baseUrl: URL(string: "https//base-url-to-record.com/")!) } succulent.start() let app = XCUIApplication() app.launchEnvironment["succulentBaseURL"] = "http://localhost:\(succulent.actualPort)/" app.launch() } override func tearDown() { super.tearDown() } }
When launching Succulent, it is possible to specify a base URL, thanks to which all requests including the base URL will be recorded, and all others ignored.
3. Add the following line to the Info.plist target of your UI test:
<key>TraceRecordPath</key> <string>$(PROJECT_DIR)/Succulent/Traces</string>
4. Direct the application to the local server.
To do this inside your main application, you need to check whether the “succulentBaseURL” environment variable exists and is configured.
It shows the url of your local web server and is configured in the setUp function, which was copied above in step 2.
Here, perhaps, that's all!
Now, when you make a simple test and run it, Succulent will write an API request and create a .trace file in the Traces folder of the target directory of your UI test. When you run the same test the next time, it will check if the file exists and run it.
You can open .trace files directly from Xcode, view all network requests and modify them as needed.
I hope the article was useful, here is the pizza:

THE END
As usual, we are waiting for comments, questions, etc. here or you can look at the
Open Day and ask the
teacher a question there.