Module: Watigiri::Locators::LocatorHelpers
- Included in:
- Button::Locator, Cell::Locator, Element::Locator, Row::Locator, TextArea::Locator, TextField::Locator
- Defined in:
- lib/watigiri/locators/element/locator.rb
Instance Method Summary collapse
- #fetch_value(element, how) ⇒ Object
- #locate ⇒ Object
- #locate_all ⇒ Object
-
#locate_element(how, what, _driver_scope = @query_scope.wd) ⇒ Object
Is only used when there is no regex, index or visibility locators.
-
#locate_elements(how, what, _scope = @query_scope.wd) ⇒ Object
“how” can only be :css or :xpath.
- #noko_element(element) ⇒ Object
- #nokogiri_to_selenium(element) ⇒ Object
- #regex? ⇒ Boolean
- #set_nokogiri ⇒ Object
- #text_regexp_deprecation ⇒ Object
Instance Method Details
#fetch_value(element, how) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/watigiri/locators/element/locator.rb', line 55 def fetch_value(element, how) noko_element = noko_element(element) return super if noko_element.nil? case how when :text noko_element.inner_text when :tag_name noko_element.name.downcase else noko_element.attribute(how.to_s.tr('_', '-')).to_s.strip end end |
#locate ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/watigiri/locators/element/locator.rb', line 17 def locate @nokogiri = @selector.delete(:nokogiri) return super unless @nokogiri || regex? set_nokogiri element = using_watir(:first) return if element.nil? @nokogiri ? element.element : nokogiri_to_selenium(element) end |
#locate_all ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/watigiri/locators/element/locator.rb', line 28 def locate_all @nokogiri = @selector.delete(:nokogiri) return super unless @nokogiri || regex? set_nokogiri elements = using_watir(:all) @nokogiri ? elements.map(&:element) : elements.map { |el| nokogiri_to_selenium(el) } end |
#locate_element(how, what, _driver_scope = @query_scope.wd) ⇒ Object
Is only used when there is no regex, index or visibility locators
39 40 41 42 43 44 |
# File 'lib/watigiri/locators/element/locator.rb', line 39 def locate_element(how, what, _driver_scope = @query_scope.wd) return super unless @nokogiri el = @query_scope.doc.send("at_#{how}", what) Watigiri::Element.new element: el, selector: {how => what} end |
#locate_elements(how, what, _scope = @query_scope.wd) ⇒ Object
“how” can only be :css or :xpath
47 48 49 50 51 52 53 |
# File 'lib/watigiri/locators/element/locator.rb', line 47 def locate_elements(how, what, _scope = @query_scope.wd) return super unless @nokogiri || regex? @query_scope.doc.send(how, what).map do |el| Watigiri::Element.new element: el, selector: {how => what} end end |
#noko_element(element) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/watigiri/locators/element/locator.rb', line 83 def noko_element(element) if !(@nokogiri || regex?) || element.is_a?(Selenium::WebDriver::Element) nil elsif element.is_a?(Watigiri::Element) element.element else element end end |
#nokogiri_to_selenium(element) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/watigiri/locators/element/locator.rb', line 69 def nokogiri_to_selenium(element) return element if element.is_a?(Selenium::WebDriver::Element) tag = element.tag_name index = @query_scope.doc.xpath(".//#{tag}").find_index { |el| el == element.element } Watir::Element.new(@query_scope, index: index, tag_name: tag).wd end |
#regex? ⇒ Boolean
76 77 78 79 80 81 |
# File 'lib/watigiri/locators/element/locator.rb', line 76 def regex? return @regex unless @regex.nil? return false unless (@selector.keys & %i[adjacent visible label text visible_text visible_label]).empty? @regex = @selector.values.any? { |v| v.is_a?(Regexp) } end |
#set_nokogiri ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/watigiri/locators/element/locator.rb', line 93 def set_nokogiri return if @query_scope.doc # should be using `#fragment` instead of `#inner_html`, but can't because of # https://github.com/sparklemotion/nokogiri/issues/572 doc = if @query_scope.is_a?(Watir::Browser) Nokogiri::HTML(@query_scope.html) else Nokogiri::HTML(@query_scope.inner_html) end @query_scope.doc = doc.tap { |d| d.css('script').remove } end |
#text_regexp_deprecation ⇒ Object
107 108 109 |
# File 'lib/watigiri/locators/element/locator.rb', line 107 def text_regexp_deprecation(*) # Nokogiri can not determine visible text so no need to check end |