Module: RWebSpec::Core
- Included in:
- AbstractWebPage, LoadTestHelper, RSpecHelper, TestScript, WebTestCase
- Defined in:
- lib/rwebspec-common/core.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.
-
#open_browser(opts = {}) ⇒ Object
open a browser, and set base_url via hash, but does not acually.
-
#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.
-
#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…
-
#today(format = nil) ⇒ Object
(also: #getToday_AU, #getToday_US, #getToday)
default date format returned is 29/12/2007.
- #tomorrow(format = nil) ⇒ Object
-
#try_for(timeout = $testwise_polling_timeout, polling_interval = $testwise_polling_interval || 1, &block) ⇒ Object
Try the operation up to specified timeout (in seconds), and sleep given interval (in seconds).
- #use_current_browser(how = :title, what = /.*/) ⇒ Object
-
#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') }
243 244 245 |
# File 'lib/rwebspec-common/core.rb', line 243 def allow(& block) yield end |
#days_before(days, format = nil) ⇒ Object
305 306 307 308 |
# File 'lib/rwebspec-common/core.rb', line 305 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
314 315 316 317 |
# File 'lib/rwebspec-common/core.rb', line 314 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))
254 255 256 257 258 259 260 261 262 |
# File 'lib/rwebspec-common/core.rb', line 254 def failsafe(& block) begin yield rescue RWebSpec::Assertion => e1 rescue ArgumentError => ae rescue RSpec::Expectations::ExpectationNotMetError => ree 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.
403 404 405 406 407 408 409 |
# File 'lib/rwebspec-common/core.rb', line 403 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
219 220 221 |
# File 'lib/rwebspec-common/core.rb', line 219 def on(page, & block) yield page end |
#open_browser(opts = {}) ⇒ Object
open a browser, and set base_url via hash, but does not acually
example:
open_browser :base_url => http://localhost:8080, :browser => :ie
There are 3 ways to set base url
1. pass as first argument
2. If running using TestWise, used as confiured
3. Use default value set
New Options:
:browser => :ie | :firefox | :chrome | :safari
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rwebspec-common/core.rb', line 31 def open_browser(opts = {}) # puts "[INFO] RWebSpec.Framework currently set to => #{RWebSpec.framework }" =begin if RWebSpec.framework =~ /watir/i RWebSpec.load_watir self.class.send(:include, RWebSpec::Driver) load(File.dirname(__FILE__) + "/web_page.rb") return open_browser_by_watir(opts) end if RWebSpec.framework =~ /selenium/i RWebSpec.load_selenium self.class.send(:include, RWebSpec::Driver) load(File.dirname(__FILE__) + "/web_page.rb") return open_browser_by_selenium(opts) end =end # puts "[INFO] No underlying framework is set, try to determine browser: #{opts.inspect}" if opts.class == Hash if opts[:browser] if opts[:browser].to_s =~ /ie/i || opts[:browser].to_s =~ /internet\sexplorer/i # puts "[INFO] based on browser, set to Watir" RWebSpec.framework = "Watir" self.class.send(:include, RWebSpec::Driver) # Reload abstract web page to load driver load(File.dirname(__FILE__) + "/web_page.rb") return open_browser_by_watir(opts) end # puts "[INFO] based on browser, set to Selenium" # not IE, using selenium RWebSpec.framework = "Selenium" self.class.send(:include, RWebSpec::Driver) load(File.dirname(__FILE__) + "/web_page.rb") return open_browser_by_selenium(opts) end end # puts "[INFO] browser type not specified, decide framework based on platform" if RUBY_PLATFORM =~ /mingw/ # if it is Windows, set to Watir RWebSpec.framework = "Watir" self.class.send(:include, RWebSpec::Driver) puts "[INFO] Extends of RWebSpec::Driver" load(File.dirname(__FILE__) + "/web_page.rb") return open_browser_by_watir(opts) else RWebSpec.framework = "Selenium" self.class.send(:include, RWebSpec::Driver) load(File.dirname(__FILE__) + "/web_page.rb") # using extend somehow does not work for RSpec # extend RWebSpec::Driver return open_browser_by_selenium(opts) end 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.
395 396 397 398 399 |
# File 'lib/rwebspec-common/core.rb', line 395 def paragraphs(total) (1..interpret_value(total)).map do sentences(3..8).capitalize end.join("\n\n") end |
#random_boolean ⇒ Object
330 331 332 |
# File 'lib/rwebspec-common/core.rb', line 330 def random_boolean return random_number(0, 1) == 1 end |
#random_char(lowercase = true) ⇒ Object
334 335 336 337 338 339 340 |
# File 'lib/rwebspec-common/core.rb', line 334 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
342 343 344 |
# File 'lib/rwebspec-common/core.rb', line 342 def random_digit() sprintf("%c", random_number(48, 57)) end |
#random_number(min, max) ⇒ Object
return a random number >= min, but <= max
326 327 328 |
# File 'lib/rwebspec-common/core.rb', line 326 def random_number(min, max) rand(max-min+1)+min end |
#random_str(length, lowercase = true) ⇒ Object
346 347 348 349 350 351 352 |
# File 'lib/rwebspec-common/core.rb', line 346 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
355 356 357 358 359 |
# File 'lib/rwebspec-common/core.rb', line 355 def random_string_in(arr) return nil if arr.empty? index = random_number(0, arr.length-1) arr[index] 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.
387 388 389 390 391 |
# File 'lib/rwebspec-common/core.rb', line 387 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 }
227 228 229 230 231 232 233 234 235 |
# File 'lib/rwebspec-common/core.rb', line 227 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…
192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/rwebspec-common/core.rb', line 192 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 |
#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
297 298 299 |
# File 'lib/rwebspec-common/core.rb', line 297 def today(format = nil) format_date(Time.now, date_format(format)) end |
#tomorrow(format = nil) ⇒ Object
320 321 322 |
# File 'lib/rwebspec-common/core.rb', line 320 def tomorrow(format = nil) days_from_now(1, date_format(format)) end |
#try_for(timeout = $testwise_polling_timeout, polling_interval = $testwise_polling_interval || 1, &block) ⇒ Object
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' }
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/rwebspec-common/core.rb', line 123 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 yield last_error = nil return true rescue RWebSpec::Assertion => e1 last_error = e1 rescue ArgumentError => ae last_error = ae rescue RSpec::Expectations::ExpectationNotMetError => ree last_error = ree 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 |
#use_current_browser(how = :title, what = /.*/) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rwebspec-common/core.rb', line 91 def use_current_browser(how = :title, what = /.*/) puts "[INFO] user current browser => #{RWebSpec.framework}" if RWebSpec.framework =~ /watir/i self.class.send(:include, RWebSpec::Driver) use_current_watir_browser(how, what) elsif RWebSpec.framework =~ /selenium/i self.class.send(:include, RWebSpec::Driver) use_current_selenium_browser(how, what) else # not specified, guess if RUBY_PLATFORM =~ /mingw/i RWebSpec.framework = "Watir" self.class.send(:include, RWebSpec::Driver) load(File.dirname(__FILE__) + "/web_page.rb") use_current_watir_browser(how, what) else RWebSpec.framework = "Selenium" self.class.send(:include, RWebSpec::Driver) load(File.dirname(__FILE__) + "/web_page.rb") use_current_selenium_browser(how, what) end end end |
#value_in_range(range) ⇒ Object
Pick a random value out of a given range.
366 367 368 369 370 371 372 373 |
# File 'lib/rwebspec-common/core.rb', line 366 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.
377 378 379 380 381 382 383 |
# File 'lib/rwebspec-common/core.rb', line 377 def words(total) if total.class == Range (1..interpret_value(total)).map { WORDS[random_number(total.min, total.max)] }.join(' ') else (1..interpret_value(total)).map { WORDS[random_number(0, total)] }.join(' ') end end |
#yesterday(format = nil) ⇒ Object
310 311 312 |
# File 'lib/rwebspec-common/core.rb', line 310 def yesterday(format = nil) days_before(1, date_format(format)) end |