在 Detox 测试不起作用时启用通知
Posted
技术标签:
【中文标题】在 Detox 测试不起作用时启用通知【英文标题】:Enable notification while test with Detox isn't working 【发布时间】:2019-11-25 04:23:01 【问题描述】:我正在尝试使用device.launchApp( permissions: notifications: 'YES' )
启用权限,但它似乎没有被调用。
这是我的测试:
describe('Mobile test', () =>
beforeAll(async () =>
await detox.init(config, launchApp: false );
await device.launchApp( permissions: notifications: 'YES' );
);
describe('Smoke', () =>
it('my test', async () =>
mock.cardData.assignee = mock.signupData.profile.fullName;
await Operator.signup(mock.signupData);
);
);
);
这是使用详细 loglevel
进行测试的输出:
detox[8345] INFO: [DetoxServer.js] server listening on localhost:65156...
detox[8345] DEBUG: [AsyncWebSocket.js/WEBSOCKET_OPEN] opened web socket to: ws://localhost:65156
detox[8345] DEBUG: [DetoxServer.js/LOGIN] role=tester, sessionId=087d9cea-5251-fbda-160a-f89d4927dee3
detox[8345] DEBUG: [DetoxServer.js/LOGIN_SUCCESS] role=tester, sessionId=087d9cea-5251-fbda-160a-f89d4927dee3
detox[8345] DEBUG: [exec.js/EXEC_CMD, #0] /usr/bin/xcrun simctl list -j
detox[8345] DEBUG: [exec.js/EXEC_CMD, #1] applesimutils --list --byType "iPhone X" --byOS "12.2"
detox[8345] DEBUG: [exec.js/EXEC_TRY, #1] Searching for device matching iPhone X...
detox[8345] DEBUG: [exec.js/EXEC_CMD, #2] applesimutils --list --byId "B2E486B7-8CD7-4B94-9969-3C6901591BD8"
detox[8345] DEBUG: [exec.js/EXEC_CMD, #3] /usr/bin/xcrun simctl uninstall B2E486B7-8CD7-4B94-9969-3C6901591BD8 com.amadeu.mobileapp
detox[8345] DEBUG: [exec.js/EXEC_TRY, #3] Uninstalling com.amadeu.mobileapp...
detox[8345] DEBUG: [exec.js/EXEC_SUCCESS, #3] com.amadeu.mobileapp uninstalled
detox[8345] DEBUG: [exec.js/EXEC_CMD, #4] /usr/bin/xcrun simctl install B2E486B7-8CD7-4B94-9969-3C6901591BD8 "/Users/amadeu.filho/Developer/amadeu-mobile/ios/build/Build/Products/Debug-iphonesimulator/amadeumobile.app"
detox[8345] DEBUG: [exec.js/EXEC_TRY, #4] Installing /Users/amadeu.filho/Developer/amadeu-mobile/ios/build/Build/Products/Debug-iphonesimulator/amadeumobile.app...
detox[8345] DEBUG: [exec.js/EXEC_SUCCESS, #4] /Users/amadeu.filho/Developer/amadeu-mobile/ios/build/Build/Products/Debug-iphonesimulator/amadeumobile.app installed
detox[8345] DEBUG: [exec.js/EXEC_CMD, #5] /usr/bin/xcrun simctl terminate B2E486B7-8CD7-4B94-9969-3C6901591BD8 com.amadeu.mobileapp
detox[8345] DEBUG: [exec.js/EXEC_TRY, #5] Terminating com.amadeu.mobileapp...
detox[8345] DEBUG: [exec.js/EXEC_SUCCESS, #5] com.amadeu.mobileapp terminated
detox[8345] DEBUG: [exec.js/EXEC_CMD, #6] SIMCTL_CHILD_DYLD_INSERT_LIBRARIES="/Users/amadeu.filho/Library/Detox/ios/1ea4f89e9092026849f937e47281627c5f5251d6/Detox.framework/Detox" /usr/bin/xcrun simctl launch B2E486B7-8CD7-4B94-9969-3C6901591BD8 com.amadeu.mobileapp --args -detoxServer "ws://localhost:65156" -detoxSessionId "087d9cea-5251-fbda-160a-f89d4927dee3"
detox[8345] DEBUG: [exec.js/EXEC_TRY, #6] Launching com.amadeu.mobileapp...
detox[8345] DEBUG: [exec.js/EXEC_CMD, #7] /usr/bin/xcrun simctl get_app_container B2E486B7-8CD7-4B94-9969-3C6901591BD8 com.amadeu.mobileapp
detox[8345] INFO: [AppleSimUtils.js] com.amadeu.mobileapp launched. To watch simulator logs, run:
/usr/bin/xcrun simctl spawn B2E486B7-8CD7-4B94-9969-3C6901591BD8 log stream --level debug --style compact --predicate 'processImagePath beginsWith "/Users/amadeu.filho/Library/Developer/CoreSimulator/Devices/B2E486B7-8CD7-4B94-9969-3C6901591BD8/data/Containers/Bundle/Application/6F69DC89-7478-4502-8998-8B93E8D061C6/amadeumobile.app"'
detox[8345] DEBUG: [DetoxServer.js/CANNOT_FORWARD] role=testee not connected, cannot fw action (sessionId=087d9cea-5251-fbda-160a-f89d4927dee3)
detox[8345] DEBUG: [DetoxServer.js/LOGIN] role=testee, sessionId=087d9cea-5251-fbda-160a-f89d4927dee3
detox[8345] DEBUG: [DetoxServer.js/LOGIN_SUCCESS] role=testee, sessionId=087d9cea-5251-fbda-160a-f89d4927dee3
我也尝试在应用打开时手动调用 applesimutils,但是,我不知道 detox 是否使用不同的包来运行测试套件。
➜ applesimutils --byId "B2E486B7-8CD7-4B94-9969-3C6901591BD8" --bundle com.amadeu.mobileapp --setPermissions notifications=YES
我正在使用 iPhone X 模拟器。
"buildversion" : "16E226",
"availability" : "(available)",
"isAvailable" : true,
"identifier" : "com.apple.CoreSimulator.SimRuntime.iOS-12-2",
"version" : "12.2",
"name" : "iOS 12.2"
【问题讨论】:
【参考方案1】:看来我们应该在不启动的情况下初始化排毒,然后使用device.launchApp( permissions: notifications: 'YES' )
所以,在init.js
文件中,您可以使用:
const detox = require('detox');
const adapter = require('detox/runners/jest/adapter');
const specReporter = require('detox/runners/jest/specReporter');
const config = require('../package.json').detox;
jest.setTimeout(120000);
jasmine.getEnv().addReporter(adapter);
jasmine.getEnv().addReporter(specReporter);
beforeAll(async () =>
await detox.init(config, launchApp: false );
await device.launchApp( permissions: notifications: 'YES' );
);
beforeEach(async () =>
await adapter.beforeEach();
);
afterAll(async () =>
await adapter.afterAll();
await detox.cleanup();
);
【讨论】:
如果您的应用在设置权限之前启动,它们将不会生效。 我有点困惑。我遇到了类似的情况,在我的规范中我使用permissions: notifications: 'YES'
启动但它一直失败,因为我在应用程序启动后立即收到通知提示。我正在初始化 Detox,就像上面的 await detox.init(config, launchApp: false );
但没有 await device.launchApp( permissions: notifications: 'YES' );
所以在这种情况下,我告诉 Detox 不要启动应用程序,但我的权限尚未生效。这是怎么发生的?以上是关于在 Detox 测试不起作用时启用通知的主要内容,如果未能解决你的问题,请参考以下文章