Module: RWebSpec::WebDriver::Utils
- Included in:
- AbstractWebPage, LoadTestHelper, RSpecHelper, TestScript, WebTestCase
- Defined in:
- lib/rwebspec-webdriver/test_utils.rb
Constant Summary collapse
- WORDS =
%w(alias consequatur aut perferendis sit voluptatem accusantium doloremque aperiam eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo aspernatur aut odit aut fugit sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt neque dolorem ipsum quia dolor sit amet consectetur adipisci velit sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem ut enim ad minima veniam quis nostrum exercitationem ullam corporis nemo enim ipsam voluptatem quia voluptas sit suscipit laboriosam nisi ut aliquid ex ea commodi consequatur quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae et iusto odio dignissimos ducimus qui blanditiis praesentium laudantium totam rem voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident sed ut perspiciatis unde omnis iste natus error similique sunt in culpa qui officia deserunt mollitia animi id est laborum et dolorum fuga et harum quidem rerum facilis est et expedita distinctio nam libero tempore cum soluta nobis est eligendi optio cumque nihil impedit quo porro quisquam est qui minus id quod maxime placeat facere possimus omnis voluptas assumenda est omnis dolor repellendus temporibus autem quibusdam et aut consequatur vel illum qui dolorem eum fugiat quo voluptas nulla pariatur at vero eos et accusamus officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae itaque earum rerum hic tenetur a sapiente delectus ut aut reiciendis voluptatibus maiores doloribus asperiores repellat)
Instance Method Summary collapse
-
#allow(&block) ⇒ Object
(also: #shall_allow, #allowing)
Does not provide real function, other than make enhancing test syntax.
- #days_before(days, format = nil) ⇒ Object
- #days_from_now(days, format = nil) ⇒ Object (also: #days_after)
-
#failsafe(&block) ⇒ Object
(also: #fail_safe)
try operation, ignore if errors occur.
-
#interpret_value(value) ⇒ Object
If an array or range is passed, a random value will be selected to match.
-
#on(page) {|page| ... } ⇒ Object
Example: on @page do |i| i.enter_text(‘btn1’) i.click_button(‘btn1’) end.
-
#paragraphs(total) ⇒ Object
Generate a given number of paragraphs.
- #random_boolean ⇒ Object
- #random_char(lowercase = true) ⇒ Object
- #random_digit ⇒ Object
-
#random_number(min, max) ⇒ Object
return a random number >= min, but <= max.
- #random_str(length, lowercase = true) ⇒ Object
-
#random_string_in(arr) ⇒ Object
(also: #random_string_in_collection)
Return a random string in a rangeof pre-defined strings.
-
#repeat_try(num_tries = $testwise_polling_timeout || 30, interval = $testwise_polling_interval || 1, &block) ⇒ Object
Try the operation up to specified times, and sleep given interval (in seconds) Error will be ignored until timeout Example repeat_try(3, 2) { click_button(‘Search’ } # 3 times, 6 seconds in total repeat_try { click_button(‘Search’ } # using default 5 tries, 2 second interval.
-
#sentences(total) ⇒ Object
Generate a given number of sentences.
-
#shall_not_allow(&block) ⇒ Object
(also: #do_not_allow)
fail the test if user can perform the operation.
-
#symbol_to_sequence(symb) ⇒ Object
Convert :first to 1, :second to 2, and so on…
-
#take_screenshot ⇒ Object
use win32screenshot library to save curernt active window, which shall be IE.
-
#today(format = nil) ⇒ Object
(also: #getToday_AU, #getToday_US, #getToday)
default date format returned is 29/12/2007.
- #tomorrow(format = nil) ⇒ Object
- #try(timeout = $testwise_polling_timeout, polling_interval = $testwise_polling_interval || 1, &block) ⇒ Object
-
#try_for(timeout = $testwise_polling_timeout, polling_interval = $testwise_polling_interval || 1, &block) ⇒ Object
(also: #try_upto, #try_up_to, #try_until)
Try the operation up to specified timeout (in seconds), and sleep given interval (in seconds).
-
#value_in_range(range) ⇒ Object
Pick a random value out of a given range.
-
#words(total) ⇒ Object
Generate a given number of words.
- #yesterday(format = nil) ⇒ Object
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 { ('Register') }
44 45 46 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 44 def allow(& block) yield end |
#days_before(days, format = nil) ⇒ Object
211 212 213 214 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 211 def days_before(days, format = nil) return nil if !(days.instance_of?(Fixnum)) format_date(Time.now - days * 24 * 3600, date_format(format)) end |
#days_from_now(days, format = nil) ⇒ Object Also known as: days_after
220 221 222 223 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 220 def days_from_now(days, format = nil) return nil if !(days.instance_of?(Fixnum)) format_date(Time.now + days * 24 * 3600, date_format(format)) 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))
55 56 57 58 59 60 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 55 def failsafe(& block) begin yield rescue =>e end end |
#interpret_value(value) ⇒ Object
If an array or range is passed, a random value will be selected to match. All other values are simply returned.
305 306 307 308 309 310 311 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 305 def interpret_value(value) case value when Array then value.rand when Range then value_in_range(value) else value end end |
#on(page) {|page| ... } ⇒ Object
Example:
on @page do |i|
i.enter_text('btn1')
i.('btn1')
end
20 21 22 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 20 def on(page, & block) yield page end |
#paragraphs(total) ⇒ Object
Generate a given number of paragraphs. If a range is passed, it will generate a random number of paragraphs within that range.
297 298 299 300 301 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 297 def paragraphs(total) (1..interpret_value(total)).map do sentences(3..8).capitalize end.join("\n\n") end |
#random_boolean ⇒ Object
236 237 238 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 236 def random_boolean return random_number(0, 1) == 1 end |
#random_char(lowercase = true) ⇒ Object
240 241 242 243 244 245 246 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 240 def random_char(lowercase = true) if lowercase sprintf("%c", random_number(97, 122)) else sprintf("%c", random_number(65, 90)) end end |
#random_digit ⇒ Object
248 249 250 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 248 def random_digit() sprintf("%c", random_number(48, 57)) end |
#random_number(min, max) ⇒ Object
return a random number >= min, but <= max
232 233 234 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 232 def random_number(min, max) rand(max-min+1)+min end |
#random_str(length, lowercase = true) ⇒ Object
252 253 254 255 256 257 258 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 252 def random_str(length, lowercase = true) randomStr = "" length.times { randomStr += random_char(lowercase) } randomStr end |
#random_string_in(arr) ⇒ Object Also known as: random_string_in_collection
Return a random string in a rangeof pre-defined strings
261 262 263 264 265 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 261 def random_string_in(arr) return nil if arr.empty? index = random_number(0, arr.length-1) arr[index] end |
#repeat_try(num_tries = $testwise_polling_timeout || 30, interval = $testwise_polling_interval || 1, &block) ⇒ Object
Try the operation up to specified times, and sleep given interval (in seconds) Error will be ignored until timeout Example
repeat_try(3, 2) { click_button('Search' } # 3 times, 6 seconds in total
repeat_try { click_button('Search' } # using default 5 tries, 2 second interval
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 70 def repeat_try(num_tries = $testwise_polling_timeout || 30, interval = $testwise_polling_interval || 1, & block) num_tries ||= 1 (num_tries - 1).times do |num| begin yield return rescue => e # puts "debug: #{num} failed: #{e}" sleep interval end end # last try, throw error if still fails begin yield rescue => e raise e.to_s + " after trying #{num_tries} times every #{interval} seconds" end yield end |
#sentences(total) ⇒ Object
Generate a given number of sentences. If a range is passed, it will generate a random number of sentences within that range.
289 290 291 292 293 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 289 def sentences(total) (1..interpret_value(total)).map do words(5..20).capitalize end.join('. ') end |
#shall_not_allow(&block) ⇒ Object Also known as: do_not_allow
fail the test if user can perform the operation
Example:
shall_not_allow { 1/0 }
28 29 30 31 32 33 34 35 36 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 28 def shall_not_allow(& block) operation_performed_ok = false begin yield operation_performed_ok = true rescue end raise "Operation shall not be allowed" if operation_performed_ok end |
#symbol_to_sequence(symb) ⇒ Object
Convert :first to 1, :second to 2, and so on…
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 130 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 |
#take_screenshot ⇒ Object
use win32screenshot library to save curernt active window, which shall be IE
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/test_utils.rb', line 148 def take_screenshot if $testwise_screenshot_supported && is_windows? begin screenshot_image_filename = "screenshot_" + Time.now.strftime("%m%d%H%M%S") + ".jpg" the_dump_dir = default_dump_dir FileUtils.mkdir_p(the_dump_dir) unless File.exists?(the_dump_dir) screenshot_image_filepath = File.join(the_dump_dir, screenshot_image_filename) screenshot_image_filepath.gsub!("/", "\\") if is_windows? FileUtils.rm_f(screenshot_image_filepath) if File.exist?(screenshot_image_filepath) case @web_browser..browser when :internet_explorer Win32::Screenshot::Take.of(:window, :title => /internet\sexplorer/i).write(screenshot_image_filepath) else Win32::Screenshot::Take.of(:foreground).write(screenshot_image_filepath) end notify_screenshot_location(screenshot_image_filepath) rescue => e puts "error on taking screenshot: #{e}" end end end |
#today(format = nil) ⇒ Object Also known as: getToday_AU, getToday_US, getToday
default date format returned is 29/12/2007. if supplied parameter is not ‘%m/%d/%Y’ -> 12/29/2007 Otherwise, “2007-12-29”, which is most approiate date format
%a - The abbreviated weekday name (``Sun'')
%A - The full weekday name (``Sunday'')
%b - The abbreviated month name (``Jan'')
%B - The full month name (``January'')
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (``AM'' or ``PM'')
%S - Second of the minute (00..60)
%U - Week number of the current year,
starting with the first Sunday as the first
day of the first week (00..53)
%W - Week number of the current year,
starting with the first Monday as the first
day of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal ``%'' character
203 204 205 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 203 def today(format = nil) format_date(Time.now, date_format(format)) end |
#tomorrow(format = nil) ⇒ Object
226 227 228 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 226 def tomorrow(format = nil) days_from_now(1, date_format(format)) end |
#try(timeout = $testwise_polling_timeout, polling_interval = $testwise_polling_interval || 1, &block) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 121 def try(timeout = $testwise_polling_timeout, polling_interval = $testwise_polling_interval || 1, &block) puts "Warning: method 'try' is deprecated (won't support in RWebSpec 3), use try_until instead." try_until(timeout, polling_interval) { yield } end |
#try_for(timeout = $testwise_polling_timeout, polling_interval = $testwise_polling_interval || 1, &block) ⇒ Object Also known as: try_upto, try_up_to, try_until
Try the operation up to specified timeout (in seconds), and sleep given interval (in seconds). Error will be ignored until timeout Example
try_for { click_link('waiting')}
try_for(10, 2) { click_button('Search' } # try to click the 'Search' button upto 10 seconds, try every 2 seconds
try_for { click_button('Search' }
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 99 def try_for(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 |
#value_in_range(range) ⇒ Object
Pick a random value out of a given range.
272 273 274 275 276 277 278 279 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 272 def value_in_range(range) case range.first when Integer then number_in_range(range) when Time then time_in_range(range) when Date then date_in_range(range) else range.to_a.rand end end |
#words(total) ⇒ Object
Generate a given number of words. If a range is passed, it will generate a random number of words within that range.
283 284 285 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 283 def words(total) (1..interpret_value(total)).map { WORDS[random_number(0, total)] }.join(' ') end |
#yesterday(format = nil) ⇒ Object
216 217 218 |
# File 'lib/rwebspec-webdriver/test_utils.rb', line 216 def yesterday(format = nil) days_before(1, date_format(format)) end |