Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Namespace locators in Webrat::Locators
- Loading branch information
Showing
6 changed files
with
164 additions
and
128 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,30 @@ | ||
require "webrat/core/locators/locator" | ||
|
||
class AreaLocator < Locator | ||
module Webrat | ||
module Locators | ||
|
||
class AreaLocator < Locator | ||
|
||
def locate | ||
@scope.area_by_element(area_element) | ||
end | ||
def locate | ||
@scope.area_by_element(area_element) | ||
end | ||
|
||
def area_element | ||
area_elements.detect do |area_element| | ||
Webrat::XML.attribute(area_element, "title") =~ matcher || | ||
Webrat::XML.attribute(area_element, "id") =~ matcher | ||
end | ||
end | ||
def area_element | ||
area_elements.detect do |area_element| | ||
Webrat::XML.attribute(area_element, "title") =~ matcher || | ||
Webrat::XML.attribute(area_element, "id") =~ matcher | ||
end | ||
end | ||
|
||
def matcher | ||
/#{Regexp.escape(@value.to_s)}/i | ||
end | ||
def matcher | ||
/#{Regexp.escape(@value.to_s)}/i | ||
end | ||
|
||
def area_elements | ||
Webrat::XML.css_search(@scope.dom, "area") | ||
end | ||
def area_elements | ||
Webrat::XML.css_search(@scope.dom, "area") | ||
end | ||
|
||
end | ||
|
||
end | ||
end |
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 |
---|---|---|
@@ -1,40 +1,46 @@ | ||
require "webrat/core/locators/locator" | ||
|
||
class ButtonLocator < Locator | ||
module Webrat | ||
module Locators | ||
|
||
class ButtonLocator < Locator | ||
|
||
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, *ButtonField.xpath_search) | ||
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 |
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 |
---|---|---|
@@ -1,23 +1,29 @@ | ||
require "webrat/core/locators/locator" | ||
|
||
class FieldByIdLocator < Locator | ||
module Webrat | ||
module Locators | ||
|
||
class FieldByIdLocator < Locator | ||
|
||
def locate | ||
@scope.field_by_element(field_element) | ||
end | ||
def locate | ||
@scope.field_by_element(field_element) | ||
end | ||
|
||
def field_element | ||
field_elements.detect do |field_element| | ||
if @value.is_a?(Regexp) | ||
Webrat::XML.attribute(field_element, "id") =~ @value | ||
else | ||
Webrat::XML.attribute(field_element, "id") == @value.to_s | ||
def field_element | ||
field_elements.detect do |field_element| | ||
if @value.is_a?(Regexp) | ||
Webrat::XML.attribute(field_element, "id") =~ @value | ||
else | ||
Webrat::XML.attribute(field_element, "id") == @value.to_s | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
def field_elements | ||
Webrat::XML.xpath_search(@scope.dom, *Webrat::Field.xpath_search) | ||
end | ||
def field_elements | ||
Webrat::XML.xpath_search(@scope.dom, *Field.xpath_search) | ||
end | ||
|
||
end | ||
|
||
end | ||
end |
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 |
---|---|---|
@@ -1,27 +1,33 @@ | ||
require "webrat/core/locators/locator" | ||
|
||
class FieldNamedLocator < Locator | ||
module Webrat | ||
module Locators | ||
|
||
class FieldNamedLocator < Locator | ||
|
||
def locate | ||
@scope.field_by_element(field_element) | ||
end | ||
def locate | ||
@scope.field_by_element(field_element) | ||
end | ||
|
||
def field_element | ||
field_elements.detect do |field_element| | ||
Webrat::XML.attribute(field_element, "name") == @value.to_s | ||
end | ||
end | ||
def field_element | ||
field_elements.detect do |field_element| | ||
Webrat::XML.attribute(field_element, "name") == @value.to_s | ||
end | ||
end | ||
|
||
def field_elements | ||
Webrat::XML.xpath_search(@scope.dom, *xpath_searches) | ||
end | ||
def field_elements | ||
Webrat::XML.xpath_search(@scope.dom, *xpath_searches) | ||
end | ||
|
||
def xpath_searches | ||
if @field_types.any? | ||
@field_types.map { |field_type| field_type.xpath_search }.flatten | ||
else | ||
Array(Field.xpath_search) | ||
end | ||
end | ||
|
||
def xpath_searches | ||
if @field_types.any? | ||
@field_types.map { |field_type| field_type.xpath_search }.flatten | ||
else | ||
Array(Field.xpath_search) | ||
end | ||
|
||
end | ||
|
||
end |
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 |
---|---|---|
@@ -1,52 +1,58 @@ | ||
require "webrat/core/locators/locator" | ||
|
||
class LinkLocator < Locator | ||
module Webrat | ||
module Locators | ||
|
||
class LinkLocator < Locator | ||
|
||
def locate | ||
@scope.link_by_element(link_element) | ||
end | ||
def locate | ||
@scope.link_by_element(link_element) | ||
end | ||
|
||
def link_element | ||
matching_links.min { |a, b| Webrat::XML.inner_text(a).length <=> Webrat::XML.inner_text(b).length } | ||
end | ||
def link_element | ||
matching_links.min { |a, b| Webrat::XML.inner_text(a).length <=> Webrat::XML.inner_text(b).length } | ||
end | ||
|
||
def matching_links | ||
@matching_links ||= link_elements.select do |link_element| | ||
matches_text?(link_element) || | ||
matches_id?(link_element) | ||
end | ||
end | ||
def matching_links | ||
@matching_links ||= link_elements.select do |link_element| | ||
matches_text?(link_element) || | ||
matches_id?(link_element) | ||
end | ||
end | ||
|
||
def matches_text?(link) | ||
if @value.is_a?(Regexp) | ||
matcher = @value | ||
else | ||
matcher = /#{Regexp.escape(@value.to_s)}/i | ||
end | ||
def matches_text?(link) | ||
if @value.is_a?(Regexp) | ||
matcher = @value | ||
else | ||
matcher = /#{Regexp.escape(@value.to_s)}/i | ||
end | ||
|
||
replace_nbsp(Webrat::XML.inner_text(link)) =~ matcher || | ||
replace_nbsp_ref(Webrat::XML.inner_html(link)) =~ matcher || | ||
Webrat::XML.attribute(link, "title")=~ matcher | ||
end | ||
replace_nbsp(Webrat::XML.inner_text(link)) =~ matcher || | ||
replace_nbsp_ref(Webrat::XML.inner_html(link)) =~ matcher || | ||
Webrat::XML.attribute(link, "title")=~ matcher | ||
end | ||
|
||
def matches_id?(link) | ||
if @value.is_a?(Regexp) | ||
(Webrat::XML.attribute(link, "id") =~ @value) ? true : false | ||
else | ||
(Webrat::XML.attribute(link, "id") == @value) ? true : false | ||
end | ||
end | ||
def matches_id?(link) | ||
if @value.is_a?(Regexp) | ||
(Webrat::XML.attribute(link, "id") =~ @value) ? true : false | ||
else | ||
(Webrat::XML.attribute(link, "id") == @value) ? true : false | ||
end | ||
end | ||
|
||
def link_elements | ||
Webrat::XML.css_search(@scope.dom, *Webrat::Link.css_search) | ||
end | ||
def link_elements | ||
Webrat::XML.css_search(@scope.dom, *Link.css_search) | ||
end | ||
|
||
def replace_nbsp(str) | ||
str.gsub([0xA0].pack('U'), ' ') | ||
end | ||
def replace_nbsp(str) | ||
str.gsub([0xA0].pack('U'), ' ') | ||
end | ||
|
||
def replace_nbsp_ref(str) | ||
str.gsub(' ',' ').gsub(' ', ' ') | ||
end | ||
def replace_nbsp_ref(str) | ||
str.gsub(' ',' ').gsub(' ', ' ') | ||
end | ||
|
||
end | ||
|
||
end | ||
end |
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 |
---|---|---|
@@ -1,9 +1,15 @@ | ||
class Locator | ||
module Webrat | ||
module Locators | ||
|
||
def initialize(scope, value, *field_types) | ||
@scope = scope | ||
@value = value | ||
@field_types = field_types | ||
end | ||
class Locator | ||
|
||
def initialize(scope, value, *field_types) | ||
@scope = scope | ||
@value = value | ||
@field_types = field_types | ||
end | ||
|
||
end | ||
|
||
end | ||
end |