Catbird 0.11.0

Catbird 0.11.0

Aleksey TyurninAlexander Ignition 维护。



Catbird 0.11.0

  • 作者:
  • Alexander Ignition

Catbird

Catbird | Drozd | Дрозд

用于 UI 测试的模拟服务器

功能

  • 动态模拟服务器
  • 静态文件模拟服务器
  • 带有写入响应文件的代理服务器

依赖关系

swift-nio 安装 libressl

$ brew install libressl

安装

Cocoapods

Catbird 添加到 UI 测试目标。

target 'App' do
  use_frameworks!

  target 'AppUITests' do
    inherit! :search_paths

    pod 'Catbird'
  end
end

在Xcode项目中设置

  • 打开 方案/编辑方案...
  • 选择测试操作
  • 选择 预处理操作
    • 添加 新的运行脚本操作
    • <YOUR_APP_TARGET> 提供构建设置
    • ${PODS_ROOT}/Catbird/start.sh
  • 选择 后处理操作
    • 添加 新的运行脚本操作
    • <YOUR_APP_TARGET> 提供构建设置
    • ${PODS_ROOT}/Catbird/stop.sh

用法

import XCTest
import Catbird

enum LoginMock: RequestBagConvertible {
    case success
    case blockedUserError

    var pattern: RequestPattern {
        return RequestPattern.post(URL(string: "/login")!)
    }

    var responseData: ResponseData {
        switch self {
        case .success:
            let json: [String: Any] = [
                "data": [
                    "access_token": "abc",
                    "refresh_token": "xyz",
                    "expired_in": "123",
                ]
            ]
            return ResponseData(
                statusCode: 200,
                headerFields: ["Content-Type": "application/json"],
                body: try! JSONSerialization.data(withJSONObject: json))

        case .blockedUserError:
            let json: [String: Any] = [
                "error": [
                    "code": "user_blocked",
                    "message": "user blocked"
                ]
            ]
            return ResponseData(
                statusCode: 400,
                headerFields: ["Content-Type": "application/json"],
                body: try! JSONSerialization.data(withJSONObject: json))
        }
    }
}

final class LoginUITests: XCTestCase {

    private let catbird = Catbird()
    private var app: XCUIApplication!

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

        // Base URL in app `UserDefaults.standard.url(forKey: "url_key")`
        let url = catbird.url.appendingPathComponent("api")
        app.launchArguments = ["-url_key", url.absoluteString]
        app.launch()
    }

    override func tearDown() {
        XCTAssertNoThrow(try catbird.send(.clear), "Remove all requests")
        super.tearDown()
    }

    func testLogin() {
        XCTAssertNoThrow(try catbird.send(.add(LoginMock.success)))

        app.textFields["login"].typeText("[email protected]")
        app.secureTextFields["password"].typeText("qwerty")
        app.buttons["Done"].tap()

        XCTAssertTrue(app.staticTexts["Main Screen"].exists)
    }

    func testBlockedUserError() {
        XCTAssertNoThrow(try catbird.send(.add(LoginMock.blockedUserError)))

        app.textFields["login"].typeText("[email protected]")
        app.secureTextFields["password"].typeText("burger")
        app.buttons["Done"].tap()

        XCTAssertTrue(app.alerts["Error"].exists)
    }

}

示例项目

$ cd Example/CatbirdX
$ bundle exec pod install
$ xed .

日志

日志可以在控制台.app中以子系统com.redmadrobot.catbird查看

不要忘记在操作菜单中包含消息

  • 包含信息消息
  • 包括调试信息

不选择此选项,则只有错误消息可见

网页

您可以在页面 http://127.0.0.1:8080/catbird 上查看所有截获的请求数据列表