Module: RWebSpec::WebDriver::LoadTestHelper

Includes:
Assert, Utils
Defined in:
lib/rwebspec-webdriver/load_test_helper.rb

Constant Summary collapse

MAX_VU =
1000

Constants included from Utils

Utils::WORDS

Instance Method Summary collapse

Methods included from Assert

#assert_button_not_present, #assert_button_not_present_with_text, #assert_button_present, #assert_button_present_with_text, #assert_checkbox_not_selected, #assert_checkbox_selected, #assert_disabled, #assert_enabled, #assert_equals, #assert_exists, #assert_hidden, #assert_link_not_present_with_text, #assert_link_present_with_text, #assert_nil, #assert_not, #assert_not_exists, #assert_not_nil, #assert_option_equals, #assert_option_not_present, #assert_option_present, #assert_option_value_equals, #assert_option_value_not_present, #assert_option_value_present, #assert_radio_option_not_present, #assert_radio_option_not_selected, #assert_radio_option_present, #assert_radio_option_selected, #assert_text_field_value, #assert_text_in_element, #assert_text_in_page_source, #assert_text_not_in_page_source, #assert_text_not_present, #assert_text_not_present_in_table, #assert_text_present, #assert_text_present_in_table, #assert_title_equals, #assert_visible, #fail

Methods included from Utils

#days_before, #days_from_now, #interpret_value, #on, #paragraphs, #random_boolean, #random_char, #random_digit, #random_number, #random_str, #random_string_in, #repeat_try, #sentences, #shall_not_allow, #take_screenshot, #today, #tomorrow, #try_until, #value_in_range, #words, #yesterday

Instance Method Details

#allow(&block) ⇒ Object Also known as: shall_allow, allowing

Does not provide real function, other than make enhancing test syntax

Example:

allow { click_button('Register') }


25
26
27
# File 'lib/rwebspec-webdriver/load_test_helper.rb', line 25

def allow(&block)
  yield
end

#failsafe(&block) ⇒ Object Also known as: fail_safe

try operation, ignore if errors occur

Example:

failsafe { click_link("Logout") }  # try logout, but it still OK if not being able to (already logout))


35
36
37
38
39
40
# File 'lib/rwebspec-webdriver/load_test_helper.rb', line 35

def failsafe(&block)
  begin
    yield
  rescue =>e
  end
end

#log_time(msg, &block) ⇒ Object

monitor current execution using

Usage

log_time { browser.click_button('Confirm') }


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
# File 'lib/rwebspec-webdriver/load_test_helper.rb', line 89

def log_time(msg, &block)
  start_time = Time.now
  yield
  end_time = Time.now

  Thread.current[:log] ||= []
  Thread.current[:log] << {:file => File.basename(__FILE__),
    :message => msg,
    :start_time => Time.now,
  :duration => Time.now - start_time}

  if $LOADWISE_MONITOR
    begin
      require 'java'
      puts "Calling Java 1"
      java_import com.loadwise.db.MemoryDatabase
      #puts "Calling Java 2: #{MemoryDatabase.count}"
      MemoryDatabase.addEntry(1, "zdfa01", "a_spec.rb", msg, start_time, end_time);
      puts "Calling Java Ok: #{MemoryDatabase.count}"
    rescue NameError => ne
      puts "Name Error: #{ne}"
      # failed to load Java class
    rescue => e
      puts "Failed to calling Java: #{e.class.name}"
    end
  end
  # How to notify LoadWise at real time
  # LoadWise to collect CPU
end

#open_browser(base_url, options = {}) ⇒ Object

only support firefox or Celerity



12
13
14
15
16
17
# File 'lib/rwebspec-webdriver/load_test_helper.rb', line 12

def open_browser(base_url, options = {})
  default_options = {:resynchronize => false, :firefox => false }
  options = default_options.merge(options)
  options[:firefox] ||= (ENV['LOADWISE_PREVIEW'] || $LOADWISE_PREVIEW)
  RWebSpec::WebDriver::WebBrowser.new(base_url, nil, options)
end

#run_with_virtual_users(virtual_user_count = 2, preview = false, &block) ⇒ Object



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
# File 'lib/rwebspec-webdriver/load_test_helper.rb', line 119

def run_with_virtual_users(virtual_user_count = 2, preview = false, &block)
  raise "too many virtual users" if virtual_user_count > MAX_VU

  begin
    if defined?(LOADWISE_PREVIEW)
      preview = LOADWISE_PREVIEW
    end
  rescue => e1
  end

  if preview
    virtual_user_count = 1
    $LOADWISE_PREVIEW = true
  end

  if (virtual_user_count <= 1)
    yield
  else
    threads = []
    vu_reports = {}
    virtual_user_count.times do |idx|
      threads[idx] = Thread.new do
        start_time = Time.now
        vu_reports[idx] ||= []
        begin
          yield
          vu_reports[idx] =  Thread.current[:log]
        rescue => e
          vu_reports[idx] =  Thread.current[:log]
          vu_reports[idx] ||= []
          vu_reports[idx] << { :error => e }
        end
        vu_reports[idx] ||= []
        vu_reports[idx] << { :message => "Total Duration", :duration => Time.now - start_time }
        puts "VU[#{idx+1}] #{Time.now - start_time}s"
      end
    end

    threads.each {|t| t.join; }
    vu_reports.each do |key, value|
      value.each do |entry|
        if entry[:error] then
          puts "Error: #{entry[:error]}"
        else
          puts "[#{key}] #{entry[:message]}, #{entry[:duration]}"
        end
      end
    end

    return vu_reports
  end
end

#symbol_to_sequence(symb) ⇒ Object

Convert :first to 1, :second to 2, and so on…



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rwebspec-webdriver/load_test_helper.rb', line 70

def symbol_to_sequence(symb)
  value = { :zero => 0,
    :first => 1,
    :second => 2,
    :third => 3,
    :fourth => 4,
    :fifth => 5,
    :sixth => 6,
    :seventh => 7,
    :eighth => 8,
    :ninth => 9,
  :tenth => 10 }[symb]
  return value || symb.to_i
end

#try(timeout = $testwise_polling_timeout, polling_interval = $testwise_polling_interval || 1, &block) ⇒ Object Also known as: try_upto

Try the operation up to specified timeout (in seconds), and sleep given interval (in seconds). Error will be ignored until timeout Example

try { click_link('waiting')}
try(10, 2) { click_button('Search' } # try to click the 'Search' button upto 10 seconds, try every 2 seconds
try { click_button('Search' }


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rwebspec-webdriver/load_test_helper.rb', line 49

def try(timeout = $testwise_polling_timeout, polling_interval = $testwise_polling_interval || 1, &block)
  start_time = Time.now

  last_error = nil
  until (duration = Time.now - start_time) > timeout
    begin
      return if yield
      last_error = nil
    rescue => e
      last_error = e
    end
    sleep polling_interval
  end

  raise "Timeout after #{duration.to_i} seconds with error: #{last_error}." if last_error
  raise "Timeout after #{duration.to_i} seconds."
end