-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecballium.coffee
executable file
·381 lines (327 loc) · 9.45 KB
/
ecballium.coffee
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
window.imports={}
window.wait = (delay)->
d = $.Deferred()
cb = ()=>
console.log 'wait done',delay
d.resolve()
setTimeout cb,delay
d
# static storage
window.ecb_storage = {};
class Ecballium
state: 'init'
logbuf:''
skipScnOnError: true
animate:false
DELAY:5000
REPEAT_TIME:5
DELAY_FOR_REPEAT:1000
root:$(document)
window:window
storage:window.ecb_storage
WD_DELAY:30*1000;
wd:null
#navigator: "#{navigator.appCodeName} #{navigator.appName} #{navigator.appVersion} #{navigator.cookieEnabled} #{navigator.platform} #{navigator.userAgent}";
navigator: "#{navigator.appVersion} | #{navigator.platform}";
constructor:(opts)->
$.extend @,opts
@scripts = []
@loc =
file:0
scn:0
step:0
outline: 0
console.log '**** new ecb',@loc.step,@scripts
if not window.dbg_ecbs
window.dbg_ecbs = []
window.dbg_ecbs.push(@)
@URL = '/'+(window.location.pathname.split('/').slice(1,-1)).join('/')
if not @hash
@hash = window.location.hash.slice(1);
if @hash == ''
@hash = 'console'
console.log 'URL',@URL
$(this).on 'ecb_next', (e,state)=>
console.log 'ecb_next_trigger',state
e.stopImmediatePropagation()
@state_machine state
path = window.location.pathname.replace 'launcher.html','stub.html'
@W=opener;
@frame=$ @W.document
@scripts.push @hash
if @par
@scripts = @scripts.concat @par.scripts
if @console
@file = @compile_gerkhin @console.text
else
@file = @get_file @hash
wait(1000).done ()=> #debug delay
@init()
next: (state)->
console.log 'next',state
@state=state
$(this).trigger 'ecb_next',state
state_machine: (state,e)->
console.log 'state machine',state
switch state
#wait for modules loading
when 'find_next_step' then @find_next_step()
when 'step_ready'
if @inject()
if @loc.step == 0 #handle scenario begin
scn = @loc2scn()
@post 'msg',scn.name
@run_step()
else
# wait until injection comleted
wait(@DELAY/2+10).done ()=>
@next('step_ready')
when 'step_done' then @find_next_step()
when 'feature_done'
if @par
@par.last_child = @par.child
delete @par.child
@W.ecballiumbot.ecb = @par
@par.next('step_done')
else
@post('msg','all tests done')
#$.cookie('ecballium',null)
else
throw("unknown state #{state}")
init: ()->
if not @par
window.addEventListener "message"
,(e)=>
@run_on_target_done(null,e.data)
,false
@next 'step_ready'
get_file: (file)->
$.get("#{@URL}/#{file}.feature",null,null,'text')
.done (data)=>
if not @par
$('textarea.editor').val(data)
@file = @compile_gerkhin(data)
console.log 'compiled',@file
compile_gerkhin: (data)->
current_scenario = null;
n = 0;
gather_string = false;
curfile = {'scenarios':[]}
for i in data.split('\n').concat(['end step'])
n+=1;
ti=i.trim()
if gather_string
if ti=='"""'
gather_string = false
else
current_step[data]+="#{i}\n"
continue
if ti==''
continue
if i[0]=='#'
continue
if ti[0]=='|'
if 'data' not of current_step
current_step['data']=[]
current_step['data'].push(k.trim() for k in ti.slice(1,-1).split('|'))
continue
if ti=='"""'
gather_string=true;
current_step['data']=''
continue
#console.log 'current step',current_step,@files[file]
if current_step
if current_step.desc=='Examples:'
curfile['scenarios'].slice(-1)[0].outline=[]
outline=current_step.data
for k in outline[1..]
#console.log 'outline parse',k,outline
od={}
for l,ln in outline[0]
od[l]=k[ln]
curfile['scenarios'].slice(-1)[0].outline.push(od)
else
curfile['scenarios'].slice(-1)[0].steps.push(current_step)
current_step=null
re=ti.replace /^Scenario:/,''
if re!=ti
current_screnario=re
curfile['scenarios'].push({name:re.trim(),steps:[]})
continue
re=ti.replace /^Feature:/,''
if re!=ti
curfile['feature_name']=re.trim()
continue
current_step={desc:ti,line:n}
curfile
inject_script: (name)->
try
fh=@frame.find('head')
script= document.createElement('script')
script.type = 'text/javascript'
script.src = "#{@URL}/#{name}.js"
script.charset = 'UTF-8'
fh[0].appendChild(script)
$(script).attr('x-injected','')
catch e
console.log("script #{name} not found")
inject: ()->
@frame=$(@W.document)
fh = @frame.find('head')
scr = fh.find('script[x-injected]')
if scr.length==0
@inject_script('ecballiumbot')
wait(2010).done ()=>
@W.ecballiumbot.ecb = @
@inject_script 'lib'
for i in @scripts
@inject_script i
return false
return true
find_next_step:()->
#check if there are another steps in scenario
scn = @loc2scn()
@loc.step+=1
if @loc.step>=scn.steps.length
@loc.step=0
if 'outline' of scn
@loc.outline+=1
if @loc.outline>=scn.outline.length
@loc.outline=0
else
@loc.scn-=1 #repeat scenario again
@loc.scn+=1
#check if there are another scenarios in file
file=@file
if @loc.scn>=file.scenarios.length
@next 'feature_done'
return
@next 'step_ready'
on_scenario_change: ()->
@root=$(document)
loc2step:()->
scn=@loc2scn()
tmp=$.extend {},scn.steps[@loc.step]
if 'outline' of scn
outline = scn.outline[@loc.outline]
tmp.desc = tmp.desc.replace /(<[^<>]+>)/,(v)->
#console.log 'outline replace',outline,v.slice(1,-1)
outline[v.slice(1,-1)]
tmp
loc2scn:()->
@file.scenarios[@loc.scn]
run_step:()->
@current_step = @loc2step()
step = @current_step.desc
console.log 'run_step',step,@file,@loc.step
@post('pre')
@W.postMessage step,"#{@W.location.protocol}//#{@W.location.host}"
#set WatchDog
@wd = wait(@WD_DELAY);
@wd.done ()=>
@run_on_target_done null,'watch_dog'
log: (msg,obj)->
@logbuf+="#{new Date()} #{msg}\n"
@jsonobj=obj
@logbuf+=JSON.stringify(obj,@replacer,1)
replacer: (key,value)->
#console.log 'replacer',key,value,$.type value
out=value
if (value==window)
out = '[window]'
if (value==document)
out = '[document]'
if value and value.tagName
v=$(value)
out=
tag:v[0].tagName
attrs: [i.name,i.value] for i in v[0].attributes
#console.log 'replacer out',out
out
post: (status,msg='')->
if status=='msg'
data=
step: msg
log: @logbuf
navigator:@navigator
else if status == 'pre'
step=@loc2step()
li=$("<dt class='pre_msg'>#{step.desc}</dt>")
$('.log dl').append(li)
return
else
step=@loc2step()
data=
msg:msg
file: @hash
step: step.desc
line: step.line
log: @logbuf
navigator:@navigator
@logbuf=''
#$.post('/test',{status:status,data:JSON.stringify data,null,1})
console.log '===',status,' = ',data.step,data
msg_el = "#{status} #{data.step}"
if data.msg
msg_el+="<div class='colapsible hidden'><pre>#{data.msg}</pre></div>"
li=$("<dt class='status_#{status}'>#{msg_el}</dt>")
$('.log dl dt.pre_msg').remove()
$('.log dl').append(li)
li.find('.colapsible').click ()->
$(this).toggleClass("hidden");
run_on_target_done: (data,status)->
console.log 'run_on_target_done',status
if @child
@child.run_on_target_done(data,status)
return
#cancel WatchDog
@wd.reject()
if status == 'redirected'
@post('success')
wait(@DELAY/2).done ()=> #wait until page reloaded
@next 'step_done'
else if status == 'failed'
msg = if @storage.last_exception then @storage.last_exception.stack else 'Unknown'
@post 'failed', msg
if not @stop_on_any
@next 'step_done'
else if status == 'error'
msg = if @storage.last_exception then @storage.last_exception.stack else 'Unknown'
@post('failed',msg)
@loc.step = 1e10
if not @stop_on_any
@next 'step_done'
else if status == 'run_feature'
@post('success')
@run_feature @pending_feature
else if status == 'load_library'
@post('success')
@inject_script @scripts.slice(-1)[0]
wait(@DELAY/2).done ()=>
@next 'step_done'
else if status == "watch_dog"
@post('failed','Watch Dog Timer')
if not @stop_on_any
@next 'step_done'
else
@post('success')
if @after_step_delay
wait(@after_step_delay).done ()=>
@next 'step_done'
@after_step_delay = null
else
@next 'step_done'
run_feature: (f)->
@child = new Ecballium
'par':@
'hash':f
$ ->
window.ecballium = new Ecballium()
$('button.run_script').on 'click',()->
console.log $('textarea.editor').val()
ecballium.child = new Ecballium
'par':ecballium
'console':
'text':$('textarea.editor').val()
$('button.clean-log').on 'click',()->
$('div.content dl').empty()