Top Level Namespace

Defined Under Namespace

Modules: AutoTest Classes: TestStarter, Time

Instance Method Summary collapse

Instance Method Details

#add_path(value) ⇒ Object



81
82
83
# File 'lib/simulation.rb', line 81

def add_path(value)
  @path << value
end

#add_to_sessions_array(session) ⇒ Object



69
70
71
# File 'lib/simulation.rb', line 69

def add_to_sessions_array(i,session)
  @sessions_array[i] = session
end

#clean_and_seed_dbObject



712
713
714
715
716
717
718
719
720
# File 'lib/spec/requests/error_reduction_spec.rb', line 712

def clean_and_seed_db
  DatabaseCleaner.strategy = :truncation
  DatabaseCleaner.start
  DatabaseCleaner.clean
  if user_inputs("use_fixtures") then
    fixtures(*user_inputs("get_fixtures"))
  end
  load "#{Rails.root}/db/test_seeds.rb"
end

#delete_groups(new_path) ⇒ Object



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
# File 'lib/spec/requests/error_reduction_spec.rb', line 256

def delete_groups(new_path)
  for i in 0..get_sessions_array.size - 1 do
    no_of_group = 1
    no_of_groups = 1
    tmp = []
    while no_of_group <= no_of_groups do
      count = 0
      no_of_groups = 0
      next_path = []
      new_path.each_with_index do |n,ind|
        if n.class != String then
          if n.keys[0].split(":").second == user_inputs("get_login_path") && n.keys[0].split(":").first.to_i == i then
            count = count + 1
          end
          if !(n.keys[0].split(":").first.to_i == i && count == no_of_group) then
            next_path << n
          else
            tmp << n
          end
        else
          next_path << n
        end
      end
      error_not_found = try_path(next_path)
      if error_not_found && tmp != [] then
        no_of_group = no_of_group + 1
      else 
        new_path = next_path
      end
      new_path.each do |n|
        if n.class != String && n.keys[0].split(":").first.to_i == i && n.values[0] == user_inputs("get_logout_path") then
          no_of_groups = no_of_groups + 1
        end
      end
      tmp = []
    end
  end
  return new_path
  puts new_path
end

#delete_ids(new_path) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/spec/requests/error_reduction_spec.rb', line 232

def delete_ids(new_path)
  for i in 0..get_sessions_array.size - 1 do
    found_session = false
    index_to_delete = nil
    new_path.each_with_index do |n, index|
      if n.class == String && n.to_i == i then
        index_to_delete = index
        if !found_session && index_to_delete != index then 
          new_path.delete_at(index_to_delete)
        end
        found_session = false
      else
        if (n.class != String && n.keys[0].split(":").first.to_i == i) then
          found_session = true
        end
      end
    end
    if !found_session then 
      new_path.delete_at(index_to_delete)
    end
    found_session = false
  end
end


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
# File 'lib/spec/requests/error_reduction_spec.rb', line 317

def delete_links_half_way(new_path, indx, user)
  next_path = []
  index = 0
  id = 0
  first_path = []
  middle_path = []
  while id <= user && index < new_path.size do
    if new_path[index].class == String then
      id = id + 1
      if id < user || new_path[index + 1].class == String then
        if new_path[index + 1].class == String then
          id = id - 1
        end
        first_path << new_path[index]
        index = index + 1
      else
        if id == user then
          middle_path << new_path[index]
          index = index + 1
        end
      end
    elsif id < user then 
      first_path << new_path[index]
      index = index + 1
    else
      middle_path << new_path[index]
      index = index + 1
    end
  end
  if indx == nil then
    tmp = middle_path[0..middle_path.size/2]
  else
    tmp = middle_path[0..indx]
  end
  middle_path = tmp
  last_path = []
  for i in index..new_path.size-1 do
    last_path << new_path[i]
  end 
  next_path = first_path + middle_path + last_path
  return [next_path, middle_path.size]
end


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
# File 'lib/spec/requests/error_reduction_spec.rb', line 166

def delete_links_without_input(new_path, number_of_links, session)
  next_path = []
  no_inputs = true 
  tmp = []
  got_one = false
  done = false
  new_path.each do |n|
    done = false
    # check if the line is an ID
    if n.class == String then
      # check if it´s the right session
      if n.to_i == session then
        # add the line to the path
        next_path << n
        # save, that the part before was processed
        done = true
        # save, that the right session id has been found
        got_one = true
      else 
        # for other session ids add the line to the tmp array
        tmp << n 
      end
    else 
      current_session = n.keys[0].split(":").first.to_i
      # if the right session id hasn´t been found, add the line to the path
      if current_session != session && !got_one then
        next_path << n
      # else add the line to the tmp array
      else
        tmp << n
        # check for input data for forms
        if n.values[0].class == Array && current_session == session then
          no_inputs = false
        end
        # if the part of the session to be deleted is finished, 
        # has no inputs and is smaller or equal to the wanted number of links,
        # delete the links of the session, other links get added to the path
        if n.values[0] == user_inputs("get_logout_path") && no_inputs && current_session == session then
          if tmp.select{|x| x.class != String && x.keys[0].split(":").first.to_i == session}.size == number_of_links then
            tmp.delete_if{|x| x.class != String && x.keys[0].split(":").first.to_i == session}
            next_path << tmp
            tmp = []
            done = true
            got_one = false 
          else
            next_path << tmp
            tmp = []
            done = true
            got_one = false
          end
        elsif n.values[0] == user_inputs("get_logout_path") && current_session == session then
          next_path << tmp
          tmp = []
          done = true
          no_inputs = true
          got_one = false
        end
      end  
    end
  end
  if !done then 
    next_path << tmp
  end
  return next_path.flatten
end


297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/spec/requests/error_reduction_spec.rb', line 297

def delete_single_links(new_path)
  i = 0
  while i < new_path.size do
    if (new_path[i].class != String && 
      !(new_path[i].values.include? user_inputs("get_logout_path")) && 
      !(new_path[i].keys[0].include? user_inputs("get_login_path"))) then
        next_path = new_path[0..i-1]+new_path[i+1..new_path.size]
        error_not_found = try_path(next_path)
        if !error_not_found then
          new_path = next_path
        else
          i = i + 1
        end
    else
      i = i + 1
    end
  end
  return new_path
end

#enlarge_path(path_to_enlarge) ⇒ Object

> could be optimized

depending on enlarging_counter add another part to the path



632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/spec/requests/error_reduction_spec.rb', line 632

def enlarge_path(path_to_enlarge)
  if enlarging_counter < max_enlarging_counter(path_to_enlarge)-1 then
    inc_enlarging_counter
  else
    return path_to_enlarge
  end
  last_path = jump_to_last_user(path_to_enlarge)
  additional_path = []
  id_count = 0
  index = 0
  while id_count < enlarging_counter do
    additional_path << path_to_enlarge[index]
    index = index + 1
    if path_to_enlarge[index].class == String && (path_to_enlarge[index].end_with? "ID") then
      id_count = id_count + 1
    end
  end
  return additional_path + last_path
end

#enlarging_counterObject



608
609
610
# File 'lib/spec/requests/error_reduction_spec.rb', line 608

def enlarging_counter
  @counter 
end

#err_messageObject



728
729
730
# File 'lib/spec/requests/error_reduction_spec.rb', line 728

def err_message
  @err_message
end

#find_next_path(session, path_to_try, lno) ⇒ Object



572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/spec/requests/error_reduction_spec.rb', line 572

def find_next_path(session, path_to_try, lno)
  root = ""
  stop = false
  while lno < path_to_try.size && !stop do
    line = path_to_try[lno]
    if line.class != String && line.keys[0].split(":").first.to_i == session then
      root = line.keys[0].split(":").second.delete("0-9")
      stop = true
    elsif line.class == String && line.split(":").first.to_i == session then
      stop = true
    end
    lno = lno + 1
  end
  return root
end

#find_next_user(root_path, session) ⇒ Object



588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# File 'lib/spec/requests/error_reduction_spec.rb', line 588

def find_next_user(root_path, session)
  index = 0
  u = 0
  user_size = Kernel.const_get(user_inputs("user_class")).find(:all).size
  while u < user_size do
    user = Kernel.const_get(user_inputs("user_class")).find(:first, :conditions => "id > #{index}")
    index = user.id
    session.visit user_inputs("get_login_path")
    user_inputs("get_login_attributes").each do |la|
      session.fill_in la[1], :with => user.send(la[0].to_sym)
    end
    session.click_button user_inputs("get_login_button")
    if session.current_path == root_path then
      return user
    end
    u = u + 1
  end 
  return false
end

#get_sessions(i, key) ⇒ Object



696
697
698
# File 'lib/spec/requests/error_reduction_spec.rb', line 696

def get_sessions(i, key)
  @sessions["#{i}"]["#{key}"]
end

#get_sessions_arrayObject



61
62
63
# File 'lib/simulation.rb', line 61

def get_sessions_array
  @sessions_array
end

#inc_enlarging_counterObject



612
613
614
# File 'lib/spec/requests/error_reduction_spec.rb', line 612

def inc_enlarging_counter
  @counter = @counter + 1
end

#init_enlarging_counterObject



616
617
618
# File 'lib/spec/requests/error_reduction_spec.rb', line 616

def init_enlarging_counter
  @counter = 0
end

#init_err_fileObject



722
723
724
725
726
# File 'lib/spec/requests/error_reduction_spec.rb', line 722

def init_err_file
  err_file = File.new("#{Rails.root}/log/errors.log", "r")
  err_file.gets
  @err_message = err_file.gets.split(/:/,2).first
end

#init_hash_sessions(key, value) ⇒ Object



688
689
690
# File 'lib/spec/requests/error_reduction_spec.rb', line 688

def init_hash_sessions(key, value)
  @sessions["#{key}"] = value
end

#init_pathObject



77
78
79
# File 'lib/simulation.rb', line 77

def init_path
  @path = []
end

#init_sessionsObject



684
685
686
# File 'lib/spec/requests/error_reduction_spec.rb', line 684

def init_sessions
  @sessions = Hash.new
end

#init_sessions_arrayObject



65
66
67
# File 'lib/simulation.rb', line 65

def init_sessions_array 
  @sessions_array = []
end

#init_user_inputsObject



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
# File 'lib/simulation.rb', line 29

def init_user_inputs
  @user_inputs = Hash.new
  file = File.new("log/user_input.log", "r")
  while line = file.gets do
    b = line.split(/:/,2)
    @user_inputs["#{b[0].strip}"] = b[1].chop
  end
  @user_inputs.each do |k,v|
    if v.start_with? "[" then
      x = v.split("],[")
      result = []
      x.each do |i|
        result << i.delete("[]\"").split(",")
      end
      result.each do |r|
        if r.class == String then
          r.strip!
        else
          r.each do |s|
            s.strip!
          end
        end
      end
      @user_inputs[k] = result
    end
  end
end

#jump_to_last_user(old_path) ⇒ Object

create a new path that starts with the last user of the old path that produced the error



655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
# File 'lib/spec/requests/error_reduction_spec.rb', line 655

def jump_to_last_user(old_path)
  # session of the last link that leads to the error
  last_session = old_path[old_path.size-1].keys[0].split(":").first.to_i
  new_path = []
  tmp = []
  old_path.each do |e|
    # search the last user of the last session
    if (e.class == String) && (e.end_with? "ID") && (e.to_i == last_session) then
      tmp = []
      tmp << e
    end
    if (e.class != String) then
      tmp << e
    end
  end
  new_path = tmp
  return new_path
end

#loggerObject



8
9
10
# File 'lib/auto_test.rb', line 8

def logger 
  Rails.logger
end

#login(login_attributes, browser, user) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/simulation.rb', line 6

def (, browser, user)
  if user_inputs("use_db_users") then
    user_inputs("get_login_attributes").each do |field|
      browser.text_field(:label => field[1]).set user.send(field[0].to_sym) 
    end
    sleep 1
  else
    index = user_inputs("get_login_names").index(user_inputs("get_unique_login_attribute_name"))        
    user.class.find(:all).each do |u|
      user_inputs("get_login_data").each do |d|
        if d[index] == u.send(user_inputs("get_unique_login_attribute_name").to_sym) then
          user_data = user_inputs("get_login_data")[index]
        end
      end  
    end
    user_inputs("get_login_attributes").each_with_index do |field, i|
      browser.text_field(:label => field[1]).set user_data[i]
    end
  end              
  sleep 2
  browser.button(:type => 'submit').click
end

#max_enlarging_counter(whole_path) ⇒ Object



620
621
622
623
624
625
626
627
628
# File 'lib/spec/requests/error_reduction_spec.rb', line 620

def max_enlarging_counter(whole_path)
  count = 0
  whole_path.each do |e|
    if e.class == String && (e.end_with? "ID") then
      count = count + 1
    end
  end
  return count
end

#pathObject



73
74
75
# File 'lib/simulation.rb', line 73

def path
  @path
end

#replace_same_users!(new_path) ⇒ Object



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/spec/requests/error_reduction_spec.rb', line 360

def replace_same_users!(new_path)
  users = Array.new(get_sessions_array.size)
  last_paths = Array.new(get_sessions_array.size)
  i = 0 
  while i < new_path.size do
    if new_path[i].class == String then
      session = new_path[i].split(":").first.to_i 
      if users[session] == new_path[i] then
        hash = Hash.new
        hash[last_paths[new_path[i].split(":").first.to_i]] = user_inputs("get_logout_path")
        new_path[i] = hash
      else 
        users[session] = new_path[i]
      end
    else 
      last_paths[new_path[i].keys[0].split(":").first.to_i] = new_path[i]
    end
    i = i + 1
  end 
end

#search_sessionsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/simulation.rb', line 85

def search_sessions
  path_to_search = File.new("#{gem_path}/../../log/new_path.log")
  sessions = Array.new
  while line = path_to_search.gets do
    line.gsub!(/[{}\"]/,'')
    if line.class == String then
      session = line.split(":").first.to_i
    else
      session = line.gsub!(/[{}\"]/,'').split(":").first.to_i
    end  
    if !(sessions.include? session) then
      sessions << session
    end
  end
  return sessions
end

#sessions(index, key, value) ⇒ Object



692
693
694
# File 'lib/spec/requests/error_reduction_spec.rb', line 692

def sessions(index, key, value)
  @sessions["#{index}"]["#{key}"] = value
end

#try_path(path_to_try) ⇒ Object



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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/spec/requests/error_reduction_spec.rb', line 381

def try_path(path_to_try)
  clean_and_seed_db
  lno = 0
  init_sessions
  init_sessions_array
  number_of_sessions = user_inputs("get_number_of_sessions").to_i
  for i in 0..number_of_sessions-1 do
    sess = Capybara::Session.new(:rack_test,Rails.application)
    add_to_sessions_array sess
    init_hash_sessions(i, Hash.new)
  end
  user = Array.new(number_of_sessions)
  session_paths = Array.new(number_of_sessions, "")
  old_session_paths = Array.new(number_of_sessions, "")
  session_index = 0
  error_not_found = true
  begin    
    lno = 0
    while lno < path_to_try.size do
      hash = Hash.new
      line = path_to_try[lno]
      if line.class == String then
        session = get_sessions_array[line.split(":").first.to_i]
      else
        session = get_sessions_array[line.keys[0].split(":").first.to_i]
        session_path = line.keys[0].split(":").second
      end
      session_index = get_sessions_array.index(session)
      lno = lno + 1
      if line.class == Hash then
        if line.values[0] == "Anmelden" then
          session.visit user_inputs("get_login_path")
          hash["#{session_index}:#{session.current_path}"] = user_inputs("get_login_button")
          if user_inputs("use_db_users") then
            user_inputs("get_login_attributes").each do |field|            
              session.fill_in field[1], :with => user[session_index].send(field[0].to_sym)
            end
          else
            index = user_inputs("get_login_names").index(user_inputs("get_unique_login_attribute_name"))        
            user[session_index].class.find(:all).each do |u|
              user_inputs("get_login_data").each do |d|
                if d[index] == u.send(user_inputs("get_unique_login_attribute_name").to_sym) then
                  user_data = user_inputs("get_login_data")[index]
                end
              end  
            end
            user_inputs("get_login_attributes").each_with_index do |field, i|
              session.fill_in field[1], :with => user_data[i]
            end
          end
          session.click_button user_inputs("get_login_button")
        elsif line.values[0] == user_inputs("get_logout_path") then
          hash["#{session_index}:#{session.current_path}"] = user_inputs("get_logout_path")
          session.visit user_inputs("get_logout_path")
        elsif line.values[0].class == Array then  
          inputs = line.values[0]
          if !session.all('input').empty? then
            i = 0
            while i < inputs.size do
              if inputs[i].strip.start_with? "radio___" then
                session.choose inputs[i + 1].strip
              elsif inputs[i].strip.start_with? "select___" then
                session.select inputs[i + 1].strip, :from => inputs[i].gsub("select___",'').strip
              elsif inputs[i].strip.start_with? "checkbox___" then  
                session.check inputs[i + 1].strip
              else
                input = session.all('input').find{ |inp|  inp[:name] == inputs[i].strip}
                input.set(inputs[i+1].strip)
              end
              i = i + 2
            end 
            hash["#{session_index}:#{session.current_path}"] = path_to_try[lno - 1].values[0]
            session.all('input').find_all{ |i|  i[:type] == "submit"}.first.click
          else 
            hash = path_to_try[lno-1]
          end
        else
          if session.current_path.delete("1-9") != session_path.delete("1-9") then
            session.visit session_path
          end
          link = line.values[0].split(/\+\+\+/)
          href = link[0].to_s
          text = link[1]
          path = session.current_path
          root = find_next_path(session_index, path_to_try, lno)
          old_p = href.split("/")
           # => check for ids in string
          links = session.all('a').select{ |l| (l[:href].to_s.delete("0-9") == href.delete("0-9")) && l.text == text}
          links_selected = links + []
          j = 1
          compare_string = ""
          new_split = session_paths[session_index].split("/")
          old_split = old_session_paths[session_index].split("/")
          while old_p[j] == old_split[j] && old_p[j] != nil do
            if new_split[j] != nil then
              compare_string << ("/" + new_split[j])
              if old_p[j].to_i.to_s == old_p[j] then
                links_selected = links_selected.select{ |l| 
                  l[:href].to_s[0..compare_string.size-1] ==  compare_string }
              end
            end
            j = j + 1
          end
          for i in 0..get_sessions_array.size - 1 do
            compare_string = ""
            if i != session_index then
              old_sess_p = old_session_paths[i].split("/")
              new_sess_p = session_paths[i].split("/")
              j = 1
              while old_p[j] == old_sess_p[j] && old_p[j] != nil do
                compare_string << "/" + new_sess_p[j] 
                j = j + 1
              end
             links_selected = links_selected.select{ |l| l[:href].start_with? compare_string  }
             if new_sess_p[j] != nil then
                compare_string << ("/" + new_sess_p[j] )
              end
              # if the string is an id
              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
                links_selected = links_selected.select{ |l| !(l[:href].start_with? compare_string) }
              end
            end
          end
          if links.size > 0 then
            if links_selected.size > 0 then
              link = links_selected[session_index % links_selected.size]
            else
              link = links[session_index % links.size]
            end
            old_session_paths[session_index] = href
            session_paths[session_index] = link[:href].to_s
            hash["#{session_index}:#{path}"] = "#{link[:href]}+++#{link.text}"
            link.click
            next_path = session.current_path.delete("0-9")
            if next_path != root then
              session.visit path
            end
          else
            hash = path_to_try[lno - 1]
          end
        end
        path_to_try[lno-1].replace(hash)  
      else
        # if there´s no authorization, no Strings will be saved in the path
        if user_inputs("use_db_users") then 
          id = line.split(":").second.to_i
          begin 
            user[session_index] = Kernel.const_get(user_inputs("user_class")).find(id) 
          rescue
            index = lno + 1
            root_path = ""
            while root_path == "" && index < path_to_try.size do
               if path_to_try[index].class != String then
                if path_to_try[index].keys[0].split(":").first.to_i == session_index then
                  if path_to_try[index].keys[0].split(":").second != user_inputs("get_login_path") then
                    root_path = path_to_try[index].keys[0].split(":").second
                  end
                end
                index = index + 1
              else 
                index = index + 1
              end
            end
            user[session_index] = find_next_user(root_path, session)
          end
        else
          user[session_index] = Kernel.const_get(user_inputs("user_class")).find(id) 
        end
        if user[session_index] == false then 
          return error_not_found
        else
          path_to_try[lno - 1] = "#{session_index}:#{user[session_index].id}ID"
        end
      end
    end  
   rescue => e
     if err_message == e.message.to_s.split(/:/,2).first then
       #puts "Found Error again, go on..."
       error_not_found = false
     else 
       f = File.new("#{Rails.root}/log/debugging.log", "w")
       f.puts path_to_try
       f.close
       error_not_found = true
     end
     return error_not_found
  
   end
  return error_not_found
end

#user_count(path_to_count) ⇒ Object



674
675
676
677
678
679
680
681
682
# File 'lib/spec/requests/error_reduction_spec.rb', line 674

def user_count(path_to_count)
  count = 0
  path_to_count.each do |e|
    if e.class == String then
      count = count + 1
    end
  end
  return count
end

#user_inputs(key) ⇒ Object



57
58
59
# File 'lib/simulation.rb', line 57

def user_inputs(key)
  @user_inputs[key]
end