Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 21st, 2012  |  syntax: None  |  size: 3.61 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # Author: Gazza | gazaly(at)gmail(dot)com
  2. # Notes:
  3. # Simulate a variety of user-actions for Stacked ( http://code.google.com/p/stacked/ )
  4. # Dependencies:
  5. # $ gem install capybara faker
  6.  
  7. require 'rubygems'
  8. require 'capybara'
  9. require 'capybara/dsl'
  10. require 'faker'
  11.  
  12. Capybara.run_server = false
  13. Capybara.current_driver = :selenium
  14. Capybara.app_host = 'http://192.168.1.101/website'
  15. Capybara.default_wait_time = 10
  16.  
  17. module Test
  18.  
  19.     class Stacked
  20.  
  21.     include Capybara::DSL
  22.  
  23.     def create_users
  24.         10000.times do
  25.                 begin
  26.                         visit '/'
  27.                         click_link 'Register'
  28.                         fill_in('cntWrp_register_register_username', :with => Faker::Internet.user_name)
  29.                         fill_in('cntWrp_register_register_password', :with => 'password')
  30.                         fill_in('cntWrp_register_register_repeatPassword', :with => 'password')
  31.                         click_button 'Register'
  32.                         click_link 'Logout'
  33.                 rescue => e
  34.                         next
  35.                 end
  36.         end
  37.     end
  38.  
  39.     def create_questions
  40.         users = retrieve_users
  41.         100.times do
  42.                 begin
  43.                         login(users)
  44.                         visit '/'
  45.                         click_link 'Ask'
  46.                         fill_in('cntWrp_ask_wndAsk_header', :with => Faker::Lorem.sentence)
  47.                         fill_in('cntWrp_ask_wndAsk_body', :with => Faker::Lorem.paragraphs(paragraph_count=3).join(','))
  48.                         click_button 'Ask'
  49.                         visit '/'
  50.                             click_link 'Logout'
  51.                 rescue => e
  52.                         next
  53.                 end
  54.         end
  55.     end
  56.  
  57.     def simulate_upvotes
  58.         users = retrieve_users
  59.         10000.times do
  60.                 begin
  61.                         questions = retrieve_all_questions
  62.                         login(users)
  63.                         visit '/'
  64.                         3.times do
  65.                                 question_link = questions[Random.rand(questions.count-1)]
  66.                                 visit question_link
  67.                                 begin
  68.                                         find('.up').click
  69.                                 rescue => e
  70.                                         puts e
  71.                                 end
  72.                                 visit '/'
  73.                         end
  74.                         click_link 'Logout'
  75.                 rescue => e
  76.                         puts e
  77.                         click_link 'Logout'
  78.                 end
  79.         end
  80.     end
  81.  
  82.     def simulate_answering_questions
  83.         users = retrieve_users
  84.         10000.times do
  85.                 begin
  86.                         questions = retrieve_all_questions
  87.                         login(users)
  88.                         visit questions[Random.rand(questions.count-1)]
  89.                         begin
  90.                                 fill_in('cntWrp_body_answerQuestion_answerBody', :with => Faker::Lorem.sentences(sentence_count=Random.rand(5)).join(','))
  91.                                 click_button 'Submit Answer'
  92.                         rescue => e
  93.                                 puts e
  94.                         end
  95.                         visit '/'
  96.                         click_link 'Logout'
  97.                 rescue => e
  98.                         puts e
  99.                         begin
  100.                                 click_link 'Logout'
  101.                         rescue => e
  102.                                 next
  103.                         end
  104.                 end
  105.         end
  106.     end
  107.  
  108.     def simulate_upvoting_answered_questions
  109.         users = retrieve_users
  110.         10000.times do
  111.                 begin
  112.                         questions = retrieve_all_questions
  113.                         login(users)
  114.                         question_link = questions[Random.rand(questions.count-1)]
  115.                         visit question_link
  116.                         begin
  117.                                 length = all('a.up').count
  118.                                 all('a.up')[Random.rand(length-1)].click
  119.                         rescue => e
  120.                                 puts e
  121.                                 puts question_link
  122.                         end
  123.                         visit '/'
  124.                         click_link 'Logout'
  125.                 rescue => e
  126.                         puts e
  127.                         begin
  128.                                 click_link 'Logout'
  129.                         rescue => e
  130.                                 next
  131.                         end
  132.                 end
  133.         end
  134.     end
  135.  
  136.     def login(users)
  137.         visit '/'
  138.         click_link 'Login'
  139.         fill_in('cntWrp_login_login_nativeWrapper_username', :with => users[Random.rand(190)])
  140.         fill_in('cntWrp_login_login_nativeWrapper_password', :with => 'password')
  141.         find('#cntWrp_login_login_nativeWrapper_loginBtn').click
  142.     end
  143.  
  144.     def retrieve_users
  145.         visit '/AllUsers.ashx'
  146.         users = find('pre').text.split(',')
  147.         users.shift
  148.         users.shift
  149.         users
  150.     end
  151.  
  152.     def retrieve_all_questions
  153.         visit '/'
  154.         questions = all('.headerLink')[0..6].map { |a| a[:href] }
  155.         questions
  156.     end
  157.  
  158.   end
  159. end
  160.  
  161. t = Test::Stacked.new
  162.  
  163. t.create_users
  164. t.create_questions
  165. t.simulate_upvotes
  166. t.simulate_answering_questions
  167. t.simulate_upvoting_answered_questions