auto_test 0.0.7.4 → 0.0.7.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,991 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'auto_test/lib/auto_test'
5
+ #
6
+ # describe "Application" do
7
+ #
8
+ # describe "error path reduction" do
9
+ # it "reduces the path to the error" do
10
+ # load "#{Rails.root}/db/test_seeds.rb"
11
+ # init_path
12
+ # init_user_inputs
13
+ # init_sessions
14
+ # init_sessions_array
15
+ # number_of_sessions = user_inputs("get_number_of_sessions").to_i
16
+ # for i in 0..number_of_sessions-1 do
17
+ # sess = Capybara::Session.new(:rack_test,Rails.application)
18
+ # add_to_sessions_array sess
19
+ # init_hash_sessions(i, Hash.new)
20
+ # end
21
+ # init_err_file
22
+ # lno = 0
23
+ # paths = File.new("lib/auto_test/log/paths.log")
24
+ # user = Array.new(3)
25
+ # session_index = 0
26
+ # begin
27
+ # puts "try_path 1"
28
+ # puts Time.now
29
+ # while line = paths.gets do
30
+ # lno = lno + 1
31
+ # # puts lno
32
+ # if !line.chop!.end_with? "ID" then
33
+ # hash = Hash.new
34
+ #
35
+ # a = line.gsub!(/[{}\"]/,'').split(/\=\>/)
36
+ # session = get_sessions_array[a[0].split(":").first.to_i]
37
+ # session_index = get_sessions_array.index(session)
38
+ # if a[0].split(":").second == user_inputs("get_login_path") then
39
+ # begin
40
+ # # necesarry if user data has changed
41
+ # # might have to be rescued if the user doesn´t exist anymore.
42
+ # user[session_index] = user[session_index].class.find(user[session_index].id)
43
+ # rescue
44
+ # end
45
+ # session.visit user_inputs("get_login_path")
46
+ # hash["#{get_sessions_array.index(session)}:#{session.current_path}"] = session.all('input').find_all{ |i| i[:type] == "submit"}.first.value
47
+ # if user_inputs("use_db_users") then
48
+ # user_inputs("get_login_attributes").each do |field|
49
+ # session.fill_in field[1], :with => user[session_index].send(field[0].to_sym)
50
+ # end
51
+ # else
52
+ # index = user_inputs("get_login_names").index(user_inputs("get_unique_login_attribute_name"))
53
+ # user[session_index].class.find(:all).each do |u|
54
+ # user_inputs("get_login_data").each do |d|
55
+ # if d[index] == u.send(user_inputs("get_unique_login_attribute_name").to_sym) then
56
+ # user_data = user_inputs("get_login_data")[index]
57
+ # end
58
+ # end
59
+ # end
60
+ # user_inputs("get_login_attributes").each_with_index do |field, i|
61
+ # session.fill_in field[1], :with => user_data[i]
62
+ # end
63
+ # end
64
+ # session.all('input').find_all{ |i| i[:type] == "submit"}.first.click
65
+ # elsif a[1] == user_inputs("get_logout_path") then
66
+ # hash["#{get_sessions_array.index(session)}:#{session.current_path}"] = user_inputs("get_logout_path")
67
+ # session.visit user_inputs("get_logout_path")
68
+ # elsif a[1][0] == "[" then
69
+ # texts = []
70
+ # inputs = a[1][1,a[1].size-2].split(/,/)
71
+ # i = 0
72
+ # while i < inputs.size do
73
+ # texts << inputs[i].strip
74
+ # texts << inputs[i+1].strip
75
+ # if inputs[i].strip.start_with? "radio___" then
76
+ # session.choose inputs[i + 1].strip
77
+ # elsif inputs[i].strip.start_with? "select___" then
78
+ # session.select inputs[i + 1].strip, :from => inputs[i].gsub("select___",'').strip
79
+ # else
80
+ # input = session.all('input').find{ |inp| inp[:name] == inputs[i].strip}
81
+ # input.set(inputs[i+1].strip)
82
+ # end
83
+ # i = i + 2
84
+ # end
85
+ # hash["#{get_sessions_array.index(session)}:#{session.current_path}"] = texts
86
+ # session.all('input').find_all{ |i| i[:type] == "submit"}.first.click
87
+ # else
88
+ # link = a[1].split(/\+\+\+/)
89
+ # href = link[0]
90
+ # text = link[1]
91
+ # link = session.all('a').find{ |l| l[:href] == href && l.text == text }
92
+ # hash["#{get_sessions_array.index(session)}:#{session.current_path}"] = link[:href].to_s + "+++" + link.text.to_s
93
+ # link.click
94
+ # end
95
+ # add_path(hash)
96
+ #
97
+ # else
98
+ # add_path(line)
99
+ # line_parts = line.split(":")
100
+ # session_index = line_parts[0].to_i
101
+ # id = line_parts[1].to_i
102
+ # # get the Constant from the String, to find the right class
103
+ # user[session_index] = Kernel.const_get(user_inputs("user_class")).find(id)
104
+ # end
105
+ # end
106
+ # rescue => e
107
+ # if err_message == e.message.to_s.split(/:/,2).first then
108
+ # # puts "Found error again!"
109
+ # add_path(hash)
110
+ # last_path = path + []
111
+ # puts Time.now
112
+ #
113
+ # puts "Replace"
114
+ # puts Time.now
115
+ # replace_same_users!(last_path)
116
+ # puts Time.now
117
+ # user_count = user_count(last_path)
118
+ # puts "Half"
119
+ # puts Time.now
120
+ # # test the path, getting divided
121
+ # for i in 1..user_count do
122
+ # error_not_found = false
123
+ # index = nil
124
+ # path_still_changing = true
125
+ # last_index = nil
126
+ # while !error_not_found && path_still_changing do
127
+ # return_val = delete_links_half_way(last_path, index, i)
128
+ # index = return_val[1]
129
+ # if index == last_index then
130
+ # path_still_changing = false
131
+ # else
132
+ # last_index = index
133
+ # path_still_changing = true
134
+ # end
135
+ # next_path = return_val[0]
136
+ # error_not_found = try_path(next_path)
137
+ # while error_not_found do
138
+ # return_val = delete_links_half_way(last_path, index, i)
139
+ # index = return_val[1]
140
+ # next_path = return_val[0]
141
+ # error_not_found = try_path(next_path)
142
+ # end
143
+ # if !error_not_found then
144
+ # last_path = next_path
145
+ # end
146
+ # index = nil
147
+ # end
148
+ # end
149
+ # puts Time.now
150
+ #
151
+ # puts "Jump"
152
+ # puts Time.now
153
+ # new_path = jump_to_last_user(last_path)
154
+ # init_enlarging_counter
155
+ # error_not_found = true
156
+ # # test the jump_to_last_user path
157
+ # while error_not_found do
158
+ # error_not_found = try_path(new_path)
159
+ # if error_not_found then
160
+ # new_path = enlarge_path(last_path)
161
+ # end
162
+ # end
163
+ # puts Time.now
164
+ # last_path = new_path
165
+ #
166
+ #
167
+ # puts "Delete Groups"
168
+ # puts Time.now
169
+ # last_path = delete_groups(last_path)
170
+ # puts Time.now
171
+ #
172
+ # puts "Single"
173
+ # puts Time.now
174
+ # last_path = delete_single_links(last_path)
175
+ # puts Time.now
176
+ # puts "Delete IDs"
177
+ # puts Time.now
178
+ # delete_ids(last_path)
179
+ # puts Time.now
180
+ # f = File.new("lib/auto_test/log/new_path.log", "w")
181
+ # f.puts last_path
182
+ # f.close
183
+ # else
184
+ # puts "Different error, stopping..."
185
+ # puts e
186
+ # end
187
+ # end
188
+ # end
189
+ # end
190
+ # end
191
+ #
192
+
193
+
194
+
195
+ describe "Application" do
196
+
197
+ describe "error path reduction" do
198
+ it "reduces the path to the error" do
199
+ load "#{Rails.root}/db/test_seeds.rb"
200
+ init_path
201
+ init_user_inputs
202
+ init_sessions
203
+ init_sessions_array
204
+ number_of_sessions = user_inputs("get_number_of_sessions").to_i
205
+ for i in 0..number_of_sessions-1 do
206
+ sess = Capybara::Session.new(:rack_test,Rails.application)
207
+ add_to_sessions_array sess
208
+ init_hash_sessions(i, Hash.new)
209
+ end
210
+ init_err_file
211
+ lno = 0
212
+ paths = File.new("lib/auto_test/log/paths.log")
213
+ user = Array.new(number_of_sessions)
214
+ session_index = 0
215
+ begin
216
+ puts "try_path 1"
217
+ puts Time.now
218
+ while line = paths.gets do
219
+ lno = lno + 1
220
+ # puts lno
221
+ if !line.chop!.end_with? "ID" then
222
+ hash = Hash.new
223
+
224
+ a = line.gsub!(/[{}\"]/,'').split(/\=\>/)
225
+ session = get_sessions_array[a[0].split(":").first.to_i]
226
+ session_index = get_sessions_array.index(session)
227
+ if a[0].split(":").second == user_inputs("get_login_path") then
228
+ begin
229
+ # necesarry if user data has changed
230
+ # might have to be rescued if the user doesn´t exist anymore.
231
+ user[session_index] = user[session_index].class.find(user[session_index].id)
232
+ rescue
233
+ end
234
+ session.visit user_inputs("get_login_path")
235
+ hash["#{get_sessions_array.index(session)}:#{session.current_path}"] = session.all('input').find_all{ |i| i[:type] == "submit"}.first.value
236
+ if user_inputs("use_db_users") then
237
+ user_inputs("get_login_attributes").each do |field|
238
+ session.fill_in field[1], :with => user[session_index].send(field[0].to_sym)
239
+ end
240
+ else
241
+ index = user_inputs("get_login_names").index(user_inputs("get_unique_login_attribute_name"))
242
+ user[session_index].class.find(:all).each do |u|
243
+ user_inputs("get_login_data").each do |d|
244
+ if d[index] == u.send(user_inputs("get_unique_login_attribute_name").to_sym) then
245
+ user_data = user_inputs("get_login_data")[index]
246
+ end
247
+ end
248
+ end
249
+ user_inputs("get_login_attributes").each_with_index do |field, i|
250
+ session.fill_in field[1], :with => user_data[i]
251
+ end
252
+ end
253
+ session.all('input').find_all{ |i| i[:type] == "submit"}.first.click
254
+ elsif a[1] == user_inputs("get_logout_path") then
255
+ hash["#{get_sessions_array.index(session)}:#{session.current_path}"] = user_inputs("get_logout_path")
256
+ session.visit user_inputs("get_logout_path")
257
+ elsif a[1][0] == "[" then
258
+ texts = []
259
+ inputs = a[1][1,a[1].size-2].split(/,/)
260
+ i = 0
261
+ while i < inputs.size do
262
+ texts << inputs[i].strip
263
+ texts << inputs[i+1].strip
264
+ if inputs[i].strip.start_with? "radio___" then
265
+ session.choose inputs[i + 1].strip
266
+ elsif inputs[i].strip.start_with? "select___" then
267
+ session.select inputs[i + 1].strip, :from => inputs[i].gsub("select___",'').strip
268
+ elsif inputs[i].strip.start_with? "checkbox___" then
269
+ session.check inputs[i + 1].strip
270
+ else
271
+ input = session.all('input').find{ |inp| inp[:name] == inputs[i].strip}
272
+ input.set(inputs[i+1].strip)
273
+ end
274
+ i = i + 2
275
+ end
276
+ hash["#{get_sessions_array.index(session)}:#{session.current_path}"] = texts
277
+ session.all('input').find_all{ |i| i[:type] == "submit"}.first.click
278
+ else
279
+ link = a[1].split(/\+\+\+/)
280
+ href = link[0]
281
+ text = link[1]
282
+ link = session.all('a').find{ |l| l[:href] == href && l.text == text }
283
+ hash["#{get_sessions_array.index(session)}:#{session.current_path}"] = link[:href].to_s + "+++" + link.text.to_s
284
+ link.click
285
+ end
286
+ add_path(hash)
287
+
288
+ else
289
+ add_path(line)
290
+ line_parts = line.split(":")
291
+ session_index = line_parts[0].to_i
292
+ id = line_parts[1].to_i
293
+ # get the Constant from the String, to find the right class
294
+ user[session_index] = Kernel.const_get(user_inputs("user_class")).find(id)
295
+ end
296
+ end
297
+ rescue => e
298
+ if err_message == e.message.to_s.split(/:/,2).first then
299
+ # puts "Found error again!"
300
+ add_path(hash)
301
+ puts Time.now
302
+ puts "Jump"
303
+ puts Time.now
304
+ new_path = jump_to_last_user(path)
305
+ init_enlarging_counter
306
+ error_not_found = true
307
+ # test the jump_to_last_user path
308
+ while error_not_found do
309
+ error_not_found = try_path(new_path)
310
+ if error_not_found then
311
+ new_path = enlarge_path(path)
312
+ end
313
+ end
314
+ puts Time.now
315
+ last_path = new_path
316
+
317
+ puts "Replace"
318
+ puts Time.now
319
+ replace_same_users!(last_path)
320
+ puts Time.now
321
+ user_count = user_count(last_path)
322
+ puts "Half"
323
+ puts Time.now
324
+ # test the path, getting divided
325
+ for i in 1..user_count do
326
+ error_not_found = false
327
+ index = nil
328
+ path_still_changing = true
329
+ last_index = nil
330
+ while !error_not_found && path_still_changing do
331
+ return_val = delete_links_half_way(last_path, index, i)
332
+ index = return_val[1]
333
+ if index == last_index then
334
+ path_still_changing = false
335
+ else
336
+ last_index = index
337
+ path_still_changing = true
338
+ end
339
+ next_path = return_val[0]
340
+ error_not_found = try_path(next_path)
341
+ while error_not_found do
342
+ return_val = delete_links_half_way(last_path, index, i)
343
+ index = return_val[1]
344
+ next_path = return_val[0]
345
+ error_not_found = try_path(next_path)
346
+ end
347
+ if !error_not_found then
348
+ last_path = next_path
349
+ end
350
+ index = nil
351
+ end
352
+ end
353
+ puts Time.now
354
+
355
+ puts "Delete Groups"
356
+ puts Time.now
357
+ last_path = delete_groups(last_path)
358
+ puts Time.now
359
+
360
+ puts "Single"
361
+ puts Time.now
362
+ last_path = delete_single_links(last_path)
363
+ puts Time.now
364
+ puts "Delete IDs"
365
+ puts Time.now
366
+ delete_ids(last_path)
367
+ puts Time.now
368
+ f = File.new("lib/auto_test/log/new_path.log", "w")
369
+ f.puts last_path
370
+ f.close
371
+ else
372
+ puts "Different error, stopping..."
373
+ puts e
374
+ end
375
+ end
376
+ end
377
+ end
378
+ end
379
+ def delete_links_without_input(new_path, number_of_links, session)
380
+ next_path = []
381
+ no_inputs = true
382
+ tmp = []
383
+ got_one = false
384
+ done = false
385
+ new_path.each do |n|
386
+ done = false
387
+ # check if the line is an ID
388
+ if n.class == String then
389
+ # check if it´s the right session
390
+ if n.to_i == session then
391
+ # add the line to the path
392
+ next_path << n
393
+ # save, that the part before was processed
394
+ done = true
395
+ # save, that the right session id has been found
396
+ got_one = true
397
+ else
398
+ # for other session ids add the line to the tmp array
399
+ tmp << n
400
+ end
401
+ else
402
+ current_session = n.keys[0].split(":").first.to_i
403
+ # if the right session id hasn´t been found, add the line to the path
404
+ if current_session != session && !got_one then
405
+ next_path << n
406
+ # else add the line to the tmp array
407
+ else
408
+ tmp << n
409
+ # check for input data for forms
410
+ if n.values[0].class == Array && current_session == session then
411
+ no_inputs = false
412
+ end
413
+ # if the part of the session to be deleted is finished,
414
+ # has no inputs and is smaller or equal to the wanted number of links,
415
+ # delete the links of the session, other links get added to the path
416
+ if n.values[0] == user_inputs("get_logout_path") && no_inputs && current_session == session then
417
+ if tmp.select{|x| x.class != String && x.keys[0].split(":").first.to_i == session}.size == number_of_links then
418
+ tmp.delete_if{|x| x.class != String && x.keys[0].split(":").first.to_i == session}
419
+ next_path << tmp
420
+ tmp = []
421
+ done = true
422
+ got_one = false
423
+ else
424
+ next_path << tmp
425
+ tmp = []
426
+ done = true
427
+ got_one = false
428
+ end
429
+ elsif n.values[0] == user_inputs("get_logout_path") && current_session == session then
430
+ next_path << tmp
431
+ tmp = []
432
+ done = true
433
+ no_inputs = true
434
+ got_one = false
435
+ end
436
+ end
437
+ end
438
+ end
439
+ if !done then
440
+ next_path << tmp
441
+ end
442
+ return next_path.flatten
443
+ end
444
+
445
+
446
+ def delete_ids(new_path)
447
+ for i in 0..get_sessions_array.size - 1 do
448
+ found_session = false
449
+ index_to_delete = nil
450
+ new_path.each_with_index do |n, index|
451
+ if n.class == String && n.to_i == i then
452
+ index_to_delete = index
453
+ if !found_session && index_to_delete != index then
454
+ new_path.delete_at(index_to_delete)
455
+ end
456
+ found_session = false
457
+ else
458
+ if (n.class != String && n.keys[0].split(":").first.to_i == i) then
459
+ found_session = true
460
+ end
461
+ end
462
+ end
463
+ if !found_session then
464
+ new_path.delete_at(index_to_delete)
465
+ end
466
+ found_session = false
467
+ end
468
+ end
469
+
470
+ def delete_groups(new_path)
471
+ for i in 0..get_sessions_array.size - 1 do
472
+ no_of_group = 1
473
+ no_of_groups = 1
474
+ tmp = []
475
+ while no_of_group <= no_of_groups do
476
+ count = 0
477
+ no_of_groups = 0
478
+ next_path = []
479
+ new_path.each_with_index do |n,ind|
480
+ if n.class != String then
481
+ if n.keys[0].split(":").second == user_inputs("get_login_path") && n.keys[0].split(":").first.to_i == i then
482
+ count = count + 1
483
+ end
484
+ if !(n.keys[0].split(":").first.to_i == i && count == no_of_group) then
485
+ next_path << n
486
+ else
487
+ tmp << n
488
+ end
489
+ else
490
+ next_path << n
491
+ end
492
+ end
493
+ error_not_found = try_path(next_path)
494
+ if error_not_found && tmp != [] then
495
+ no_of_group = no_of_group + 1
496
+ else
497
+ new_path = next_path
498
+ end
499
+ new_path.each do |n|
500
+ if n.class != String && n.keys[0].split(":").first.to_i == i && n.values[0] == user_inputs("get_logout_path") then
501
+ no_of_groups = no_of_groups + 1
502
+ end
503
+ end
504
+ tmp = []
505
+ end
506
+ end
507
+ return new_path
508
+ puts new_path
509
+ end
510
+
511
+ def delete_single_links(new_path)
512
+ i = 0
513
+ while i < new_path.size do
514
+ if (new_path[i].class != String &&
515
+ !(new_path[i].values.include? user_inputs("get_logout_path")) &&
516
+ !(new_path[i].keys[0].include? user_inputs("get_login_path"))) then
517
+ next_path = new_path[0..i-1]+new_path[i+1..new_path.size]
518
+ error_not_found = try_path(next_path)
519
+ if !error_not_found then
520
+ new_path = next_path
521
+ else
522
+ i = i + 1
523
+ end
524
+ else
525
+ i = i + 1
526
+ end
527
+ end
528
+ return new_path
529
+ end
530
+
531
+ def delete_links_half_way(new_path, indx, user)
532
+ next_path = []
533
+ index = 0
534
+ id = 0
535
+ first_path = []
536
+ middle_path = []
537
+ while id <= user && index < new_path.size do
538
+ if new_path[index].class == String then
539
+ id = id + 1
540
+ if id < user || new_path[index + 1].class == String then
541
+ if new_path[index + 1].class == String then
542
+ id = id - 1
543
+ end
544
+ first_path << new_path[index]
545
+ index = index + 1
546
+ else
547
+ if id == user then
548
+ middle_path << new_path[index]
549
+ index = index + 1
550
+ end
551
+ end
552
+ elsif id < user then
553
+ first_path << new_path[index]
554
+ index = index + 1
555
+ else
556
+ middle_path << new_path[index]
557
+ index = index + 1
558
+ end
559
+ end
560
+ if indx == nil then
561
+ tmp = middle_path[0..middle_path.size/2]
562
+ else
563
+ tmp = middle_path[0..indx]
564
+ end
565
+ middle_path = tmp
566
+ last_path = []
567
+ for i in index..new_path.size-1 do
568
+ last_path << new_path[i]
569
+ end
570
+ next_path = first_path + middle_path + last_path
571
+ return [next_path, middle_path.size]
572
+ end
573
+
574
+ def replace_same_users!(new_path)
575
+ users = Array.new(get_sessions_array.size)
576
+ last_paths = Array.new(get_sessions_array.size)
577
+ i = 0
578
+ while i < new_path.size do
579
+ if new_path[i].class == String then
580
+ session = new_path[i].split(":").first.to_i
581
+ if users[session] == new_path[i] then
582
+ hash = Hash.new
583
+ hash[last_paths[new_path[i].split(":").first.to_i]] = user_inputs("get_logout_path")
584
+ new_path[i] = hash
585
+ else
586
+ users[session] = new_path[i]
587
+ end
588
+ else
589
+ last_paths[new_path[i].keys[0].split(":").first.to_i] = new_path[i]
590
+ end
591
+ i = i + 1
592
+ end
593
+ end
594
+
595
+ def try_path(path_to_try)
596
+ clean_and_seed_db
597
+ lno = 0
598
+ init_sessions
599
+ init_sessions_array
600
+ number_of_sessions = user_inputs("get_number_of_sessions").to_i
601
+ for i in 0..number_of_sessions-1 do
602
+ sess = Capybara::Session.new(:rack_test,Rails.application)
603
+ add_to_sessions_array sess
604
+ init_hash_sessions(i, Hash.new)
605
+ end
606
+ user = Array.new(number_of_sessions)
607
+ session_paths = Array.new(number_of_sessions, "")
608
+ old_session_paths = Array.new(number_of_sessions, "")
609
+ session_index = 0
610
+ error_not_found = true
611
+ begin
612
+ lno = 0
613
+ # puts lno
614
+ while lno < path_to_try.size do
615
+ hash = Hash.new
616
+ line = path_to_try[lno]
617
+ if line.class == String then
618
+ session = get_sessions_array[line.split(":").first.to_i]
619
+ else
620
+ session = get_sessions_array[line.keys[0].split(":").first.to_i]
621
+ session_path = line.keys[0].split(":").second
622
+ end
623
+ session_index = get_sessions_array.index(session)
624
+ lno = lno + 1
625
+ # puts lno
626
+ if line.class == Hash then
627
+ if line.values[0] == "Anmelden" then
628
+ session.visit user_inputs("get_login_path")
629
+ hash["#{session_index}:#{session.current_path}"] = session.all('input').find_all{ |i| i[:type] == "submit"}.first.value
630
+ if user_inputs("use_db_users") then
631
+ user_inputs("get_login_attributes").each do |field|
632
+ session.fill_in field[1], :with => user[session_index].send(field[0].to_sym)
633
+ end
634
+ else
635
+ index = user_inputs("get_login_names").index(user_inputs("get_unique_login_attribute_name"))
636
+ user[session_index].class.find(:all).each do |u|
637
+ user_inputs("get_login_data").each do |d|
638
+ if d[index] == u.send(user_inputs("get_unique_login_attribute_name").to_sym) then
639
+ user_data = user_inputs("get_login_data")[index]
640
+ end
641
+ end
642
+ end
643
+ user_inputs("get_login_attributes").each_with_index do |field, i|
644
+ session.fill_in field[1], :with => user_data[i]
645
+ end
646
+ end
647
+ session.all('input').find_all{ |i| i[:type] == "submit"}.first.click
648
+ elsif line.values[0] == user_inputs("get_logout_path") then
649
+ hash["#{session_index}:#{session.current_path}"] = user_inputs("get_logout_path")
650
+ session.visit user_inputs("get_logout_path")
651
+ elsif line.values[0].class == Array then
652
+ inputs = line.values[0]
653
+ if !session.all('input').empty? then
654
+ i = 0
655
+ while i < inputs.size do
656
+ if inputs[i].strip.start_with? "radio___" then
657
+ session.choose inputs[i + 1].strip
658
+ elsif inputs[i].strip.start_with? "select___" then
659
+ session.select inputs[i + 1].strip, :from => inputs[i].gsub("select___",'').strip
660
+ elsif inputs[i].strip.start_with? "checkbox___" then
661
+ session.check inputs[i + 1].strip
662
+ else
663
+ input = session.all('input').find{ |inp| inp[:name] == inputs[i].strip}
664
+ input.set(inputs[i+1].strip)
665
+ end
666
+ i = i + 2
667
+ end
668
+ hash["#{session_index}:#{session.current_path}"] = path_to_try[lno - 1].values[0]
669
+ session.all('input').find_all{ |i| i[:type] == "submit"}.first.click
670
+ else
671
+ hash = path_to_try[lno-1]
672
+ end
673
+ else
674
+ if session.current_path.delete("1-9") != session_path.delete("1-9") then
675
+ session.visit session_path
676
+ end
677
+ link = line.values[0].split(/\+\+\+/)
678
+ href = link[0].to_s
679
+ text = link[1]
680
+ path = session.current_path
681
+ root = find_next_path(session_index, path_to_try, lno)
682
+ old_p = href.split("/")
683
+ # => check for ids in string
684
+ links = session.all('a').select{ |l| (l[:href].to_s.delete("0-9") == href.delete("0-9")) && l.text == text}
685
+ links_selected = links + []
686
+ j = 1
687
+ compare_string = ""
688
+ new_split = session_paths[session_index].split("/")
689
+ old_split = old_session_paths[session_index].split("/")
690
+ while old_p[j] == old_split[j] && old_p[j] != nil do
691
+ if new_split[j] != nil then
692
+ compare_string << ("/" + new_split[j])
693
+ if old_p[j].to_i.to_s == old_p[j] then
694
+ links_selected = links_selected.select{ |l|
695
+ l[:href].to_s[0..compare_string.size-1] == compare_string }
696
+ end
697
+ end
698
+ j = j + 1
699
+ end
700
+ for i in 0..get_sessions_array.size - 1 do
701
+ compare_string = ""
702
+ if i != session_index then
703
+ old_sess_p = old_session_paths[i].split("/")
704
+ new_sess_p = session_paths[i].split("/")
705
+ j = 1
706
+ while old_p[j] == old_sess_p[j] && old_p[j] != nil do
707
+ compare_string << "/" + new_sess_p[j]
708
+ j = j + 1
709
+ end
710
+ links_selected = links_selected.select{ |l| l[:href].start_with? compare_string }
711
+ if new_sess_p[j] != nil then
712
+ compare_string << ("/" + new_sess_p[j] )
713
+ end
714
+ # if the string is an id
715
+ if old_p[j].to_i.to_s == old_p[j] && new_sess_p[j] != nil && new_sess_p[j].to_i.to_s == new_sess_p[j] then
716
+ links_selected = links_selected.select{ |l| !(l[:href].start_with? compare_string) }
717
+ end
718
+ end
719
+ end
720
+ if links.size > 0 then
721
+ if links_selected.size > 0 then
722
+ link = links_selected[session_index % links_selected.size]
723
+ else
724
+ link = links[session_index % links.size]
725
+ end
726
+ old_session_paths[session_index] = href
727
+ session_paths[session_index] = link[:href].to_s
728
+ hash["#{session_index}:#{path}"] = "#{link[:href]}+++#{link.text}"
729
+ link.click
730
+ next_path = session.current_path.delete("0-9")
731
+ if next_path != root then
732
+ session.visit path
733
+ end
734
+ else
735
+ hash = path_to_try[lno - 1]
736
+ end
737
+ end
738
+ path_to_try[lno-1].replace(hash)
739
+ else
740
+ # if there´s no authorization, no Strings will be saved in the path
741
+ if user_inputs("use_db_users") then
742
+ id = line.split(":").second.to_i
743
+ begin
744
+ user[session_index] = Kernel.const_get(user_inputs("user_class")).find(id)
745
+ rescue
746
+ index = lno + 1
747
+ root_path = ""
748
+ while root_path == "" && index < path_to_try.size do
749
+ if path_to_try[index].class != String then
750
+ if path_to_try[index].keys[0].split(":").first.to_i == session_index then
751
+ if path_to_try[index].keys[0].split(":").second != user_inputs("get_login_path") then
752
+ root_path = path_to_try[index].keys[0].split(":").second
753
+ end
754
+ end
755
+ index = index + 1
756
+ else
757
+ index = index + 1
758
+ end
759
+ end
760
+ user[session_index] = find_next_user(root_path, session)
761
+ end
762
+ else
763
+ user[session_index] = Kernel.const_get(user_inputs("user_class")).find(id)
764
+ end
765
+ if user[session_index] == false then
766
+ return error_not_found
767
+ else
768
+ path_to_try[lno - 1] = "#{session_index}:#{user[session_index].id}ID"
769
+ end
770
+ end
771
+ end
772
+ rescue => e
773
+ if err_message == e.message.to_s.split(/:/,2).first then
774
+ #puts "Found Error again, go on..."
775
+ error_not_found = false
776
+ else
777
+ f = File.new("lib/auto_test/log/debugging.log", "w")
778
+ f.puts path_to_try
779
+ f.close
780
+ # puts "Different Error:" + e.message.to_s
781
+ # puts lno
782
+ # puts line
783
+ error_not_found = true
784
+ end
785
+ return error_not_found
786
+
787
+ end
788
+ return error_not_found
789
+ end
790
+
791
+ def find_next_path(session, path_to_try, lno)
792
+ root = ""
793
+ stop = false
794
+ while lno < path_to_try.size && !stop do
795
+ line = path_to_try[lno]
796
+ if line.class != String && line.keys[0].split(":").first.to_i == session then
797
+ root = line.keys[0].split(":").second.delete("0-9")
798
+ stop = true
799
+ elsif line.class == String && line.split(":").first.to_i == session then
800
+ stop = true
801
+ end
802
+ lno = lno + 1
803
+ end
804
+ return root
805
+ end
806
+
807
+ def find_next_user(root_path, session)
808
+ index = 0
809
+ u = 0
810
+ user_size = Kernel.const_get(user_inputs("user_class")).find(:all).size
811
+ while u < user_size do
812
+ user = Kernel.const_get(user_inputs("user_class")).find(:first, :conditions => "id > #{index}")
813
+ index = user.id
814
+ session.visit user_inputs("get_login_path")
815
+ user_inputs("get_login_attributes").each do |la|
816
+ session.fill_in la[1], :with => user.send(la[0].to_sym)
817
+ end
818
+ session.all('input').find_all{ |i| i[:type] == "submit"}.first.click
819
+ if session.current_path == root_path then
820
+ return user
821
+ end
822
+ u = u + 1
823
+ end
824
+ return false
825
+ end
826
+
827
+ def enlarging_counter
828
+ @counter
829
+ end
830
+
831
+ def inc_enlarging_counter
832
+ @counter = @counter + 1
833
+ end
834
+
835
+ def init_enlarging_counter
836
+ @counter = 0
837
+ end
838
+
839
+ def max_enlarging_counter(whole_path)
840
+ count = 0
841
+ whole_path.each do |e|
842
+ if e.class == String && (e.end_with? "ID") then
843
+ count = count + 1
844
+ end
845
+ end
846
+ return count
847
+ end
848
+
849
+ # => could be optimized
850
+ # depending on enlarging_counter add another part to the path
851
+ def enlarge_path(path_to_enlarge)
852
+ if enlarging_counter < max_enlarging_counter(path_to_enlarge)-1 then
853
+ inc_enlarging_counter
854
+ else
855
+ return path_to_enlarge
856
+ end
857
+ last_path = jump_to_last_user(path_to_enlarge)
858
+ additional_path = []
859
+ id_count = 0
860
+ index = 0
861
+ while id_count < enlarging_counter do
862
+ additional_path << path_to_enlarge[index]
863
+ index = index + 1
864
+ if path_to_enlarge[index].class == String && (path_to_enlarge[index].end_with? "ID") then
865
+ id_count = id_count + 1
866
+ end
867
+ end
868
+ return additional_path + last_path
869
+ end
870
+
871
+ # create a new path that starts with the last user of the old path that produced
872
+ # the error
873
+
874
+ def jump_to_last_user(old_path)
875
+ # session of the last link that leads to the error
876
+ last_session = old_path[old_path.size-1].keys[0].split(":").first.to_i
877
+ new_path = []
878
+ tmp = []
879
+ old_path.each do |e|
880
+ # search the last user of the last session
881
+ if (e.class == String) && (e.end_with? "ID") && (e.to_i == last_session) then
882
+ tmp = []
883
+ tmp << e
884
+ end
885
+ if (e.class != String) then
886
+ tmp << e
887
+ end
888
+ end
889
+ new_path = tmp
890
+ return new_path
891
+ end
892
+
893
+ def user_count(path_to_count)
894
+ count = 0
895
+ path_to_count.each do |e|
896
+ if e.class == String then
897
+ count = count + 1
898
+ end
899
+ end
900
+ return count
901
+ end
902
+
903
+ def init_sessions
904
+ @sessions = Hash.new
905
+ end
906
+
907
+ def init_hash_sessions(key, value)
908
+ @sessions["#{key}"] = value
909
+ end
910
+
911
+ def sessions(index, key, value)
912
+ @sessions["#{index}"]["#{key}"] = value
913
+ end
914
+
915
+ def get_sessions(i, key)
916
+ @sessions["#{i}"]["#{key}"]
917
+ end
918
+
919
+ def get_sessions_array
920
+ @sessions_array
921
+ end
922
+
923
+ def init_sessions_array
924
+ @sessions_array = []
925
+ end
926
+
927
+ def add_to_sessions_array(session)
928
+ @sessions_array << session
929
+ end
930
+
931
+ def clean_and_seed_db
932
+ DatabaseCleaner.strategy = :truncation
933
+ DatabaseCleaner.start
934
+ DatabaseCleaner.clean
935
+ load "#{Rails.root}/db/test_seeds.rb"
936
+ end
937
+
938
+ def init_err_file
939
+ err_file = File.new("lib/auto_test/log/errors.log", "r")
940
+ err_file.gets
941
+ @err_message = err_file.gets.split(/:/,2).first
942
+ end
943
+
944
+ def err_message
945
+ @err_message
946
+ end
947
+
948
+ def init_user_inputs
949
+ @user_inputs = Hash.new
950
+ file = File.new("lib/auto_test/log/user_input.log", "r")
951
+ while line = file.gets do
952
+ b = line.split(/:/,2)
953
+ @user_inputs["#{b[0].strip}"] = b[1].chop
954
+ end
955
+ @user_inputs.each do |k,v|
956
+ if v.start_with? "[" then
957
+ x = v.split("],[")
958
+ result = []
959
+ x.each do |i|
960
+ result << i.delete("[]\"").split(",")
961
+ end
962
+ result.each do |r|
963
+ if r.class == String then
964
+ r.strip!
965
+ else
966
+ r.each do |s|
967
+ s.strip!
968
+ end
969
+ end
970
+ end
971
+ @user_inputs[k] = result
972
+ end
973
+ end
974
+ end
975
+
976
+ def user_inputs(key)
977
+ @user_inputs[key]
978
+ end
979
+
980
+ def path
981
+ @path
982
+ end
983
+
984
+ def init_path
985
+ @path = []
986
+ end
987
+
988
+ def add_path(value)
989
+ @path << value
990
+ end
991
+