Insider
Insider是一个工具框架,它为测试工具(如Appium、Calabash、Frank等)设置了应用的后门。
为什么我需要这个?
因为在自动化测试中,由于测试环境与应用程序隔离,很难涵盖许多场景。
- 在测试场景中对应用程序设置特定的状态;
- 模拟推送通知;
- 使用自定义schemes/通用链接模拟应用调用;
- 模拟handoff调用;
- 管理应用沙盒中的文件/目录;
- 在测试执行期间从应用程序收集指标(CPU、内存等.);
- 等等。
Insider在应用程序内部运行HTTP服务器并监听命令(RPC)。默认情况下,Insider运行在https://:8080
。
命令代表一个简单的HTTP请求:https://:8080/<command>
。您可以先配置应用程序,使其在接收到此类远程命令时模拟动作。
特性
内置特性 | 命令 | HTTP方法 | |
---|---|---|---|
向应用程序发送消息(字典); | /send |
POST | |
向应用程序发送消息,并等待响应; | /sendAndWaitForResponse |
POST | |
使用给定参数通过NSNotificationCenter发送本地通知; | /notification |
POST | |
获取设备系统状态信息(CPU、内存、IP地址等); | /systemInfo |
GET | |
管理应用程序沙盒中的文件/目录(文档、库、tmp); | /documents/<命令> /library/<命令> /tmp/<命令> |
见下表 |
文件管理特性支持的命令
文件管理命令 | HTTP方法 |
---|---|
列出项目: /<目录>/list |
GET |
下载项目: /<目录>/download |
GET |
上传项目: /<目录>/upload |
POST |
移动项目: /<目录>/move |
POST |
删除项目: /<目录>/delete |
POST |
创建文件夹: /<目录>/create |
POST |
在 scripts
目录中可以找到示例 Ruby 脚本,它们展示了内置功能的使用方法。
查阅API参考获取更多信息。
安装
手动安装
为了将 Insider 库包含到您的项目中,您需要从提供的源代码构建一个动态框架并将其包含到您的项目中,或者将其整个 Insider 库作为子项目通过复制到您的项目目录或作为 Git 子模块包含进来。
Carthage
如果您正在使用 Carthage,您可以在工作区中通过在 Cartfile
中添加以下行来构建库。
github "alexmx/Insider"
CocoaPods
如果您正在使用 CocoaPods,您同样可以通过在 Podfile
中添加以下行来集成库。
platform :ios, '8.0'
use_frameworks!
target 'YourAppTarget' do
pod "Insider"
end
用法
用例 #1:模拟推送通知
import Insider
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Launch the Isider with the given delegate
Insider.shared.start(withDelegate: self)
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// Process push notification
}
}
extension AppDelegate: InsiderDelegate {
// This delegate method is invoked when a remote message is sent to the app
func insider(_ insider: Insider, didReceiveRemote message: InsiderMessage?) {
// Simulate push notification
application(UIApplication.shared, didReceiveRemoteNotification: message!)
}
}
为了测试这个示例,运行 InsiderDemo
应用目标,然后进入 scripts
目录并运行 send_message.rb
脚本。
用例 #2:使用自定义协议模拟应用调用
import Insider
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Insider.shared.start(withDelegate: self)
return true
}
func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
// Process custom scheme invocation
return true
}
}
extension AppDelegate: InsiderDelegate {
func insider(_ insider: Insider, returnResponseMessageForRemote message: InsiderMessage?) -> InsiderMessage? {
// Simulate app invocation using a custom scheme
let url = URL(string: "insiderDemo://hello/params")
let response = application(UIApplication.shared, handleOpen: url!)
return ["response" as NSObject : response as AnyObject]
}
}
为了测试这个示例,运行 InsiderDemo
应用目标,然后进入 scripts
目录并运行 send_message_with_response.rb
脚本。
用例 #3:在测试执行期间获取应用程序系统信息
import Insider
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Insider.shared.start()
return true
}
}
由于这是一个内置功能,在这种情况下不需要为 Insider 设置代表。要测试此示例,运行 InsiderDemo
应用目标,然后进入 scripts
目录并运行 system_info.rb
脚本。
用例 #4:将文件添加到应用程序沙盒中的“文档”文件夹。
import Insider
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Insider.shared.start()
return true
}
}
由于这是一个内置功能,在这种情况下不需要为 Insider 设置代表。
应用程序沙盒支持 3 个目录:
- 文档:
https://:8080/documents
- 图书馆:
https://:8080/library
- tmp:
https://:8080/tmp
您可以创建新文件夹。从应用程序沙盒上传、下载、移动、删除文件/文件夹。
为了测试此示例,运行 InsiderDemo
应用目标,并在浏览器中打开
您将看到位于应用程序沙盒中的文件。
如果您需要在自动化脚本中使用沙盒文件管理 API,请参阅上面“文件管理命令”部分。
致谢
内幕 在幕后使用这些令人惊叹的库
许可证
该项目受MIT许可证协议的约束。请查看LICENSE文件。