Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
46 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
class ButtonLocator | ||
|
||
def initialize(scope, value) | ||
@scope = scope | ||
@value = value | ||
end | ||
|
||
def locate | ||
@scope.field_by_element(button_element) | ||
end | ||
|
||
def button_element | ||
button_elements.detect do |element| | ||
@value.nil? || | ||
matches_id?(element) || | ||
matches_value?(element) || | ||
matches_html?(element) || | ||
matches_alt?(element) | ||
end | ||
end | ||
|
||
def matches_id?(element) | ||
(@value.is_a?(Regexp) && Webrat::XML.attribute(element, "id") =~ @value) || | ||
(!@value.is_a?(Regexp) && Webrat::XML.attribute(element, "id") == @value.to_s) | ||
end | ||
|
||
def matches_value?(element) | ||
Webrat::XML.attribute(element, "value") =~ /^\W*#{Regexp.escape(@value.to_s)}/i | ||
end | ||
|
||
def matches_html?(element) | ||
Webrat::XML.inner_html(element) =~ /#{Regexp.escape(@value.to_s)}/i | ||
end | ||
|
||
def matches_alt?(element) | ||
Webrat::XML.attribute(element, "alt") =~ /^\W*#{Regexp.escape(@value.to_s)}/i | ||
end | ||
|
||
def button_elements | ||
Webrat::XML.xpath_search(@scope.dom, *Webrat::ButtonField.xpath_search) | ||
end | ||
|
||
end |