Top Level Namespace

Defined Under Namespace

Modules: AutoTest Classes: TestStarter, Time

Instance Method Summary collapse

Instance Method Details

#add_path(value) ⇒ Object

add a value to the path



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

def add_path(value)
  @path << value
end

#add_to_sessions_array(session) ⇒ Object

add a sesion



701
702
703
# File 'lib/spec/requests/error_reduction_spec.rb', line 701

def add_to_sessions_array(session)
  @sessions_array << session
end

#clean_and_seed_dbObject

clean the db



706
707
708
709
# File 'lib/spec/requests/error_reduction_spec.rb', line 706

def clean_and_seed_db
  DatabaseCleaner.clean
  DatabaseCleaner.start
end

#delete_groups(new_path) ⇒ Object

delete groups of actions from the path



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

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|
        # find the right group to be deleted
        if n.class != String then
          if n.keys[0].split(":").second == AutoTest::Authentication. && 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
      # try the reduced path
      error_not_found = try_path(next_path)
      # if error was found, continue with reduced path and the same group number, otherwise
      # continue with same path and incremented group number
      if error_not_found && tmp != [] then
        no_of_group = no_of_group + 1
      else 
        new_path = next_path
      end
      # recount no of groups
      new_path.each do |n|
        if n.class != String && n.keys[0].split(":").first.to_i == i && n.values[0] == AutoTest::Authentication.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

delete the ids that are redundant



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/spec/requests/error_reduction_spec.rb', line 399

def delete_ids(new_path)
  for i in 0..get_sessions_array.size - 1 do
    found_session = false
    index_to_delete = nil
    # check if session ids are used in any action
    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

delete links half way between to User Ids or add one action



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

def delete_links_half_way(new_path, indx, user)
  next_path = []
  index = 0
  id = 0
  first_path = []
  middle_path = []
  # find the right part of the path to process
  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
        # get the first part of the path, that has to stay the same
        first_path << new_path[index]
        index = index + 1
      else
        # get the path to be reduced or added an action to
        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
  # check, if the path should be reduced or an action should be concatenated
  if indx == nil then
    tmp = middle_path[0..middle_path.size/2]
  else
    tmp = middle_path[0..indx]
  end
  middle_path = tmp
  last_path = []
  # get the rest of the path
  for i in index..new_path.size-1 do
    last_path << new_path[i]
  end 
  # conc the reduced path with the rest of the path, that hasn´t been reduced
  next_path = first_path + middle_path + last_path
  return [next_path, middle_path.size]
end

delete single links



472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/spec/requests/error_reduction_spec.rb', line 472

def delete_single_links(new_path)
  i = 0
  while i < new_path.size do
    # just delete actions other than login, logout or User id
    if (new_path[i].class != String && 
      !(new_path[i].values.include? AutoTest::Authentication.get_logout_path) && 
      !(new_path[i].keys[0].include? AutoTest::Authentication.)) then
        next_path = new_path[0..i-1]+new_path[i+1..new_path.size]
        # try the new path
        error_not_found = try_path(next_path)
        # if error was found, continue with reduced path, otherwise, with the same 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

depending on enlarging_counter add another part to the path



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

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
  # add line by line until the next user is found
  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

get the counter



654
655
656
# File 'lib/spec/requests/error_reduction_spec.rb', line 654

def enlarging_counter
  @counter 
end

#err_messageObject

get the error message



712
713
714
# File 'lib/spec/requests/error_reduction_spec.rb', line 712

def err_message
  @err_message
end

#find_next_path(session, path_to_try, lno) ⇒ Object

find the next path for the specified session



614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
# File 'lib/spec/requests/error_reduction_spec.rb', line 614

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

find next user, that fits the path



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

def find_next_user(root_path, session)
  index = 0
  u = 0
  user_size = AutoTest::Authentication.user_class.find(:all).size
  while u < user_size do
    user = AutoTest::Authentication.user_class.find(:first, :conditions => "id > #{index}")
    index = user.id
    # login the user
    session.visit AutoTest::Authentication.
    AutoTest::Authentication..each do |la|
      session.fill_in la[1], :with => user.send(la[0].to_sym)
    end
    session.click_button AutoTest::Authentication.
    # check if the path is the right one
    if session.current_path == root_path then
      return user
    end
    u = u + 1
  end 
  return false
end

#get_sessions(i, key) ⇒ Object

get session



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

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

#get_sessions_arrayObject

get the sessions array



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

def get_sessions_array
  @sessions_array
end

#inc_enlarging_counterObject

increment the counter



659
660
661
# File 'lib/spec/requests/error_reduction_spec.rb', line 659

def inc_enlarging_counter
  @counter = @counter + 1
end

#init_enlarging_counterObject



751
752
753
# File 'lib/spec/requests/error_reduction_spec.rb', line 751

def init_enlarging_counter
  @counter = 0
end

#init_err_fileObject

initializers



729
730
731
732
733
# File 'lib/spec/requests/error_reduction_spec.rb', line 729

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



739
740
741
# File 'lib/spec/requests/error_reduction_spec.rb', line 739

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

#init_pathObject



747
748
749
# File 'lib/spec/requests/error_reduction_spec.rb', line 747

def init_path
  @path = []
end

#init_sessionsObject



735
736
737
# File 'lib/spec/requests/error_reduction_spec.rb', line 735

def init_sessions
  @sessions = Hash.new
end

#init_sessions_arrayObject



743
744
745
# File 'lib/spec/requests/error_reduction_spec.rb', line 743

def init_sessions_array 
  @sessions_array = []
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



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

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(browser, user) ⇒ Object

login a user in a certain browser



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

def (browser, user)
  if AutoTest::Authentication.use_db_users then
    # if data read from db, fill in forms
    AutoTest::Authentication..each do |field|
      browser.text_field(:id => field[1]).set user.send(field[0].to_sym) 
    end
    sleep 1
  else
    # if data given by tester, fill these in
    index = AutoTest::Authentication..index(AutoTest::Authentication.)  
    user_data = nil
    user.class.find(:all).each do |u|
      AutoTest::Authentication..each do |d| 
        if d[index] == u.send(AutoTest::Authentication..to_sym) then
          user_data = AutoTest::Authentication.[index]
        end
      end  
    end
    AutoTest::Authentication..each_with_index do |field, i|
      browser.text_field(:id => field).set user_data[i]
    end
  end              
  sleep 2
  browser.button(:type => 'submit').click
end

#max_enlarging_counter(whole_path) ⇒ Object

get the number of users



664
665
666
667
668
669
670
671
672
# File 'lib/spec/requests/error_reduction_spec.rb', line 664

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

get the path



717
718
719
# File 'lib/spec/requests/error_reduction_spec.rb', line 717

def path
  @path
end

#replace_same_users!(new_path) ⇒ Object

replace the Strings in the path with a logout action, that are equal to the last User ID



546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/spec/requests/error_reduction_spec.rb', line 546

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]] = AutoTest::Authentication.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

get the session ids from the path



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/spec/requests/simulation_spec.rb', line 136

def search_sessions
  begin
    path_to_search = File.new("#{Rails.root}/log/new_path.log", "r")
  rescue
    path_to_search = File.new("#{Rails.root}/log/paths.log","r")
  end
  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

set a session variable



686
687
688
# File 'lib/spec/requests/error_reduction_spec.rb', line 686

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

#try_path(path_to_try) ⇒ Object

try the reduced path



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

def try_path(path_to_try)
  # clean db
  clean_and_seed_db
  lno = 0
  # reinitialize all variables and sessions
  init_sessions
  init_sessions_array
  number_of_sessions = AutoTest::Test.get_number_of_sessions
  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]
      # parse the line from the path
      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
        # login
        if line.values[0] == AutoTest::Authentication. then
          session.visit AutoTest::Authentication.
          hash["#{session_index}:#{session.current_path}"] = AutoTest::Authentication.
          if AutoTest::Authentication.use_db_users then
            AutoTest::Authentication..each do |field|            
              session.fill_in field[1], :with => user[session_index].send(field[0].to_sym)
            end
          else
            index = AutoTest::Authentication..index(AutoTest::Authentication.)        
            user[session_index].class.find(:all).each do |u|
              AutoTest::Authentication..each do |d|
                if d[index] == u.send(AutoTest::Authentication..to_sym) then
                  user_data = AutoTest::Authentication.[index]
                end
              end  
            end
            AutoTest::Authentication..each_with_index do |field, i|
              session.fill_in field[1], :with => user_data[i]
            end
          end
          session.click_button AutoTest::Authentication.
        # logout
        elsif line.values[0] == AutoTest::Authentication.get_logout_path then
          hash["#{session_index}:#{session.current_path}"] = AutoTest::Authentication.get_logout_path
          session.visit AutoTest::Authentication.get_logout_path
        # fill in forms
        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
          # find the right link
          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 the href of the link to the ones before, to find the right one
          # if the id was equal to teh last id before, it also has to be equal in the new link
          # if it was different, it has to be different  now as well
          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
          # check if links were found
          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}"
            # click the link
            link.click
            # if the link was still completely different from the original, visit the desired page
            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
        # update the line in the path
        path_to_try[lno-1].replace(hash)  
      else
        # if there´s no authorization, no Strings will be saved in the path
        if AutoTest::Authentication.use_db_users then 
          id = line.split(":").second.to_i
          begin 
            user[session_index] = AutoTest::Authentication.user_class.find(id) 
          rescue
            index = lno + 1
            root_path = ""
            # find the right user to continue
            # the next path has to be correct, so login users and check the next 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 != AutoTest::Authentication. 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] = AutoTest::Authentication.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
     # check for the right error message
     if err_message == e.message.to_s.split(/:/,2).first then
       error_not_found = false
     else 
       error_not_found = true
     end
     return error_not_found
   end
  return error_not_found
end

#user_count(path_to_count) ⇒ Object

count the user ids in the path



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

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