-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
531 lines (468 loc) · 15.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
'use strict';
var util = require("util");
var React = require('react-native');
const findNodeHandle = React.findNodeHandle;
const TestFairyBridge = React.NativeModules.TestFairyBridge;
let originalFetch = null;
class TestFairy {
/**
* Initialize a TestFairy session with options.
*
* @param appToken Your key as given to you in your TestFairy account
* @param options A dictionary of options controlling the current session
*/
static begin(appKey, options = {}) {
options = {...options, "react-native-version": require('react-native/package.json').version};
TestFairyBridge.begin(appKey, options);
}
/**
* Initialize the TestFairy SDK with shake for feedback enabled. No sessions will be recorded.
*/
static installFeedbackHandler(appKey) {
TestFairyBridge.installFeedbackHandler(appKey);
}
/**
* Uninstall previously installed feedback handlers.
*/
static uninstallFeedbackHandler() {
TestFairyBridge.uninstallFeedbackHandler();
}
/**
* Sets a correlation identifier for this session. This value can
* be looked up via web dashboard. For example, setting correlation
* to the value of the user-id after they logged in. Can be called
* only once per session (subsequent calls will be ignored.)
*
* @param correlationId Id for the current session
*/
static setCorrelationId(correlationId) {
TestFairyBridge.setCorrelationId(correlationId);
}
/**
* Sets a correlation identifier for this session. This value can
* be looked up via web dashboard. For example, setting correlation
* to the value of the user-id after they logged in. Can be called
* only once per session (subsequent calls will be ignored.)
*
* @param correlationId Id for the current session
* @param traits Attributes and custom attributes to be associated with this session
*/
static identify(correlationId, traits = {}) {
TestFairyBridge.identify(correlationId, traits);
}
/**
* Takes a screenshot.
*/
static takeScreenshot() {
TestFairyBridge.takeScreenshot();
}
/**
* Pauses the current session. This method stops recoding of
* the current session until resume has been called.
*
* @see resume
*/
static pause() {
TestFairyBridge.pause();
}
/**
* Resumes the recording of the current session. This method
* resumes a session after it was paused.
*
* @see pause
*/
static resume() {
TestFairyBridge.resume();
}
/**
* @deprecated use {@link #addEvent(String)} instead.
*/
static checkpoint(name) {
TestFairyBridge.checkpoint(name);
}
/**
* Marks an event in session. Use this text to tag a session with an event name. Later you can filter
* sessions where your user passed through this checkpoint, to better understanding user experience
* and behavior.
*
* @param eventName String
*/
static addEvent(name) {
TestFairyBridge.checkpoint(name);
}
/**
* Send a feedback on behalf of the user. Call when using a in-house
* feedback view controller with a custom design and feel. Feedback will
* be associated with the current session.
*
* @param feedbackString Feedback text
*/
static sendUserFeedback(feedback) {
TestFairyBridge.sendUserFeedback(feedback);
}
/**
* Hides a specific view from appearing in the video generated.
*
* @param view The specific view you wish to hide from screenshots
*/
static hideView(viewTag) {
if (typeof viewTag === 'number') {
TestFairyBridge.hideView(findNodeHandle(viewTag));
} else {
TestFairyBridge.hideViewWithNativeId(viewTag);
}
}
/**
* Change the server endpoint for use with on-premise hosting. Please
* contact support or sales for more information. Must be called before begin
*
* @param serverOverride server address for use with TestFairy
*/
static setServerEndpoint(url) {
TestFairyBridge.setServerEndpoint(url);
}
/**
* Remote logging, use log as you would use console.log. These logs will be sent to the server.
*/
static log(message) {
try {
TestFairyBridge.log(JSON.stringify(message));
} catch (e) {
TestFairyBridge.log(util.inspect(message));
}
}
/**
* Set a custom name for the current screen. Useful for applications that don't use more than one
* Activity. This name is displayed for a given screenshot, and will override the name of the current
* Activity.
*
* @param name String
*/
static setScreenName(name) {
TestFairyBridge.setScreenName(name);
}
/**
* Stops the current session recording. Unlike 'pause', when
* calling 'resume', a new session will be created and will be
* linked to the previous recording. Useful if you want short
* session recordings of specific use-cases of the app. Hidden
* views and user identity will be applied to the new session
* as well, if started.
*/
static stop() {
TestFairyBridge.stop();
}
/**
* Use this function to tell TestFairy who is the user,
* It will help you to search the specific user in the TestFairy dashboard.
* We recommend passing values such as email, phone number, or user id that your app may use.
*
* @param userId We recommend to use email as userId, But It can be phone number or any other unique id.
*/
static setUserId(userId) {
TestFairyBridge.setUserId(userId);
}
/**
* Records an attribute that will be added to the session.
*
* NOTE: The SDK limits you to storing 64 attribute keys. Adding more than 64 will fail and return false.
*
* @param key The key of the attribute
* @param value The value associated with the attribute max size of 1kb
* @return boolean true if successfully set attribute value, otherwise false
*/
static setAttribute(key, value) {
TestFairyBridge.setAttribute(key, value);
}
/**
* Displays the feedback activity. Allow users to provide feedback
* about the current session. All feedback will appear in your
* build report page, and in the recorded session page.
*
* Must be called after begin.
*/
static pushFeedbackController() {
TestFairyBridge.pushFeedbackController();
}
/**
* Displays the feedback form. Allow users to provide
* feedback without prior call to begin. All feedback
* will appear in your build report page, and in
* "Feedbacks" tab.
*
* This method is different from showFeedbackForm by
* that it does not require a call to begin().
*
* @param appToken Your key as given to you in your TestFairy account
* @param takeScreenshot whether screenshot should be automatically added
*/
static showFeedbackForm(appToken, takeScreenshot) {
TestFairyBridge.showFeedbackForm(appToken, takeScreenshot);
}
static hideWebViewElements(selector) {
TestFairyBridge.hideWebViewElements(selector);
}
/**
* Enables the ability to capture crashes. TestFairy
* crash handler is installed by default. Once installed
* it cannot be uninstalled. Must be called before begin.
*/
static enableCrashHandler() {
TestFairyBridge.enableCrashHandler();
}
/**
* Disables the ability to capture crashes. TestFairy
* crash handler is installed by default. Once installed
* it cannot be uninstalled. Must be called before begin.
*/
static disableCrashHandler() {
TestFairyBridge.disableCrashHandler();
}
/**
* Enables recording of a metric regardless of build settings.
* Valid values include 'cpu', 'memory', 'logcat', 'battery', 'network-requests'
* A metric cannot be enabled and disabled at the same time, therefore
* if a metric is also disabled, the last call to enable to disable wins.
* Must be called be before begin.
*/
static enableMetric(metric) {
TestFairyBridge.enableMetric(metric);
}
/**
* Disables recording of a metric regardless of build settings.
* Valid values include "cpu", "memory", "logcat", "battery", "network-requests"
* A metric cannot be enabled and disabled at the same time, therefore
* if a metric is also disabled, the last call to enable to disable wins.
* Must be called be before begin.
*/
static disableMetric(metric) {
TestFairyBridge.disableMetric(metric);
}
/**
* Enables the ability to capture video recording regardless of build settings.
* Valid values for policy include "always", "wifi" and "none"
* Valid values for quality include "high", "low", "medium"
* Values for fps must be between 0.1 and 2.0. Value will be rounded to
* the nearest frame.
*/
static enableVideo(policy, quality, framesPerSecond) {
TestFairyBridge.enableVideo(policy, quality, framesPerSecond);
}
/**
* Disables the ability to capture video recording. Must be
* called before begin.
*/
static disableVideo() {
TestFairyBridge.disableVideo();
}
/**
* Enables the ability to present the feedback form
* based on the method given. Valid values only include
* "shake". If an unrecognized method is passed,
* the value defined in the build settings will be
* used. Must be called before begin.
*/
static enableFeedbackForm(method) {
TestFairyBridge.enableFeedbackForm(method);
}
/**
* Disables the ability to present users with feedback when
* devices is shaken, or if a screenshot is taken. Must be called
* before begin.
*/
static disableFeedbackForm() {
TestFairyBridge.disableFeedbackForm();
}
/**
* Disables auto update prompts for this session. Must be called
* Must be called before begin.
*/
static disableAutoUpdate() {
TestFairyBridge.disableAutoUpdate();
}
/**
* Sets the maximum recording time. Minimum value is 60 seconds,
* else the value defined in the build settings will be used. The
* maximum value is the lowest value between this value and the
* value defined in the build settings.
* Time is rounded to the nearest minute.
* Must be called before begin.
*/
static setMaxSessionLength(seconds) {
TestFairyBridge.setMaxSessionLength(seconds);
}
/**
* Send an exception to TestFairy.
* Note, this function is limited to 5 errors.
* @param error Error
*/
static logException(error) {
TestFairyBridge.logException(error.message, error.stack);
}
/**
* Customize the feedback form.
*
* Accepted dictionary values: {
* "defaultText": <Default feedback text>,
* "isEmailMandatory": true|false,
* "isEmailVisible": true|false
* }
*
* defaultText: By setting a default text, you will override the initial content of the text area
* inside the feedback form. This way, you can standardize the way you receive feedbacks
* by specifying guidelines to your users.
*
* isEmailMandatory: Determines whether the user has to add his email address to the feedback. Default is true
*
* isEmailVisible: Determines whether the email field is displayed in the feedback form. Default is true
*/
static setFeedbackOptions(options) {
TestFairyBridge.setFeedbackOptions(options);
}
/**
* Attach a file to the session timeline at current moment in time.
*
* @param filename Name of the attached file. It must have a file extension.
* @param content A utf-8 javascript string, can be empty.
* @param mimeType MIME type of the given file, i.e "text/plain"
*/
static attachFile(filename, content, mimeType) {
TestFairyBridge.attachFile(filename, content);
}
/**
* Add a network timeline event to the current session.
*
* @param {string} url URL of the network call
* @param {string} method HTTP method
* @param {number} statusCode Response status code
* @param {number} startTimeMillis Call start time
* @param {number} endTimeMillis Response end time
* @param {number} requestSize Request body content size
* @param {number} responseSize Response body content size
* @param {string?} errorMessage Network error message if exists (optional)
* @param {string?} requestHeaders Request headers (optional)
* @param {string?} requestBody Request body (optional)
* @param {string?} responseHeaders Response headers (optional)
* @param {string?} responseBody Response body (optional)
*/
static addNetworkEvent(url, method, statusCode, startTimeMillis, endTimeMillis, requestSize, responseSize, errorMessage, requestHeaders, requestBody, responseHeaders, responseBody) {
TestFairyBridge.addNetworkEvent(url, method, statusCode, startTimeMillis, endTimeMillis, requestSize, responseSize, errorMessage, requestHeaders, requestBody, responseHeaders, responseBody);
}
/**
* Enables capturing HTTP calls and reports themn in session timeline. Capturing request and response bodies is disabled by default.
*
* @param {object} window Global window object
* @param {object} Optional configuration object. Provide `includeHeaders` and `includeBodies` keys to enable request/response headers in the log.
*/
static enableNetworkLogging(window, options) {
// ***********************
// Anatomy of a fetch() call
// ***********************
//
// fetch('https://example.com/endpoint/', {
// method: 'POST',
// headers: {
// Accept: 'application/json',
// 'Content-Type': 'application/json'
// },
// body: JSON.stringify({
// firstParam: 'yourValue',
// secondParam: 'yourOtherValue'
// })
// }).then((response) => {});
if (!window.fetch) {
return;
}
if (!originalFetch) {
originalFetch = window.fetch;
}
if (window.fetch != originalFetch) {
return;
}
if (!options) {
options = {
includeHeaders: false,
includeBodies: false
}
}
window.fetch = (...args) => {
// Grab url
const url = args[0]
// Grab HTTP method
const method = args[1] ? (args[1].method ? args[1].method : 'GET') : 'GET';
// Record call time
const startTimeMillis = new Date().getTime();
// Grab request size
const requestSize = args[1] ? (args[1].body ? args[1].body.toString().length : 0) : 0;
// Start grabbing headers
let requestHeaders = "";
let responseHeaders = "";
// Grab request headers
if (options.includeHeaders && args[1] && args[1].headers) {
Object.keys(args[1].headers).forEach((key) => {
requestHeaders += key + ": " + args[1].headers[key] + "\n";
})
requestHeaders = requestHeaders.slice(0, -1);
} else {
requestHeaders = null;
}
// Grab request body
let requestBody = null;
if (options.includeBodies && args[1] && args[1].body) {
requestBody = args[1].body.toString();
}
// Store unpolluted promise to return it eventually
const networkCall = originalFetch(...args);
// Do rest of the remaining work async
let statusCode = -1;
networkCall.then((response) => {
// Grab response headers
if (options.includeHeaders) {
response.headers.forEach((val, key) => {
responseHeaders += key + ": " + val + "\n";
});
responseHeaders = responseHeaders.slice(0, -1);
} else {
responseHeaders = null;
}
// Grab status code
statusCode = response.status;
// Grab response body
return options.includeBodies ? response.text() : null;
}).then((responseBody) => {
// Record response time
const endTimeMillis = new Date().getTime();
// Grab response size
const responseSize = responseBody ? responseBody.length : 0;
// Send event
TestFairy.addNetworkEvent(url, method, statusCode, startTimeMillis, endTimeMillis, requestSize, responseSize, null, requestHeaders, requestBody, responseHeaders, responseBody);
}).catch((err) => {
// Record response time
const endTimeMillis = new Date().getTime();
// Assume zero response (?)
const responseSize = 0;
// Grab error message
const errorMessage = err.toString();
// Send event
TestFairy.addNetworkEvent(url, method, statusCode, startTimeMillis, endTimeMillis, requestSize, responseSize, errorMessage, requestHeaders, requestBody, responseHeaders, null);
})
// WARN : Don't ever return the polluted promise above
return networkCall;
};
window.fetch.bind(window);
}
/**
* Disabled capturing HTTP calls.
*
* @param {object} window Global window object
*/
static disableNetworkLogging(window) {
window.fetch = originalFetch;
}
}
// var _testfairyConsoleLog = console.log;
// console.log = function(message) {
// _testfairyConsoleLog(message);
// TestFairy.log(message);
// }
module.exports = TestFairy;