GithubHelp home page GithubHelp logo

succulent's Introduction

Succulent

Version License Platform Build Status

Succulent is a Swift library to provide API recording and replay for automated testing on iOS.

Succulent creates a local web server that you point your app to, instead of the live API. In recording mode, Succulent receives the API request from the app and then makes the same request to the live API, recording the request and response for future replay.

Succulent can also handle mutating requests, like POST, PUT and DELETE: after a mutating request Succulent stores a new version of any subsequent responses, then correctly simulates the change during playback.

Why?

Succulent solves the problem of getting repeatable API results to support stable automated testing.

Example

Set up Succulent in your XCTestCase's setUp method:

var app: XCUIApplication!
var succulent: Succulent!

override func setUp() {
	super.setUp()

	self.app = XCUIApplication()

	if let traceUrl = self.traceUrl {
		// Replay using an existing trace file
		self.succulent = Succulent(replayFrom: traceUrl)
	} else {
		// Record to a new trace file
		// The "/" at the end of the base URL is required
		self.succulent = Succulent(recordTo: self.recordUrl, baseUrl: URL(string: "{YOUR-REAL-BASE-URL}/")!)
	}

	self.succulent.start()

	self.app.launchEnvironment["succulentBaseURL"] = "http://localhost:\(succulent.actualPort)/"
	self.app.launch()
}

/// 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: "Succulent")
}

/// 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")
}

Note that recordUrl uses a string that must be set up in your UI testing directory's Info.plist file:

	<key>TraceRecordPath</key>
	<string>$(PROJECT_DIR)/Succulent/</string>

You also need to give the target you are testing permission to connect to a local server. This is done by adding the following to the Info.plist of the target you are testing against:

	<key>NSAppTransportSecurity</key>
	<dict>
 		<key>NSAllowsLocalNetworking</key>
 		<true/>
	</dict>

With this setting, Succulent records trace files into your project source tree. Therefore your Succulent traces are committed to source control with your test files, and when you build and run your tests the traces are copied into the test application.

Finally, in your app, look for the "succulentBaseURL" environment variable, and use that URL in place of your live API URL:

let apiBaseUrlString = ProcessInfo.processInfo.environment["succulentBaseURL"] ?? "{YOUR-REAL-BASE-URL}"
let apiBaseUrl = URL(string: baseUrlString)

There is an example project in the Example directory. To run the example project, run pod install from within the Example directory, then open the Xcode workspace and run the tests. The example project demonstrates some of the use of Succulent in a stand-alone setting rather than as it is intended, which is for UI automation testing of another app.

Requirements

Installation

Succulent is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "Succulent"

Authors

Karl von Randow, Tom Carey

License

Succulent is available under the MIT license. See the LICENSE file for more info.

succulent's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

succulent's Issues

Public Cocoapods Support

Hey Cactuslab,

This library is awesome, loving using it in my local stuff. I see that you guys did the ground work for supporting Cocoapods but I am not seeing Succulent's pod spec in the public Cocapods repo. I would be glad to help where I can. Is this something that can be done?

New maintainer for the project

I have noticed this project is no longer being maintained. Would it be possible for me to take over as the project's maintainer?

Tests crash

When running tests it crashes in testEncoding(), the underlying error is

 Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo={NSUnderlyingError=0x7ffd69321e60 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)" UserInfo={_kCFStreamErrorCodeKey=8, _kCFStreamErrorDomainKey=12}}, NSErrorFailingURLStringKey=http://localhost:55349/encodingTest%28%20%2742%27%20%29, NSErrorFailingURLKey=http://localhost:55349/encodingTest%28%20%2742%27%20%29, _kCFStreamErrorDomainKey=12, _kCFStreamErrorCodeKey=8, NSLocalizedDescription=A server with the specified hostname could not be found.}

Version 10.1 (10B61)

Swift 5 Support

Swift 5 is out. Succulent has been updated to work in swift 5 but it has a dependency which is Embassy. If you'd like to use Succulent in Swift 5 then you should specify something like the following in your pod file:

pod 'Succulent', git: 'https://github.com/cactuslab/Succulent.git', commit: 'd9044967382f733f8570e0419d7344042ba79066', configurations: ['Test']
pod 'Embassy', git: 'https://github.com/envoy/Embassy.git', commit: 'faad1ac1b9cfa49e14ac4136fc244228e0f2bb04', configurations: ['Test']

When Embassy gets a new version out we will need to push a new Succulent version to match.

support record https?

does this library support https? I am trying on https but it seems to work only with http.

Subsequent POST request with different request body will not get recorded

Hello,

Use case

  1. Log in with an incorrect password, expect an error message, dismiuss the message
  2. Log in with the correct password, expect to be in logged in state

Problem

The subsequent request/response won't get recorded in the trace file because Succulent doesn't look at POST body and won't see the the subsequent request as a different request.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.