Does not provide real function, other than make enhancing test syntax
Example:
allow { click_button('Register') }
# File lib/rwebspec-common/core.rb, line 227 def allow(& block) yield end
# File lib/rwebspec-common/core.rb, line 286 def days_before(days, format = nil) return nil if !(days.instance_of?(Fixnum)) format_date(Time.now - days * 24 * 3600, date_format(format)) end
# File lib/rwebspec-common/core.rb, line 295 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
try operation, ignore if errors occur
Example:
failsafe { click_link("Logout") } # try logout, but it still OK if not being able to (already logout))
# File lib/rwebspec-common/core.rb, line 238 def failsafe(& block) begin yield rescue =>e end end
If an array or range is passed, a random value will be selected to match. All other values are simply returned.
# File lib/rwebspec-common/core.rb, line 384 def interpret_value(value) case value when Array then value.rand when Range then value_in_range(value) else value end end
Example:
on @page do |i| i.enter_text('btn1') i.click_button('btn1') end
# File lib/rwebspec-common/core.rb, line 203 def on(page, & block) yield page end
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
# File lib/rwebspec-common/core.rb, line 24 def open_browser(opts = {}) puts "[INFO] RWebSpec.Framework currently set to => #{RWebSpec.framework }" 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 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/ || opts[:browser].to_s =~ /internet\sexplorer/ 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
Generate a given number of paragraphs. If a range is passed, it will generate a random number of paragraphs within that range.
# File lib/rwebspec-common/core.rb, line 376 def paragraphs(total) (1..interpret_value(total)).map do sentences(3..8).capitalize end.join("\n\n") end
# File lib/rwebspec-common/core.rb, line 311 def random_boolean return random_number(0, 1) == 1 end
# File lib/rwebspec-common/core.rb, line 315 def random_char(lowercase = true) if lowercase sprintf("%c", random_number(97, 122)) else sprintf("%c", random_number(65, 90)) end end
# File lib/rwebspec-common/core.rb, line 323 def random_digit() sprintf("%c", random_number(48, 57)) end
return a random number >= min, but <= max
# File lib/rwebspec-common/core.rb, line 307 def random_number(min, max) rand(max-min+1)+min end
# File lib/rwebspec-common/core.rb, line 327 def random_str(length, lowercase = true) randomStr = "" length.times { randomStr += random_char(lowercase) } randomStr end
Return a random string in a rangeof pre-defined strings
# File lib/rwebspec-common/core.rb, line 336 def random_string_in(arr) return nil if arr.empty? index = random_number(0, arr.length-1) arr[index] end
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
# File lib/rwebspec-common/core.rb, line 152 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
Generate a given number of sentences. If a range is passed, it will generate a random number of sentences within that range.
# File lib/rwebspec-common/core.rb, line 368 def sentences(total) (1..interpret_value(total)).map do words(5..20).capitalize end.join('. ') end
fail the test if user can perform the operation
Example:
shall_not_allow { 1/0 }
# File lib/rwebspec-common/core.rb, line 211 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
Convert :first to 1, :second to 2, and so on…
# File lib/rwebspec-common/core.rb, line 176 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
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
# File lib/rwebspec-common/core.rb, line 278 def today(format = nil) format_date(Time.now, date_format(format)) end
# File lib/rwebspec-common/core.rb, line 301 def tomorrow(format = nil) days_from_now(1, date_format(format)) end
# File lib/rwebspec-common/core.rb, line 139 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_for instead." try_for(timeout, polling_interval) { yield } end
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' }
# File lib/rwebspec-common/core.rb, line 116 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 => 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
# File lib/rwebspec-common/core.rb, line 84 def use_current_browser(how = :title, what = /.*/) puts "[INFO] user current browser => #{RWebSpec.framework}" if RWebSpec.framework =~ /watir/ self.class.send(:include, RWebSpec::Driver) use_current_watir_browser(how, what) elsif RWebSpec.framework =~ /selenium/ self.class.send(:include, RWebSpec::Driver) use_current_selenium_browser(how, what) else # not specified, guess if RUBY_PLATFORM =~ /mingw/ 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
Pick a random value out of a given range.
# File lib/rwebspec-common/core.rb, line 347 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
Generate a given number of words. If a range is passed, it will generate a random number of words within that range.
# File lib/rwebspec-common/core.rb, line 358 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
# File lib/rwebspec-common/core.rb, line 291 def yesterday(format = nil) days_before(1, date_format(format)) end