-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
419 lines (354 loc) · 15 KB
/
test.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
"use strict";
require("./helpers/setup");
var unorm = require("unorm"),
wd = require("wd"),
_ = require('underscore'),
STACKIFY = require('stackify'),
Q = require('q'),
require_directory = require('require-directory'),
fs = require('fs');
GLOBAL.webdriver = require('selenium-webdriver');
var config = require('./config/config.json');
var test_flow = require('./config/test_flow.json');
var i = 0;
var print = "";
var test_data = require('./test_data.js');
var test_count = 0;
var sys = require('sys');
var exec = require('child_process').exec;
var child;
var Pass = 0;
var Fail = 0;
var common_functions = require('./helpers/common_functions.js');
var contextObjects = [];
exports.PassCases = function() {
return Pass;
}
exports.FailCases = function() {
return Fail;
}
var get_test_cases_list = function(callback) {
debugger;
var case_list = {};
var cases = require_directory(module, './test_cases');
for (var item in cases) { // This will allows you take test cases from multiple files present in cases folder
for (var sub_item in cases[item]["list"]) {
if (typeof(case_list[sub_item]) == "undefined") {
case_list[sub_item] = cases[item]["list"][sub_item];
}
}
}
callback(case_list);
};
var init = exports.init = function(callback) { // This function is used to load appium drivers
try {
var serverConfig = config.servers.local; // Appium Server details imported from config.json present in config folder
GLOBAL.driver = wd.promiseChainRemote(serverConfig);
require("./helpers/logging").configure(driver);
var desired = _.clone(config.environments.android18); // Device setting imported from config.json present in config folder
desired.app = config.build_file_path; // // App_path imported from config.json present in config folder
GLOBAL.driver
.init(desired)
.setImplicitWaitTimeout(10000);
debugger;
setTimeout(function() {
callback();
}, 50000); // This will wait for 20 seconds until the driver loads and app starts
} catch (e) {
callback(e);
}
};
var init_chrome = exports.init_chrome = function(callback) {
try {
GLOBAL.driver = new webdriver.Builder().
withCapabilities(config.environments.android19).
usingServer('http://172.16.0.220:4725/wd/hub').
build();
driver.get('http://pex.builtapp.io/');
debugger;
setTimeout(function() {
callback();
}, 50000);
} catch (e) {
callback(e);
}
};
var initios = exports.initios = function(callback) { // This function is used to load appium drivers
try {
var serverConfig = config.servers.local;
GLOBAL.driver = wd.promiseChainRemote(serverConfig);
require("./helpers/logging").configure(driver);
var desired = _.clone(config.environments.ios81);
desired.app = config.build_file_path_ios;
GLOBAL.driver
.init(desired)
.setImplicitWaitTimeout(10000);
debugger;
setTimeout(function() {
callback();
}, 50000); // This will wait for 50 seconds until the driver loads and app starts
} catch (e) {
callback(e);
}
};
var initios_safari = exports.initios_safari = function(callback) { // This function is used to load appium drivers
try {
var serverConfig = config.servers.local;
GLOBAL.driver = wd.promiseChainRemote(serverConfig);
require("./helpers/logging").configure(driver);
var desired = _.clone(config.environments.ios82);
desired.browserName = 'safari';
GLOBAL.driver
.init(desired)
.setImplicitWaitTimeout(10000);
debugger;
setTimeout(function() {
callback();
}, 50000); // This will wait for 50 seconds until the driver loads and app starts
driver.contexts()
.then(function(contextObjects){
driver.context(contextObjects[1]);
},function(err){
console.log(err);
});
return driver.get("www.google.com");
} catch (e) {
callback(e);
}
};
var run = exports.run = function(callback) {
try {
debugger;
// below are the steps to record video using command prompt and store the same in the device memory
// below function will add child item one by one presented in a single flow
var stackify_processor = new STACKIFY(function(index, stackify) {
var item = stackify.item(index);
var flow_name = stackify.options.flow_name;
try {
item.call().done(function(output) { //call from cases file to perform
if (output == "Pass") { // if the child item return value as "Pass" then will it execute another child item else it will execute the another flow and make the test case as fail
print = "yes";
stackify.next(index, "Function got Executed: " + index); // This will execue another child item
} else {
print = "no";
Fail++
++i
test_count++;
child = exec("adb shell screencap -p /sdcard/" + flow_name + ".png", function(error, stdout, stderr) { // this allows you to take screenshot of failed testcase and store in the device memory
sys.print('stdout: ' + stdout);
sys.print('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
child = exec("adb pull /sdcard/" + flow_name + ".png ./result/" + flow_name + ".png", function(error, stdout, stderr) { // this will sore the above screenshot to the desktop
sys.print('stdout: ' + stdout);
sys.print('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
fs.appendFile(common_functions.myresults_path(), common_functions.result("TCID-" + test_count, test_data.test_detail[flow_name], 'Fail', flow_name + ".png")); // This will print the failed testcase in the result file
stackify.parent.next(stackify.parent.index, "Function got Executed: " + index); // this will directly jump to the another flow and will skip all the child items
}
});
} catch (e) {
stackify.error(index, e);
}
}, function(index, stackify) {}, function(index, stackify) {
stackify.parent.next(stackify.parent.index, "Function got Executed: " + index);
});
var flow_details = function(item) {
debugger;
var flow_name = "";
var function_name_list = [];
for (var key in item) {
debugger;
function_name_list = item[key];
flow_name = key;
}
return {
"flow_name": flow_name,
"function_name_list": function_name_list
};
};
var stackify_parent_processor = new STACKIFY(function(index, stackify_parent) {
debugger;
var item = stackify_parent.item(index);
var flow_det = flow_details(item);
var function_list = [];
for (var i = 0, length = flow_det.function_name_list.length; i < length; i++) {
function_list.push(stackify_parent.options.steps[flow_det.function_name_list[i]]);
}
console.log("Executing- " + flow_det.flow_name);
stackify_processor.process({
'items': function_list,
'slab': 1,
'flow_name': flow_det.flow_name
}, stackify_parent);
}, function(index, stackify_parent) {
var item = stackify_parent.item(index);
var flow_det = flow_details(item);
if (print != "no") {
++i
console.log("Case: " + i + " Executed")
test_count++;
Pass++;
fs.appendFile(common_functions.myresults_path(), common_functions.result("TCID-" + test_count, test_data.test_detail[flow_det.flow_name], 'Pass', i));
}
debugger;
}, function(index, stackify_parent) {
callback(stackify_parent.requests);
}, 'stackify_parent_processor');
get_test_cases_list(function(test_cases) {
debugger;
if (config.execute == "flow") {
var flow = {};
var flow_type = "default";
try {
if (config.session.is_authenticated) {
flow_type = "authenticated";
} else {
flow_type = "un_authenticated";
}
} catch (e) {}
console.log("Executing " + flow_type + " flow.");
flow = test_flow[flow_type];
stackify_parent_processor.process({
'items': flow,
'steps': test_cases,
'slab': 1
});
} else {
var function_list = [];
for (var item in test_cases) {
function_list.push({
"name": item,
"func": test_cases[item]
});
}
stackify_processor.process({
'items': function_list,
'slab': 1
});
}
});
} catch (e) {
callback(e);
}
};
var runios = exports.runios = function(callback) {
console.log("Executing Run Function");
try {
debugger;
var stackify_processor = new STACKIFY(function(index, stackify) {
var item = stackify.item(index);
var flow_name = stackify.options.flow_name;
try {
item.call().done(function(output) { //call from cases file to perform
if (output == "Pass") {
print = "yes"; // if the child item return value as "Pass" then will it execute another child item else it will execute the another flow and make the test case as fail
stackify.next(index, "Function got Executed: " + index); // This will execue another child item
} else {
print = "no";
Fail++
++i
test_count++;
child = exec("idevicescreenshot ./result/" + flow_name + "", function(error, stdout, stderr) { // this allows you to take screenshot of failed testcase and store in the device memory
sys.print('stdout: ' + stdout);
sys.print('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
fs.appendFile(common_functions.myresults_path(), common_functions.result("TCID-" + test_count, test_data.test_detail[flow_name], 'Fail', flow_name + ".tiff")); // This will print the failed testcase in the result file
stackify.parent.next(stackify.parent.index, "Function got Executed: " + index); // this will directly jump to the another flow and will skip all the child items
}
});
} catch (e) {
stackify.error(index, e);
}
}, function(index, stackify) {}, function(index, stackify) {
stackify.parent.next(stackify.parent.index, "Function got Executed: " + index);
});
var flow_details = function(item) {
debugger;
var flow_name = "";
var function_name_list = [];
for (var key in item) {
debugger;
function_name_list = item[key];
flow_name = key;
}
return {
"flow_name": flow_name,
"function_name_list": function_name_list
};
};
var stackify_parent_processor = new STACKIFY(function(index, stackify_parent) {
debugger;
var item = stackify_parent.item(index);
var flow_det = flow_details(item);
var function_list = [];
for (var i = 0, length = flow_det.function_name_list.length; i < length; i++) {
function_list.push(stackify_parent.options.steps[flow_det.function_name_list[i]]);
}
console.log("Executing- " + flow_det.flow_name);
stackify_processor.process({
'items': function_list,
'slab': 1,
'flow_name': flow_det.flow_name
}, stackify_parent);
}, function(index, stackify_parent) {
var item = stackify_parent.item(index);
var flow_det = flow_details(item);
if (print != "no") {
++i
console.log("Case: " + i + " Executed")
test_count++;
Pass++;
console.log(Pass);
fs.appendFile(common_functions.myresults_path(), common_functions.result("TCID-" + test_count, test_data.test_detail[flow_det.flow_name], 'Pass', i));
}
debugger;
}, function(index, stackify_parent) {
callback(stackify_parent.requests);
}, 'stackify_parent_processor');
get_test_cases_list(function(test_cases) {
debugger;
if (config.execute == "flow") {
var flow = {};
var flow_type = "default";
try {
if (config.session.is_authenticated) {
flow_type = "authenticated";
} else {
flow_type = "un_authenticated";
}
} catch (e) {}
console.log("Executing " + flow_type + " flow.");
flow = test_flow[flow_type];
stackify_parent_processor.process({
'items': flow,
'steps': test_cases,
'slab': 1
});
} else {
var function_list = [];
for (var item in test_cases) {
function_list.push({
"name": item,
"func": test_cases[item]
});
}
stackify_processor.process({
'items': function_list,
'slab': 1
});
}
});
} catch (e) {
console.log("Error: ", e);
callback(e);
}
};