Class: Rsel::SeleniumTest
- Inherits:
-
Object
- Object
- Rsel::SeleniumTest
- Includes:
- Support
- Defined in:
- lib/rsel/selenium_test.rb
Overview
Main Selenium-test class, and wrapper for all Selenium method calls. This class is intended to be instantiated in a FitNesse / Slim script table. May be customized or extended using a subclass.
@note: Function names beginning with the words 'check', 'ensure', 'reject', 'note', 'show', or 'start' cannot be called from a Slim script table, since these are keywords that receive special handling by Slim.
Instance Attribute Summary collapse
-
#browser ⇒ Object
readonly
Returns the value of attribute browser.
-
#found_failure ⇒ Object
Returns the value of attribute found_failure.
-
#stop_on_failure ⇒ Object
Returns the value of attribute stop_on_failure.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#begin_scenario ⇒ Object
Begin a new scenario, and forget about any previous failures.
-
#begin_study ⇒ Object
Study the current web page, for more efficient parsing and data retrieval on the Rsel side rather than the Selenium browser side.
-
#button_exists(locator, scope = {}) ⇒ Object
Ensure that a button exists on the page.
-
#checkbox_is_disabled(locator, scope = {}) ⇒ Object
Verify that a given checkbox is disabled (unchecked).
-
#checkbox_is_enabled(locator, scope = {}) ⇒ Object
Verify that a given checkbox is enabled (checked).
-
#click(locator, scope = {}) ⇒ Object
Click on a link or button, and wait for a page to load.
-
#click_back ⇒ Object
Click the Back button to navigate to the previous page.
-
#click_button(locator, scope = {}) ⇒ Object
(also: #press)
Press a button.
-
#click_link(locator, scope = {}) ⇒ Object
(also: #follow)
Click on a link.
-
#close_browser(show_errors = '') ⇒ Object
Stop the session and close the browser window.
-
#continue_study ⇒ Object
If studying is turned off, a flag is set, but the page is not deleted.
-
#disable_checkbox(locator, scope = {}) ⇒ Object
Disable (uncheck) a checkbox by clicking on it.
-
#do_not_see(text, scope = nil) ⇒ Object
Ensure that the given text does not appear on the current page.
-
#do_not_see_title(title) ⇒ Object
Ensure that the current page does not have the given title text.
-
#do_not_see_within_seconds(text, seconds = -1,, scope = nil) ⇒ Object
Ensure that the given text does not appear on the current page, eventually.
-
#dropdown_equals(locator, option, scope = {}) ⇒ Object
Check whether an option is currently selected in a dropdown/combo box.
-
#dropdown_includes(locator, option, scope = {}) ⇒ Object
Check whether an option exists in a dropdown/combo box.
-
#enable_checkbox(locator, scope = {}) ⇒ Object
Enable (check) a checkbox by clicking on it.
-
#end_if ⇒ Object
End an if block.
-
#end_scenario ⇒ Object
End the current scenario.
-
#end_study ⇒ Object
Turn off studying.
-
#errors ⇒ Object
Show any current error messages.
-
#field_contains(locator, text, scope = {}) ⇒ Object
Verify that a text field contains the given text.
-
#field_equals(locator, text, scope = {}) ⇒ Object
Verify that a text field's value equals the given text.
-
#field_equals_among(field, value, ids = {}, scope = {}) ⇒ Object
Check a value (with #set_field) in the named field, based on the given name/value pairs.
-
#fields_equal(fields = {}, scope = {}) ⇒ Object
Check values (with #set_field) in the named fields of a hash, based on the given name/value pairs.
-
#fields_equal_among(fields = {}, ids = {}, scope = {}) ⇒ Object
Check values (with #set_field) in the named fields, based on the given name/value pairs, and with mapping of names in the ids field.
-
#fill_in_with(locator, text, scope = {}) ⇒ Object
Fill in a field with the given text.
-
#generic_field_equals(locator, value = '', scope = {}) ⇒ Object
A generic way to check any field, of any type.
-
#if_i_see(text) ⇒ Object
If I see the given text, do the steps until I see an otherwise or end_if.
-
#if_is(text, expected) ⇒ Object
If the first parameter is the same as the second, do the steps until I see an otherwise or end_if.
-
#if_parameter(text) ⇒ Object
If the given parameter is "yes" or "true", do the steps until I see an otherwise or end_if.
-
#initialize(url, options = {}) ⇒ SeleniumTest
constructor
Initialize a test, connecting to the given Selenium server.
-
#link_exists(locator, scope = {}) ⇒ Object
Ensure that a link exists on the page.
-
#maximize_browser ⇒ Object
Maximize the browser window.
-
#method_missing(method, *args, &block) ⇒ Object
Invoke a missing method.
-
#open_browser ⇒ Object
Start the session and open a browser to the URL defined at the start of the test.
-
#otherwise ⇒ Object
The else case to match any if.
-
#page_loads_in_seconds_or_less(seconds) ⇒ Object
Wait some number of seconds for the current page request to finish.
-
#pause_seconds(seconds) ⇒ Object
(also: #pause_secs)
Pause for a certain number of seconds.
-
#radio_is_disabled(locator, scope = {}) ⇒ Object
Verify that a given radio button is disabled (unchecked).
-
#radio_is_enabled(locator, scope = {}) ⇒ Object
Verify that a given radio button is enabled (checked).
-
#refresh_page ⇒ Object
Reload the current page.
-
#reset_conditionals ⇒ Object
Reset the conditional stack to its initial state.
-
#respond_to?(orgmethod, include_private = false) ⇒ Boolean
Return true if SeleniumTest explicitly responds to a given method name, or if the underlying Selenium::Client::Driver instance can respond to it.
-
#row_exists(cells) ⇒ Object
Ensure that a table row with the given cell values exists.
-
#see(text, scope = nil) ⇒ Object
Ensure that the given text appears on the current page.
-
#see_alert_within_seconds(text = nil, seconds = -1)) ⇒ Object
Ensure that an alert appears, optionally having the given text, within the given time or the default timeout.
-
#see_title(title) ⇒ Object
Ensure that the current page has the given title text.
-
#see_within_seconds(text, seconds = -1,, scope = nil) ⇒ Object
Ensure that the given text appears on the current page, eventually.
-
#select_from_dropdown(option, locator, scope = {}) ⇒ Object
Select an option from a dropdown/combo box.
-
#select_radio(locator, scope = {}) ⇒ Object
Select a radio button.
-
#set_field(locator, value = '', scope = {}) ⇒ Object
A generic way to fill in any field, of any type.
-
#set_field_among(field, value, ids = {}, scope = {}) ⇒ Object
Set a value (with #set_field) in the named field, based on the given name/value pairs.
-
#set_fields(fields = {}, scope = {}) ⇒ Object
Set values (with #set_field) in the named fields of a hash, based on the given name/value pairs.
-
#set_fields_among(fields = {}, ids = {}, scope = {}) ⇒ Object
Set values (with #set_field) in the named fields, based on the given name/value pairs, and with mapping of names in the ids field.
-
#set_fields_study_min(level = nil) ⇒ Object
Set the minimum number of fields that must appear in a set_fields before studying is invoked.
-
#type_into_field(text, locator, scope = {}) ⇒ Object
Type a value into the given field.
-
#visit(path_or_url) ⇒ Object
Load an absolute URL or a relative path in the browser.
Methods included from Support
#apply_scope, #csspath, #escape_for_hash, #failed_within, #globify, #normalize_ids, #result_within, #selenium_compare, #string_is_true?, #strip_tags, #xpath, #xpath_expressions, #xpath_row_containing, #xpath_sanitize
Constructor Details
#initialize(url, options = {}) ⇒ SeleniumTest
Initialize a test, connecting to the given Selenium server.
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 90 |
# File 'lib/rsel/selenium_test.rb', line 63
def initialize(url, options={})
# Strip HTML tags from URL
@url = strip_tags(url)
@browser = Selenium::Client::Driver.new(
:host => options[:host] || 'localhost',
:port => options[:port] || 4444,
:browser => options[:browser] || '*firefox',
:url => @url,
:default_timeout_in_seconds => options[:timeout] || 300)
# Accept Booleans or strings, case-insensitive
if options[:stop_on_failure].to_s =~ /true/i
@stop_on_failure = true
else
@stop_on_failure = false
end
@found_failure = false
@conditional_stack = [ true ]
# Study data
@study = StudyHtml.new()
# @fields_study_min: The minimum number of fields to set_fields or fields_equal at once before studying is invoked.
# To be on the safe side, studying is turned off by default, unless you set another value.
@fields_study_min = parse_fields_study_min(options[:study], 0)
@default_fields_study_min = @fields_study_min
# @xpath_study_length_min: The minimum number of characters in an xpath before studying is invoked when @fields_study_min == 1.
@xpath_study_length_min = 100
# A list of error messages:
@errors = []
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Invoke a missing method. If a method is called on a SeleniumTest instance, and that method is not explicitly defined, this method will check to see whether the underlying Selenium::Client::Driver instance can respond to that method. If so, that method is called instead.
Prefixing a method with "check_" will verify the return string against the last argument. This allows checking string values within an if block.
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 |
# File 'lib/rsel/selenium_test.rb', line 1421
def method_missing(method, *args, &block)
return skip_status if skip_step?
do_check = false
if(/^check_/ === method.to_s)
# This emulates the FitNesse | Check | prefix, without the messy string return.
do_check = true
method = method.to_s.sub(/^check_/,'').to_sym
check_against = args.pop.to_s
end
# Allow methods like "Type" that have Ruby homonyms to be called with a "selenium" prefix.
method = method.to_s.sub(/^selenium_/,'').to_sym if /^selenium_/ === method.to_s
if @browser.respond_to?(method)
# Most methods should dirty the study object. These include get_eval and get_expression.
@study.dirty unless /^(get_(el|[^e])|is_)/ === method.to_s
begin
result = @browser.send(method, *args, &block)
rescue
failure "Method #{method} error"
else
# The method call succeeded
# Should we check this against another string?
if do_check
return pass_if selenium_compare(result.to_s, check_against),
"Expected '#{check_against}', but got '#{result.to_s}'"
end
# Did it return true or false?
return failure if result == false
# If a string, return that. We might Check or Show it.
return result if result == true || (result.is_a? String)
# Not a Boolean return value or string--assume passing
return true
end
else
super
end
end
|
Instance Attribute Details
#browser ⇒ Object (readonly)
Returns the value of attribute browser.
92 93 94 |
# File 'lib/rsel/selenium_test.rb', line 92
def browser
@browser
end
|
#found_failure ⇒ Object
Returns the value of attribute found_failure.
92 93 94 |
# File 'lib/rsel/selenium_test.rb', line 92
def found_failure
@found_failure
end
|
#stop_on_failure ⇒ Object
Returns the value of attribute stop_on_failure.
92 93 94 |
# File 'lib/rsel/selenium_test.rb', line 92
def stop_on_failure
@stop_on_failure
end
|
#url ⇒ Object (readonly)
Returns the value of attribute url.
92 93 94 |
# File 'lib/rsel/selenium_test.rb', line 92
def url
@url
end
|
Instance Method Details
#begin_scenario ⇒ Object
Begin a new scenario, and forget about any previous failures. This allows you to modularize your tests into standalone sections that can run independently of previous scenarios, regardless of whether those scenarios passed or failed.
236 237 238 239 240 |
# File 'lib/rsel/selenium_test.rb', line 236
def begin_scenario
@found_failure = false
@errors = []
return true
end
|
#begin_study ⇒ Object
Study the current web page, for more efficient parsing and data retrieval on the Rsel side rather than the Selenium browser side. Named for the Perl "study" command. Like its namesake, begin_study takes some time.
Warning: When you study, you can learn information more quickly, but you may not be aware of what's going on around you! If any action you perform has side-effects on the web page, you may not notice them while studying is on. This is why so many methods call #end_study automatically. But many cases remain where you could get into trouble if you forget this.
Note: Calling this twice in a row without an intervening end_study will result in the first end_study not actually ending studying. Again, many methods call end_study, so this is a possible, but unlikely, problem.
177 178 179 180 181 182 |
# File 'lib/rsel/selenium_test.rb', line 177
def begin_study
return skip_status if skip_step?
fail_on_exception do
@study.begin_section { page_to_study }
end
end
|
#button_exists(locator, scope = {}) ⇒ Object
Ensure that a button exists on the page.
570 571 572 573 574 575 576 577 578 579 |
# File 'lib/rsel/selenium_test.rb', line 570
def button_exists(locator, scope={})
return skip_status if skip_step?
locator = loc(locator, 'button', scope)
@study.undo_last_dirty # This method does not modify the browser page contents.
# Study workaround when possible.
bodynode = @study.get_node(locator)
return true if bodynode
pass_if @browser.element?(locator)
end
|
#checkbox_is_disabled(locator, scope = {}) ⇒ Object
Verify that a given checkbox is disabled (unchecked)
867 868 869 870 871 872 873 874 875 876 877 878 |
# File 'lib/rsel/selenium_test.rb', line 867
def checkbox_is_disabled(locator, scope={})
return skip_status if skip_step?
xp = loc(locator, 'checkbox', scope)
@study.undo_last_dirty # This method does not modify the browser page contents.
begin
enabled = @browser.checked?(xp)
rescue
failure "Can't identify checkbox #{locator}"
else
return !enabled
end
end
|
#checkbox_is_enabled(locator, scope = {}) ⇒ Object
Verify that a given checkbox is enabled (checked)
815 816 817 818 819 820 821 822 823 824 825 826 |
# File 'lib/rsel/selenium_test.rb', line 815
def checkbox_is_enabled(locator, scope={})
return skip_status if skip_step?
xp = loc(locator, 'checkbox', scope)
@study.undo_last_dirty # This method does not modify the browser page contents.
begin
enabled = @browser.checked?(xp)
rescue
failure "Can't identify checkbox #{locator}"
else
return enabled
end
end
|
#click(locator, scope = {}) ⇒ Object
Click on a link or button, and wait for a page to load.
708 709 710 711 712 713 |
# File 'lib/rsel/selenium_test.rb', line 708
def click(locator, scope={})
return skip_status if skip_step?
fail_on_exception do
@browser.click(loc(locator, 'link_or_button', scope))
end
end
|
#click_back ⇒ Object
Click the Back button to navigate to the previous page.
281 282 283 284 285 286 287 |
# File 'lib/rsel/selenium_test.rb', line 281
def click_back
return skip_status if skip_step?
end_study
fail_on_exception do
@browser.go_back
end
end
|
#click_button(locator, scope = {}) ⇒ Object Also known as: press
Press a button. Passes if the button exists and is not disabled. Fails if the button is not found, or is disabled.
750 751 752 753 754 755 756 |
# File 'lib/rsel/selenium_test.rb', line 750
def click_button(locator, scope={})
return skip_status if skip_step?
button = loc(locator, 'button', scope)
fail_on_exception do
ensure_editable(button) && @browser.click(button)
end
end
|
#click_link(locator, scope = {}) ⇒ Object Also known as: follow
Click on a link.
728 729 730 731 732 733 |
# File 'lib/rsel/selenium_test.rb', line 728
def click_link(locator, scope={})
return skip_status if skip_step?
fail_on_exception do
@browser.click(loc(locator, 'link', scope))
end
end
|
#close_browser(show_errors = '') ⇒ Object
Stop the session and close the browser window. Show error messages in an exception if requested.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/rsel/selenium_test.rb', line 133
def close_browser(show_errors='')
@browser.close_current_browser_session
end_study
if in_conditional?
# Note the lack of return. This just adds an error to the stack if we're in a conditional.
failure "If without matching End if"
reset_conditionals
end
# Show errors in an exception if requested.
if (!(/not|without/i === show_errors) && @errors.length > 0)
raise StopTestStepFailed, @errors.join("\n").gsub('<','<')
end
return true
end
|
#continue_study ⇒ Object
If studying is turned off, a flag is set, but the page is not deleted. This method turns the flag back on, without reloading the web page. Useful if you called one of the methods that calls #end_study, but you expect that the page (or the relevant part of a page) didn't actually change. (Or you just want to verify data on the old page.)
205 206 207 208 |
# File 'lib/rsel/selenium_test.rb', line 205
def continue_study
return skip_status if skip_step?
pass_if @study.keep_clean(true), "Unable to continue studying: no page has been studied!"
end
|
#disable_checkbox(locator, scope = {}) ⇒ Object
Disable (uncheck) a checkbox by clicking on it. If the checkbox is already disabled, do nothing. Passes if the checkbox exists and is editable. Fails if the checkbox is not found, or is not editable.
795 796 797 798 799 800 801 |
# File 'lib/rsel/selenium_test.rb', line 795
def disable_checkbox(locator, scope={})
return skip_status if skip_step?
cb = loc(locator, 'checkbox', scope)
fail_on_exception do
ensure_editable(cb) && checkbox_is_enabled(cb) && @browser.click(cb)
end
end
|
#do_not_see(text, scope = nil) ⇒ Object
Ensure that the given text does not appear on the current page.
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/rsel/selenium_test.rb', line 355 def do_not_see(text, scope=nil) return skip_status if skip_step? if scope.nil? # Can't do a Study workaround - it doesn't know what's visible. return pass_if [email protected]?(text) else selector = loc("css=", '', scope).strip @study.undo_last_dirty # This method does not modify the browser page contents. begin # Can't do a Study workaround - it doesn't know what's visible. match = selenium_compare(@browser.get_text(selector), globify(text)) return pass_if !match, "'#{text}' not expected, but found in '#{@browser.get_text(selector)}'" rescue # Do not see the selector, so do not see the text within it. return true end end end |
#do_not_see_title(title) ⇒ Object
Ensure that the current page does not have the given title text.
483 484 485 486 |
# File 'lib/rsel/selenium_test.rb', line 483
def do_not_see_title(title)
return skip_status if skip_step?
pass_if !(@browser.get_title == title)
end
|
#do_not_see_within_seconds(text, seconds = -1,, scope = nil) ⇒ Object
Ensure that the given text does not appear on the current page, eventually.
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/rsel/selenium_test.rb', line 433 def do_not_see_within_seconds(text, seconds=-1, scope=nil) return skip_status if skip_step? end_study if scope.nil? && (seconds.is_a? Hash) scope = seconds seconds = -1 end seconds = @browser.default_timeout_in_seconds if seconds == -1 if scope.nil? return pass_if result_within(seconds) { [email protected]?(text) } # This would be better if it worked: # pass_if @browser.wait_for(:no_text => text, :timeout_in_seconds => seconds); else selector = loc("css=", '', scope).strip return pass_if failed_within(seconds) { selenium_compare(@browser.get_text(selector), globify(text)) } end end |
#dropdown_equals(locator, option, scope = {}) ⇒ Object
Check whether an option is currently selected in a dropdown/combo box.
988 989 990 991 992 993 994 995 996 997 998 |
# File 'lib/rsel/selenium_test.rb', line 988
def dropdown_equals(locator, option, scope={})
return skip_status if skip_step?
begin
selected = @browser.get_selected_label(loc(locator, 'select', scope))
@study.undo_last_dirty # This method does not modify the browser page contents.
rescue
failure "Can't identify dropdown #{locator}"
else
pass_if selected == option, "Dropdown equals '#{selected}', not '#{option}'"
end
end
|
#dropdown_includes(locator, option, scope = {}) ⇒ Object
Check whether an option exists in a dropdown/combo box.
963 964 965 966 967 968 969 970 971 972 973 |
# File 'lib/rsel/selenium_test.rb', line 963
def dropdown_includes(locator, option, scope={})
return skip_status if skip_step?
# TODO: Apply scope
dropdown = XPath::HTML.select(locator)
opt = dropdown[XPath::HTML.option(option)]
opt_str = "xpath=#{opt.to_s}"
# Study workaround when possible.
bodynode = @study.get_node(opt_str)
return true if bodynode
pass_if @browser.element?(opt_str)
end
|
#enable_checkbox(locator, scope = {}) ⇒ Object
Enable (check) a checkbox by clicking on it. If the checkbox is already enabled, do nothing. Passes if the checkbox exists and is editable. Fails if the checkbox is not found, or is not editable.
773 774 775 776 777 778 779 |
# File 'lib/rsel/selenium_test.rb', line 773
def enable_checkbox(locator, scope={})
return skip_status if skip_step?
cb = loc(locator, 'checkbox', scope)
fail_on_exception do
ensure_editable(cb) && checkbox_is_disabled(cb) && @browser.click(cb)
end
end
|
#end_if ⇒ Object
End an if block.
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 |
# File 'lib/rsel/selenium_test.rb', line 1555
def end_if
return false if aborted?
# If there was no prior matching if, fail.
return failure "End if without matching if" if !in_conditional?
last_status = @conditional_stack.pop
# If this end_if is within an un-executed if block, don't execute it.
return nil if last_status.nil?
return true
end
|
#end_scenario ⇒ Object
End the current scenario. For now, this does not do anything, but is merely provided for symmetry with #begin_scenario.
251 252 253 254 |
# File 'lib/rsel/selenium_test.rb', line 251
def end_scenario
end_study
return true
end
|
#end_study ⇒ Object
Turn off studying. Several other methods call this, including:
- #click_back
- #close_browser
- #end_scenario
- #page_loads_in_seconds_or_less
- #refresh_page
- #see_within_seconds
- #do_not_see_within_seconds
- #visit Don't be afraid to call this method as many times as you need to!
194 195 196 197 198 |
# File 'lib/rsel/selenium_test.rb', line 194
def end_study
@study.end_section
return skip_status if skip_step?
return true
end
|
#errors ⇒ Object
Show any current error messages. Also clears the error message log.
157 158 159 160 161 |
# File 'lib/rsel/selenium_test.rb', line 157
def errors
current_errors = @errors
@errors = []
return current_errors.join("\n")
end
|
#field_contains(locator, text, scope = {}) ⇒ Object
Verify that a text field contains the given text. The field may include additional text, as long as the expected value is in there somewhere.
660 661 662 663 664 665 666 667 668 669 670 |
# File 'lib/rsel/selenium_test.rb', line 660
def field_contains(locator, text, scope={})
return skip_status if skip_step?
begin
field = @browser.field(loc(locator, 'field', scope))
@study.undo_last_dirty # This method does not modify the browser page contents.
rescue
failure "Can't identify field #{locator}"
else
pass_if field.include?(text), "Field contains '#{field}', not '#{text}'"
end
end
|
#field_equals(locator, text, scope = {}) ⇒ Object
Verify that a text field's value equals the given text. The value must match exactly.
685 686 687 688 689 690 691 692 693 694 695 |
# File 'lib/rsel/selenium_test.rb', line 685
def field_equals(locator, text, scope={})
return skip_status if skip_step?
begin
field = @browser.field(loc(locator, 'field', scope))
@study.undo_last_dirty # This method does not modify the browser page contents.
rescue
failure "Can't identify field #{locator}"
else
pass_if field == text, "Field contains '#{field}', not '#{text}'"
end
end
|
#field_equals_among(field, value, ids = {}, scope = {}) ⇒ Object
Check a value (with #set_field) in the named field, based on the given name/value pairs. Uses Rsel::Support#escape_for_hash to allow certain characters in FitNesse.
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 |
# File 'lib/rsel/selenium_test.rb', line 1296
def field_equals_among(field, value, ids={}, scope={})
return skip_status if skip_step?
# FitNesse passes in "" for an empty field. Fix it.
ids = {} if ids == ""
normalize_ids(ids)
if ids[field.downcase]
return generic_field_equals(escape_for_hash(ids[field.downcase]), value, scope)
else
return generic_field_equals(field, value, scope)
end
end
|
#fields_equal(fields = {}, scope = {}) ⇒ Object
Check values (with #set_field) in the named fields of a hash, based on the given name/value pairs. Uses Rsel::Support#escape_for_hash to allow certain characters in FitNesse. Note: Order of entries is not guaranteed, and depends on the version of Ruby on your server!
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 |
# File 'lib/rsel/selenium_test.rb', line 1321
def fields_equal(fields={}, scope={})
return skip_status if skip_step?
# FitNesse passes in "" for an empty field. Fix it.
fields = {} if fields == ""
method_study = false
if @fields_study_min != 0 && @fields_study_min <= fields.length
method_study = true
# Then we want the page to be studied throughout this method.
begin_study
end
fields.keys.each do |field|
unless generic_field_equals(escape_for_hash(field.to_s), escape_for_hash(fields[field]), scope)
end_study if method_study
return failure
end
end
if method_study
end_study
@study.undo_last_dirty
end
return true
end
|
#fields_equal_among(fields = {}, ids = {}, scope = {}) ⇒ Object
Check values (with #set_field) in the named fields, based on the given name/value pairs, and with mapping of names in the ids field. Uses Rsel::Support#escape_for_hash to allow certain characters in FitNesse. Note: Order of entries is not guaranteed, and depends on the version of Ruby on your server!
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 |
# File 'lib/rsel/selenium_test.rb', line 1383
def fields_equal_among(fields={}, ids={}, scope={})
return skip_status if skip_step?
# FitNesse passes in "" for an empty field. Fix it.
ids = {} if ids == ""
fields = {} if fields == ""
method_study = false
if @fields_study_min != 0 && @fields_study_min <= fields.length
method_study = true
# Then we want the page to be studied throughout this method.
begin_study
end
fields.keys.each do |field|
unless field_equals_among(escape_for_hash(field.to_s), escape_for_hash(fields[field]), ids, scope)
end_study if method_study
return failure
end
end
if method_study
end_study
@study.undo_last_dirty
end
return true
end
|
#fill_in_with(locator, text, scope = {}) ⇒ Object
Fill in a field with the given text. Passes if the field exists and is editable. Fails if the field is not found, or is not editable.
644 645 646 647 |
# File 'lib/rsel/selenium_test.rb', line 644
def fill_in_with(locator, text, scope={})
return skip_status if skip_step?
type_into_field(text, locator, scope)
end
|
#generic_field_equals(locator, value = '', scope = {}) ⇒ Object
A generic way to check any field, of any type. (Just about.) Kind of nasty since it needs to use Javascript on the page.
Types accepted:
- a*
- button*
- input
- type=button*
- type=checkbox
- type=image*
- type=radio*
- type=reset*
- type=submit*
- type=text
- select
- textarea
* Value is ignored: this control type is just clicked/selected.
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 |
# File 'lib/rsel/selenium_test.rb', line 1253
def generic_field_equals(locator, value='', scope={})
return skip_status if skip_step?
fail_on_exception do
loceval = loc(locator, 'field', scope)
@study.undo_last_dirty # This method does not modify the browser page contents.
case tagname(loceval)
when 'input.text', /^textarea\./
return field_equals(loceval, value)
when 'input.radio'
if string_is_true?(value)
return radio_is_enabled(loceval)
else
return radio_is_disabled(loceval)
end
when 'input.checkbox'
if string_is_true?(value)
return checkbox_is_enabled(loceval)
else
return checkbox_is_disabled(loceval)
end
when /^select\./
return dropdown_equals(loceval, value)
else
return failure("Unidentified field for comparison: #{locator}.")
end
end
end
|
#if_i_see(text) ⇒ Object
If I see the given text, do the steps until I see an otherwise or end_if. Otherwise do not do those steps.
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 |
# File 'lib/rsel/selenium_test.rb', line 1493
def if_i_see(text)
return false if aborted?
cond = false
begin
cond = @browser.text?(text)
rescue
# Something went wrong. Therefore, I did not see the text.
cond = false
end
return push_conditional(cond)
end
|
#if_is(text, expected) ⇒ Object
If the first parameter is the same as the second, do the steps until I see an otherwise or end_if. Otherwise do not do those steps.
1546 1547 1548 1549 |
# File 'lib/rsel/selenium_test.rb', line 1546
def if_is(text, expected)
return false if aborted?
return push_conditional(selenium_compare(text, expected))
end
|
#if_parameter(text) ⇒ Object
If the given parameter is "yes" or "true", do the steps until I see an otherwise or end_if. Otherwise do not do those steps.
1521 1522 1523 1524 |
# File 'lib/rsel/selenium_test.rb', line 1521
def if_parameter(text)
return false if aborted?
return push_conditional(string_is_true?(text))
end
|
#link_exists(locator, scope = {}) ⇒ Object
Ensure that a link exists on the page.
545 546 547 548 549 550 551 552 553 554 |
# File 'lib/rsel/selenium_test.rb', line 545
def link_exists(locator, scope={})
return skip_status if skip_step?
locator = loc(locator, 'link', scope)
@study.undo_last_dirty # This method does not modify the browser page contents.
# Study workaround when possible.
bodynode = @study.get_node(locator)
return true if bodynode
pass_if @browser.element?(locator)
end
|
#maximize_browser ⇒ Object
Maximize the browser window. May not work in some browsers.
309 310 311 312 |
# File 'lib/rsel/selenium_test.rb', line 309
def maximize_browser
@browser.window_maximize
return true
end
|
#open_browser ⇒ Object
Start the session and open a browser to the URL defined at the start of the test. If a browser session is already open, just return true.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rsel/selenium_test.rb', line 104
def open_browser
return true if @browser.session_started?
begin
@browser.start_new_browser_session
rescue
raise StopTestCannotConnect,
"Cannot connect to Selenium server at #{@browser.host}:#{@browser.port}"
else
visit @url
end
# Use javascript-xpath for IE, since it's a lot faster than the default
if @browser.browser_string == '*iexplore'
@browser.use_xpath_library('javascript-xpath')
end
# Make Selenium highlight elements whenever it locates them
@browser.highlight_located_element = true
end
|
#otherwise ⇒ Object
The else case to match any if.
| if parameter | $login_by_phone | | type | $login | into field | phone_number | | otherwise | | type | $login | into field | login | | end if |
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 |
# File 'lib/rsel/selenium_test.rb', line 1577
def otherwise
return false if aborted?
# If there was no prior matching if, fail.
return failure "Otherwise without matching if" if !in_conditional?
# If this otherwise is within an un-executed if block, don't execute it.
return nil if in_nil_conditional?
last_stack = @conditional_stack.pop
@conditional_stack.push !last_stack
return true if @conditional_stack.last == true
return nil if @conditional_stack.last == false
return failure
end
|
#page_loads_in_seconds_or_less(seconds) ⇒ Object
Wait some number of seconds for the current page request to finish. Fails if the page does not finish loading within the specified time.
1026 1027 1028 1029 1030 1031 1032 |
# File 'lib/rsel/selenium_test.rb', line 1026
def page_loads_in_seconds_or_less(seconds)
return skip_status if skip_step?
end_study
fail_on_exception do
@browser.wait_for_page_to_load(seconds)
end
end
|
#pause_seconds(seconds) ⇒ Object Also known as: pause_secs
Pause for a certain number of seconds.
1009 1010 1011 1012 1013 |
# File 'lib/rsel/selenium_test.rb', line 1009
def pause_seconds(seconds)
return skip_status if skip_step?
sleep seconds.to_i
return true
end
|
#radio_is_disabled(locator, scope = {}) ⇒ Object
Verify that a given radio button is disabled (unchecked)
894 895 896 897 898 899 900 901 902 903 904 905 |
# File 'lib/rsel/selenium_test.rb', line 894
def radio_is_disabled(locator, scope={})
return skip_status if skip_step?
xp = loc(locator, 'radio_button', scope)
@study.undo_last_dirty # This method does not modify the browser page contents.
begin
enabled = @browser.checked?(xp)
rescue
failure "Can't identify radio #{locator}"
else
return !enabled
end
end
|
#radio_is_enabled(locator, scope = {}) ⇒ Object
Verify that a given radio button is enabled (checked)
842 843 844 845 846 847 848 849 850 851 852 853 |
# File 'lib/rsel/selenium_test.rb', line 842
def radio_is_enabled(locator, scope={})
return skip_status if skip_step?
xp = loc(locator, 'radio_button', scope)
@study.undo_last_dirty # This method does not modify the browser page contents.
begin
enabled = @browser.checked?(xp)
rescue
failure "Can't identify radio #{locator}"
else
return enabled
end
end
|
#refresh_page ⇒ Object
Reload the current page.
295 296 297 298 299 300 301 |
# File 'lib/rsel/selenium_test.rb', line 295
def refresh_page
return skip_status if skip_step?
end_study
fail_on_exception do
@browser.refresh
end
end
|
#reset_conditionals ⇒ Object
Reset the conditional stack to its initial state. This method is included mainly for unit testing purposes, and is not intended to be called by normal test scripts.
1598 1599 1600 |
# File 'lib/rsel/selenium_test.rb', line 1598
def reset_conditionals
@conditional_stack = [true]
end
|
#respond_to?(orgmethod, include_private = false) ⇒ Boolean
Return true if SeleniumTest explicitly responds to a given method name, or if the underlying Selenium::Client::Driver instance can respond to it. This is a counterpart to #method_missing, used for checking whether a given method can be called on this instance.
1468 1469 1470 1471 1472 1473 1474 1475 1476 |
# File 'lib/rsel/selenium_test.rb', line 1468
def respond_to?(orgmethod, include_private=false)
# Allow methods like "Type" that have Ruby homonyms to be called with a "selenium" prefix.
method = orgmethod.to_s.sub(/^(check_)?(selenium_)?/,'')
if @browser.respond_to?(method)
true
else
super
end
end
|
#row_exists(cells) ⇒ Object
Ensure that a table row with the given cell values exists. Order does not matter as of v0.1.1.
595 596 597 598 599 600 601 602 603 604 |
# File 'lib/rsel/selenium_test.rb', line 595
def row_exists(cells)
return skip_status if skip_step?
cells = cells.split(/, */).map { |s| escape_for_hash(s) }
locator = "xpath=#{xpath_row_containing(cells)}"
# Study workaround when possible.
bodynode = @study.get_node(locator)
return true if bodynode
pass_if @browser.element?(locator)
end
|
#see(text, scope = nil) ⇒ Object
Ensure that the given text appears on the current page.
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/rsel/selenium_test.rb', line 326
def see(text, scope=nil)
return skip_status if skip_step?
if scope.nil?
# Can't do a Study workaround - it doesn't know what's visible.
return pass_if @browser.text?(text)
else
selector = loc("css=", '', scope).strip
@study.undo_last_dirty # This method does not modify the browser page contents.
# Can't do a Study workaround - it doesn't know what's visible.
fail_on_exception do
match = selenium_compare(@browser.get_text(selector), globify(text))
return pass_if match,
"'#{text}' not found in '#{@browser.get_text(selector)}'"
end
end
end
|
#see_alert_within_seconds(text = nil, seconds = -1)) ⇒ Object
Ensure that an alert appears, optionally having the given text, within the given time or the default timeout.
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 |
# File 'lib/rsel/selenium_test.rb', line 507
def see_alert_within_seconds(text=nil, seconds=-1)
return skip_status if skip_step?
# Handle the case of being given seconds, but not text.
if seconds == -1
begin
if Integer(text.to_s).to_s == text.to_s
seconds = text.to_s
text = nil
end
rescue
end
end
seconds = @browser.default_timeout_in_seconds if seconds == -1
alert_text = result_within(seconds) {
@browser.get_alert
}
if alert_text
return true if text.nil?
return pass_if selenium_compare(alert_text, text),
"Expected alert '#{text}', but got '#{alert_text}'!"
else
return failure
end
end
|
#see_title(title) ⇒ Object
Ensure that the current page has the given title text.
465 466 467 468 469 470 471 472 |
# File 'lib/rsel/selenium_test.rb', line 465
def see_title(title)
return skip_status if skip_step?
# Study workaround when possible. (Probably won't happen a lot, but possible.)
bodynode = @study.get_node('xpath=/html/head/title')
return true if bodynode && bodynode.inner_text.strip == title
pass_if @browser.get_title == title,
"Page title is '#{@browser.get_title}', not '#{title}'"
end
|
#see_within_seconds(text, seconds = -1,, scope = nil) ⇒ Object
Ensure that the given text appears on the current page, eventually.
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'lib/rsel/selenium_test.rb', line 396
def see_within_seconds(text, seconds=-1, scope=nil)
return skip_status if skip_step?
end_study
if scope.nil? && (seconds.is_a? Hash)
scope = seconds
seconds = -1
end
seconds = @browser.default_timeout_in_seconds if seconds == -1
if scope.nil?
return pass_if result_within(seconds) {
@browser.text?(text)
}
# This would be better if it worked:
# pass_if @browser.wait_for(:text => text, :timeout_in_seconds => seconds);
else
selector = loc("css=", '', scope).strip
return pass_if result_within(seconds) {
selenium_compare(@browser.get_text(selector), globify(text))
}
end
end
|
#select_from_dropdown(option, locator, scope = {}) ⇒ Object
Select an option from a dropdown/combo box. Passes if the dropdown exists, is editable, and includes the given option. Fails if the dropdown is not found, the option is not found, or the dropdown is not editable.
942 943 944 945 946 947 948 |
# File 'lib/rsel/selenium_test.rb', line 942
def select_from_dropdown(option, locator, scope={})
return skip_status if skip_step?
dropdown = loc(locator, 'select', scope)
fail_on_exception do
ensure_editable(dropdown) && @browser.select(dropdown, option)
end
end
|
#select_radio(locator, scope = {}) ⇒ Object
Select a radio button. Passes if the radio button exists and is editable. Fails if the radiobutton is not found, or is not editable.
920 921 922 923 924 925 926 |
# File 'lib/rsel/selenium_test.rb', line 920
def select_radio(locator, scope={})
return skip_status if skip_step?
radio = loc(locator, 'radio_button', scope)
fail_on_exception do
ensure_editable(radio) && @browser.click(radio)
end
end
|
#set_field(locator, value = '', scope = {}) ⇒ Object
A generic way to fill in any field, of any type. (Just about.) Kind of nasty since it needs to use Javascript on the page.
Types accepted:
- a*
- button*
- input
- type=button*
- type=checkbox
- type=image*
- type=radio*
- type=reset*
- type=submit*
- type=text
- select
- textarea
* Value is ignored: this control type is just clicked/selected.
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 |
# File 'lib/rsel/selenium_test.rb', line 1064
def set_field(locator, value='', scope={})
return skip_status if skip_step?
fail_on_exception do
loceval = loc(locator, 'field', scope)
mytagname = ''
begin
mytagname = tagname(loceval)
rescue
loceval = loc(locator, 'link_or_button', scope)
mytagname = tagname(loceval)
end
#puts "My tag name is #{mytagname}\n"
#puts "My loceval is #{loceval}\n" #if /^id=/ === loceval
case mytagname
when 'input.text', /^textarea\./
return type_into_field(value, loceval)
when 'input.radio'
return select_radio(loceval)
when 'input.checkbox'
if string_is_true?(value)
return enable_checkbox(loceval)
else
return disable_checkbox(loceval)
end
when /^select\./
return select_from_dropdown(value, loceval)
when /^(a|button)\./,'input.button','input.submit','input.image','input.reset'
return click(loceval)
else
return failure("Unidentified field #{locator}.")
end
end
end
|
#set_field_among(field, value, ids = {}, scope = {}) ⇒ Object
Set a value (with #set_field) in the named field, based on the given name/value pairs. Uses Rsel::Support#escape_for_hash to allow certain characters in FitNesse.
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 |
# File 'lib/rsel/selenium_test.rb', line 1114
def set_field_among(field, value, ids={}, scope={})
return skip_status if skip_step?
# FitNesse passes in "" for an empty field. Fix it.
ids = {} if ids == ""
normalize_ids(ids)
if ids[field.downcase]
return set_field(escape_for_hash(ids[field.downcase]), value, scope)
else
return set_field(field, value, scope)
end
end
|
#set_fields(fields = {}, scope = {}) ⇒ Object
Set values (with #set_field) in the named fields of a hash, based on the given name/value pairs. Uses Rsel::Support#escape_for_hash to allow certain characters in FitNesse. Note: Order of entries is not guaranteed, and depends on the version of Ruby on your server!
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 |
# File 'lib/rsel/selenium_test.rb', line 1139
def set_fields(fields={}, scope={})
return skip_status if skip_step?
# FitNesse passes in "" for an empty field. Fix it.
fields = {} if fields == ""
method_study = false
if @fields_study_min != 0 && @fields_study_min <= fields.length
method_study = true
# Then we want the page to be studied throughout this method.
begin_study
end
fields.each do |key, value|
key_esc = escape_for_hash(key.to_s)
value_esc = escape_for_hash(value.to_s)
unless set_field(key_esc, value_esc, scope)
end_study if method_study
return failure "Failed to set field #{key_esc} to #{value_esc}"
end
end
end_study if method_study
return true
end
|
#set_fields_among(fields = {}, ids = {}, scope = {}) ⇒ Object
Set values (with #set_field) in the named fields, based on the given name/value pairs, and with mapping of names in the ids field. Uses Rsel::Support#escape_for_hash to allow certain characters in FitNesse. Note: Order of entries is not guaranteed, and depends on the version of Ruby on your server!
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 |
# File 'lib/rsel/selenium_test.rb', line 1199
def set_fields_among(fields={}, ids={}, scope={})
return skip_status if skip_step?
# FitNesse passes in "" for an empty field. Fix it.
ids = {} if ids == ""
fields = {} if fields == ""
method_study = false
if @fields_study_min != 0 && @fields_study_min <= fields.length
method_study = true
# Then we want the page to be studied throughout this method.
begin_study
end
fields.each do |key, value|
key_esc = escape_for_hash(key.to_s)
value_esc = escape_for_hash(value.to_s)
unless set_field_among(key_esc, value_esc, ids, scope)
end_study if method_study
return failure("Failed to set #{key_esc} (#{ids[key_esc]}) to #{value_esc}")
end
end
end_study if method_study
return true
end
|
#set_fields_study_min(level = nil) ⇒ Object
Set the minimum number of fields that must appear in a set_fields before studying is invoked. "Never", or 0, turns off studying. "Always", or 1, turns studying on whenever a long xpath is found. Any other non-integer string resumes the studying pattern set in the initial option.
218 219 220 221 222 223 224 |
# File 'lib/rsel/selenium_test.rb', line 218
def set_fields_study_min(level=nil)
return skip_status if skip_step?
fail_on_exception do
@fields_study_min = parse_fields_study_min(level, @default_fields_study_min)
end_study if @fields_study_min == 0
end
end
|
#type_into_field(text, locator, scope = {}) ⇒ Object
Type a value into the given field. Passes if the field exists and is editable. Fails if the field is not found, or is not editable.
622 623 624 625 626 627 628 |
# File 'lib/rsel/selenium_test.rb', line 622
def type_into_field(text, locator, scope={})
return skip_status if skip_step?
field = loc(locator, 'field', scope)
fail_on_exception do
ensure_editable(field) && @browser.type(field, text)
end
end
|
#visit(path_or_url) ⇒ Object
Load an absolute URL or a relative path in the browser.
267 268 269 270 271 272 273 |
# File 'lib/rsel/selenium_test.rb', line 267
def visit(path_or_url)
return skip_status if skip_step?
end_study
fail_on_exception do
@browser.open(strip_tags(path_or_url))
end
end
|