rob*_*l12 9 react-native detox
我正在使用Detox在我的React Native项目中运行端到端测试.我也使用pretender.js来模拟我的API请求,我很难找到一种方法来了解应用程序当前是否处于"测试"模式.
我正在传递一个env变量(和使用babel-transform-inline-environment-variables)来判断我是否应该模拟请求但是shim.js在我们的发布版本中中断了.
有没有办法告诉Detox启动应用程序并从JS内部运行测试?理想情况下,我正在寻找在测试时设置的某种变量或从命令行传递的东西(TESTING=true react-native start或__TESTING__)
尝试使用react-native-config.这里还有一篇关于使用react-native-config 在React Native中管理配置的好文章.
我还在这里给出了一个答案animated-button-block-the-detox,其中包含如何使用react-native-config在测试期间禁用循环动画的工作示例.
基本思想是为所有不同的构建环境(开发,生产,测试等)创建.env配置文件.这些包含您可以从Javascript,Objective-C/Swift或Java访问的配置变量.
然后指定在构建应用程序时使用哪个.env配置文件:
$ ENVFILE=.env.staging react-native run-ios # bash
这是package.json文件的一个示例,其中detox使用.env配置文件来构建应用程序.
"detox": {
"specs": "e2e",
"configurations": {
"ios.sim.release": {
"binaryPath": "ios/build/Build/Products/Release-iphonesimulator/example.app",
"build": "ENVFILE=.env.production export RCT_NO_LAUNCH_PACKAGER=true && xcodebuild -project ios/example.xcodeproj -scheme example -configuration Release -sdk iphonesimulator -derivedDataPath ios/build",
"type": "ios.simulator",
"name": "iPhone 5s, iOS 10.3"
},
"ios.sim.test": {
"binaryPath": "ios/build/Build/Products/Debug-iphonesimulator/example.app",
"build": "ENVFILE=.env.testing xcodebuild -project ios/example.xcodeproj -scheme example -configuration Debug -sdk iphonesimulator -derivedDataPath ios/build -arch x86_64",
"type": "ios.simulator",
"name": "iPhone 5s, iOS 10.3"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我们正在利用 detox--args -detoxServer ... -detoxSessionId ...在 iOS 命令行上调用您的二进制文件并{ detoxServer: ..., detoxSessionId: ... }在 android 中的 InstrumentationRegistry 中进行设置的事实。
我们目前将其暴露给 JS 的方式对于 StackOverflow 的答案来说有点太多了,但是这里有一些示例代码以及 React Native 的文档应该可以帮助您实现这一点 - 对于 Android:
// This will throw ClassNotFoundException if not running under any test,
// but it still might not be running under Detox
Class<?> instrumentationRegistry = Class.forName("android.support.test.InstrumentationRegistry");
Method getArguments = instrumentationRegistry.getMethod("getArguments");
Bundle argumentsBundle = (Bundle) getArguments.invoke(null);
// Say you're in your BaseJavaModule.getConstants() implementation:
return Collections.<String, Object>singletonMap("isDetox", null != argumentsBundle.getString("detoxServer"));
Run Code Online (Sandbox Code Playgroud)
在 iOS 上,类似(没有 Objective-C 编译器 ATM):
return @{@"isDetox": [[[NSProcessInfo processInfo] arguments] containsObject: @"-detoxServer"]}
Run Code Online (Sandbox Code Playgroud)
请注意,也可以使用 detox 添加您自己的参数:
detox.init(config, { launchApp: false });
device.launchApp({ newInstance: true, launchArgs: {
myCustomArg: value,
...,
} });
Run Code Online (Sandbox Code Playgroud)
如果能在某个时候将其完善为一个模块,那就太好了。
| 归档时间: |
|
| 查看次数: |
1690 次 |
| 最近记录: |