Module: Awetestlib::Regression::Find
- Defined in:
- lib/awetestlib/regression/find.rb,
lib/awetestlib/regression/awetest_dsl.rb
Overview
Methods to fetch references to DOM elements for assigning to variables. Includes collections of elements. Primary use is to limit the scope of other commands to the element passed in their browser parameter. (example here)
Core collapse
- #capture_value_desc(value, desc, refs, options = nil) ⇒ Object
-
#find_all_links_with_exact_href(browser, href) ⇒ Hash
Return a hash of all links in browser with :href attribute containing the exact url in href.
-
#find_link_with_exact_href(browser, href) ⇒ Watir::Link
Return a reference to the first link in browser with :href attribute containing the exact url in href.
- #get_ancestor(descendant, element, how, what, desc = '', refs = '', dbg = $debug) ⇒ Object
- #get_attribute_value(container, element, how, what, attribute, desc = '', refs = '') ⇒ Object
- #get_directory(path) ⇒ Object
-
#get_div(browser, how, what, desc = '', dbg = false) ⇒ Watir::Div
Return a reference to a div element identified by the contents what of its attribute how.
-
#get_element(container, element, how, what, value = nil, desc = '', refs = '', options = {}) ⇒ Object
Return a reference to a DOM element specified by its type element, attribute how, and the contents of that attribute what.
-
#get_element_collection(browser, element, dbg = false) ⇒ Array
(also: #get_objects)
Return an array (collection) of all the elements of the type which contained in the browser or container supplied in browser.
-
#get_form(browser, how, what, desc = '') ⇒ Watir::Form
Return a reference to a form element identified by the contents what of its attribute how.
-
#get_frame(browser, how, what, desc = '') ⇒ Watir::Frame
Return a reference to a frame element identified by the contents what of its attribute how.
-
#get_ole(element) ⇒ Object
Return the ole object for the specified element.
-
#get_select_list(browser, how, what, desc = '') ⇒ Watir::SelectList
Return a reference to a select_list element identified by the contents what of its attribute how.
-
#get_select_options(browser, how, what, dump = false) ⇒ Array
Return an array containing the options available for selection in a select_list identifified by its attribute how, and the contents of that attribute what.
-
#get_selected_options(browser, how, what, desc = '', refs = '') ⇒ Array
Return an array containing the selected options in a select_list identified by its attribute how, and the contents of that attribute what.
-
#get_span(browser, how, what, desc = '') ⇒ Watir::Span
Return a reference to a span element identified by the contents what of its attribute how.
Deprecated collapse
- #find_index_for_element(browser, element, how, ord, what) ⇒ Object (also: #find_index_for_object) deprecated Deprecated.
Instance Method Summary collapse
- #ancestor_is_a?(descendant, tag_name, generation = 1, desc = '', refs = '') ⇒ Boolean
-
#get_active_element(container) ⇒ Object
(also: #find_element_with_focus)
def find_element_with_focus(container) container.driver.browser.switch_to.active_element end.
- #get_ancestor2(descendant, element, how, what, desc = '', dbg = $DEBUG) ⇒ Object
- #get_ancestor3(descendant, elements = [:any], hows = [], whats = [], desc = '', dbg = $DEBUG) ⇒ Object
- #get_ancestor4(descendant, args = {}) ⇒ Object
- #get_element_attribute(element, attribute, desc = '', refs = '', how = '', what = nil) ⇒ Object
- #get_element_colors(element, colors = [], desc = '', refs = '') ⇒ Object
- #get_style(container, element, how, what, style, desc = '', refs = '') ⇒ Object (also: #get_style_value)
- #highlight_element(element) ⇒ Object
- #identify_active_element(container, desc = '', parent = false) ⇒ Object
Instance Method Details
#ancestor_is_a?(descendant, tag_name, generation = 1, desc = '', refs = '') ⇒ Boolean
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/awetestlib/regression/awetest_dsl.rb', line 510 def ancestor_is_a?(descendant, tag_name, generation = 1, desc = '', refs = '') tag_name = 'a' if tag_name == 'link' msg = (desc, "#{descendant.tag_name.upcase} with id '#{descendant.attribute_value('id')}' has", "tag: '#{tag_name}'", "#{generation} level above") ancestor = descendant.dup count = 0 while count < generation ancestor = ancestor.parent count += 1 end if ancestor.respond_to?(:tag_name) and ancestor.tag_name.downcase == tag_name.downcase passed_to_log(with_caller(msg, desc, refs)) true else failed_to_log(with_caller(msg, desc, refs)) end rescue failed_to_log(unable_to, false, true) end |
#capture_value_desc(value, desc, refs, options = nil) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/awetestlib/regression/find.rb', line 95 def capture_value_desc(value, desc, = nil) opt = .dup if unless opt.kind_of?(Hash) opt = Hash.new end if value vlu = value.dup if opt[:value] vlu = nil else opt[:value] = vlu end end if desc dsc = desc.dup unless opt[:desc] opt[:desc] = dsc end end [vlu, dsc, opt] rescue failed_to_log(unable_to) end |
#find_all_links_with_exact_href(browser, href) ⇒ Hash
Return a hash of all links in browser with :href attribute containing the exact url in href.
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/awetestlib/regression/find.rb', line 301 def find_all_links_with_exact_href(browser, href) links = browser.links hash = Hash.new idx = 0 links.each do |l| idx += 1 an_href = href my_href = l.href if my_href == an_href hash[idx] = l debug_to_log("#{__method__}:#{idx}\n********\n#{l.to_s}\n\n#{l.to_yaml}") end end hash end |
#find_index_for_element(browser, element, how, ord, what) ⇒ Object Also known as: find_index_for_object
Find the index of an element within browser which has attribute how containing what
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/awetestlib/regression/find.rb', line 344 def find_index_for_element(browser, element, how, ord, what) element_sym = (element.to_s.pluralize).to_sym how_str = how.to_s ptrn = /#{how}:\s+#{what}/i list = get_element_collection(browser, element_sym, true) cnt = 0 idx = 0 list.each do |nty| s = nty.to_s # a = nty.to_a if s =~ ptrn cnt += 1 if cnt == ord break end end idx += 1 end idx end |
#find_link_with_exact_href(browser, href) ⇒ Watir::Link
Return a reference to the first link in browser with :href attribute containing the exact url in href.
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/awetestlib/regression/find.rb', line 321 def find_link_with_exact_href(browser, href) links = browser.links link = nil index = 0 links.each do |l| index += 1 an_href = href my_href = l.href if my_href == an_href link = l # debug_to_log("#{__method__}:#{__LINE__}\n********\n#{l.to_s}\n\n#{l.to_yaml}") break end end link end |
#get_active_element(container) ⇒ Object Also known as: find_element_with_focus
def find_element_with_focus(container)
container.driver.browser.switch_to.active_element
end
700 701 702 |
# File 'lib/awetestlib/regression/awetest_dsl.rb', line 700 def get_active_element(container) container.execute_script("return document.activeElement") end |
#get_ancestor(descendant, element, how, what, desc = '', refs = '', dbg = $debug) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/awetestlib/regression/find.rb', line 68 def get_ancestor(descendant, element, how, what, desc = '') found = false how = 'class_name' if how.to_s == 'class' tag = element.to_s.downcase debug_to_log("target: #{descendant.tag_name} :id=>#{descendant.id}") debug_to_log("goal: #{element} :#{how}=>#{what} #{desc}") ancestor = target.parent debug_to_log("#{ancestor.tag_name}: :class=>'#{ancestor.class_name}'") code = "ancestor.#{how}" what.is_a?(Regexp) ? code << " =~ /#{what.source}/" : code << " == '#{what}'" debug_to_log("#{code}") until found do debug_to_log("#{ancestor.tag_name}: :class=>'#{ancestor.class_name}'") if ancestor.tag_name == tag if eval(code) found = true break end end break unless ancestor ancestor = ancestor.parent end ancestor rescue failed_to_log(unable_to) end |
#get_ancestor2(descendant, element, how, what, desc = '', dbg = $DEBUG) ⇒ Object
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
# File 'lib/awetestlib/regression/awetest_dsl.rb', line 569 def get_ancestor2(descendant, element, how, what, desc = '', dbg = $DEBUG) elements = Array.new if element.is_a?(Array) element.each do |e| elements << e.to_s.downcase end else elements[0] = element.tag_name.downcase end found = false how = 'class_name' if how.to_s == 'class' debug_to_log("target: #{descendant.tag_name} :id=>'#{descendant.attribute_value('id')}'") if dbg debug_to_log("goal: #{nice_array(elements)} :#{how}='#{what}' #{desc}") if dbg ancestor = descendant.parent debug_to_log("#{ancestor.tag_name}: :class=>'#{ancestor.class_name}'") if dbg code = "ancestor.#{how}" what.is_a?(Regexp) ? code << " =~ /#{what.source}/" : code << " == '#{what}'" debug_to_log("#{code}") if dbg until found do break unless ancestor if ancestor.tag_name =~ /html/i ancestor = nil break end debug_to_log("#{ancestor.tag_name}: :class=>'#{ancestor.class_name}' :id=>'#{ancestor.attribute_value('id')}'") if dbg if elements.include?(ancestor.tag_name.downcase) if eval(code) found = true break end end ancestor = ancestor.parent end [ancestor, (ancestor.tag_name.downcase if ancestor)] rescue failed_to_log(unable_to) end |
#get_ancestor3(descendant, elements = [:any], hows = [], whats = [], desc = '', dbg = $DEBUG) ⇒ Object
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 |
# File 'lib/awetestlib/regression/awetest_dsl.rb', line 611 def get_ancestor3(descendant, elements = [:any], hows = [], whats = [], desc = '', dbg = $DEBUG) fail 'Parameter \'elements\' must be an array.' unless elements.is_a?(Array) fail 'Parameter \'hows\' must be an array.' unless hows.is_a?(Array) fail 'Parameter \'whats\' must be an array.' unless whats.is_a?(Array) fail 'Parameters \'hows\' and \'whats\' must be the same length.' unless hows.length == whats.length found = false debug_to_log("target: #{descendant.tag_name} :id=>'#{descendant.attribute_value('id')}'") if dbg debug_to_log("goal: #{element.tag_name.upcase} :#{how}='#{what}' #{desc}") if dbg ancestor = descendant.parent debug_to_log("#{ancestor.tag_name}: :class=>'#{ancestor.class_name}'") if dbg code == '' (0..hows.length).each do |idx| hows[idx] = 'class_name' if hows[idx].to_s == 'class' code = "ancestor.#{how}" whats[idx].is_a?(Regexp) ? code << " =~ /#{whats[idx].source}/" : code << " == '#{whats[idx]}'" code << ' and ' if idx < hows.length debug_to_log("#{code}") if dbg end until found do break unless ancestor break if ancestor.tag_name =~ /html/i debug_to_log("#{ancestor.tag_name}: :class=>'#{ancestor.class_name}' :id=>'#{descendant.attribute_value('id')}'") if dbg if elements.include?(ancestor.tag_name.downcase.to_sym) if eval(code) found = true break end end ancestor = ancestor.parent end [ancestor, ancestor.tag_name.downcase] rescue failed_to_log(unable_to) end |
#get_ancestor4(descendant, args = {}) ⇒ Object
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 |
# File 'lib/awetestlib/regression/awetest_dsl.rb', line 648 def get_ancestor4(descendant, args = {}) desc = args[:desc] if args[:desc] errors = Array.new [:elements, :hows, :whats].each do |key| if args.keys.include?(key) unless args[key].is_a?(Array) args[key] = [args[key]] end eval("#{key.to_s} = args[#{key}]") else errors << "Parameter '#{key}' is required. " end end if hows and whats unless hows.length == whats.length errors << "Parameters 'hows' and 'whats' must be the same length. " end end if errors.length > 0 failed_to_log("#{method_to_title(__method__)}: #{nice_array(errors)} #{desc}") else found = false ancestor = descendant.parent code == '' (0..hows.length).each do |idx| hows[idx] = 'class_name' if hows[idx].to_s == 'class' code = "ancestor.#{how}" whats[idx].is_a?(Regexp) ? code << " =~ /#{whats[idx].source}/" : code << " == '#{whats[idx]}'" code << ' and ' if idx < hows.length debug_to_log("#{code}") if dbg end until found do break unless ancestor debug_to_log("#{ancestor.tag_name}: :class=>'#{ancestor.class_name}' :id=>'#{descendant.attribute_value('id')}'") if dbg if elements.include?(ancestor.tag_name.downcase.to_sym) if eval(code) found = true break end end ancestor = ancestor.parent end [ancestor, ancestor.tag_name.downcase] end rescue failed_to_log(unable_to) end |
#get_attribute_value(container, element, how, what, attribute, desc = '', refs = '') ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/awetestlib/regression/find.rb', line 40 def get_attribute_value(browser, element, how, what, attribute, desc = '') #TODO: eliminate case statement by using eval with build_webdriver_fetch msg = ("Value of #{attribute} in #{element} #{how}=>#{what}.", desc) case element when :link value = browser.link(how => what).attribute_value attribute when :button value = browser.(how => what).attribute_value attribute else if browser.element(how => what).responds_to?('attribute_value') value = browser.element(how => what).attribute_value attribute end end value rescue failed_to_log(" Unable to #{msg}: '#{$!}'") end |
#get_directory(path) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/awetestlib/regression/find.rb', line 58 def get_directory(path) if File.directory?(path) debug_to_log("Directory already exists, '#{path}'.") else Dir::mkdir(path) debug_to_log("Directory was created, '#{path}'.") end path end |
#get_div(browser, how, what, desc = '', dbg = false) ⇒ Watir::Div
Return a reference to a div element identified by the contents what of its attribute how. This differs from get_element in that it waits for the element to exist before trying to return it. that uniquely identifies the element.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/awetestlib/regression/find.rb', line 169 def get_div(browser, how, what, desc = '', dbg = false) msg = ("Get :div #{how}=>#{what}.", desc) Watir::Wait.until { browser.div(how, what).exists? } div = browser.div(how, what) debug_to_log(div.inspect) if dbg if div passed_to_log(msg) return div else failed_to_log(msg) end rescue failed_to_log("Unable to '#{msg}' '#{$!}'") end |
#get_element(container, element, how, what, value = nil, desc = '', refs = '', options = {}) ⇒ Object
Return a reference to a DOM element specified by its type element, attribute how, and the contents of that attribute what. Some elements may require use of the :value attribute in addition to the designated one. The target contents for :value are supplied in value.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/awetestlib/regression/find.rb', line 22 def get_element(container, element, how, what, value = nil, desc = '', = {}) value, desc, = capture_value_desc(value, desc, ) # for backwards compatibility msg = ("Return #{element} with :#{how}=>'#{what}'", value, desc) code = build_webdriver_fetch(element, how, what, ) target = eval(code) if target and target.exists? passed_to_log(msg) target else failed_to_log(msg) nil end rescue => e unless rescue_me(e, __method__, rescue_me_command(target, how, what), "#{container.class}", target) raise e end end |
#get_element_attribute(element, attribute, desc = '', refs = '', how = '', what = nil) ⇒ Object
386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/awetestlib/regression/awetest_dsl.rb', line 386 def get_element_attribute(element, attribute, desc = '', refs = '', how = '', what = nil) msg = (desc, "Get '#{attribute}' for #{element.tag_name.upcase}", (":#{how}=>'#{what}'" if what), refs) value = element.attribute_value(attribute) if value passed_to_log(with_caller(msg)) else failed_to_log(with_caller(msg)) end value rescue failed_to_log(unable_to(msg)) end |
#get_element_collection(browser, element, dbg = false) ⇒ Array Also known as: get_objects
Return an array (collection) of all the elements of the type which contained in the browser or container supplied in browser.
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/awetestlib/regression/find.rb', line 243 def get_element_collection(browser, element, dbg = false) cnt = 0 case element when :links, :link list = browser.links sleep(1) when :tables, :table list = browser.tables when :divs, :div list = browser.divs when :buttons, :button list = browser. when :checkboxes, :checkbox list = browser.checkboxes when :radios, :radio list = browser.radios when :selectlists, :select_lists, :selectlist, :select_list list = browser.selectlists when :textfields, :text_fields, :textareas, :text_fields, :textfield, :text_field, :textarea, :text_area list = browser.textfields when :lis, :li list = browser.lis when :uls, :ul list = browser.uls else debug_to_log("Unsupported DOM object '#{which}'") end if dbg list.each do |obj| cnt += 1 debug_to_log("\n==========#{which}:\nindex: #{cnt}\n#{obj}\n#{obj.to_yaml}") end end list end |
#get_element_colors(element, colors = [], desc = '', refs = '') ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/awetestlib/regression/awetest_dsl.rb', line 368 def get_element_colors(element, colors = [], desc = '', refs = '') msg = (desc, "Get colors for #{element.tag_name.upcase}.", refs) colors = [ 'color', 'background-color', 'border-bottom-color', 'border-left-color', 'border-right-color', 'border-top-color', 'border-color' ] unless colors.size > 0 hash = {} colors.each do |color| hash[color] = normalize_color_value(element.style(color)) end hash rescue failed_to_log(unable_to(msg)) end |
#get_form(browser, how, what, desc = '') ⇒ Watir::Form
Return a reference to a form element identified by the contents what of its attribute how.
218 219 220 |
# File 'lib/awetestlib/regression/find.rb', line 218 def get_form(browser, how, what, desc = '') get_element(browser, :form, how, what, desc) end |
#get_frame(browser, how, what, desc = '') ⇒ Watir::Frame
Return a reference to a frame element identified by the contents what of its attribute how.
225 226 227 |
# File 'lib/awetestlib/regression/find.rb', line 225 def get_frame(browser, how, what, desc = '') get_element(browser, :frame, how, what, desc) end |
#get_ole(element) ⇒ Object
Detect $watir_script variable and disable if not set to true
Usable only with classic Watir.
Return the ole object for the specified element.
285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/awetestlib/regression/find.rb', line 285 def get_ole(element) ole = element.ole_object if ole passed_to_log("Found ole_object for #{element}.") ole else failed_to_log("Did not find ole_object for #{element}.") end rescue failed_to_log("Unable to find ole_object for #{element}. #{$!}") end |
#get_select_list(browser, how, what, desc = '') ⇒ Watir::SelectList
Return a reference to a select_list element identified by the contents what of its attribute how.
232 233 234 |
# File 'lib/awetestlib/regression/find.rb', line 232 def get_select_list(browser, how, what, desc = '') get_element(browser, :select_list, how, what, desc) end |
#get_select_options(browser, how, what, dump = false) ⇒ Array
Return an array containing the options available for selection in a select_list identifified by its attribute how, and the contents of that attribute what. that uniquely identifies the element. See Utilities#dump_select_list_options
130 131 132 133 134 135 136 |
# File 'lib/awetestlib/regression/find.rb', line 130 def (browser, how, what, dump = false) list = browser.select_list(how, what) (list) if dump list. rescue failed_to_log("Unable to get select options for #{how}=>#{what}. '#{$!}'") end |
#get_selected_options(browser, how, what, desc = '', refs = '') ⇒ Array
Return an array containing the selected options in a select_list identified by its attribute how, and the contents of that attribute what. that uniquely identifies the element.
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/awetestlib/regression/find.rb', line 147 def (browser, how, what) begin list = browser.select_list(how, what) rescue => e unless rescue_me(e, __method__, rescue_me_command(:select_list, how, what), "#{browser.class}") raise e end end list. if list end |
#get_span(browser, how, what, desc = '') ⇒ Watir::Span
Return a reference to a span element identified by the contents what of its attribute how. This differs from get_element in that it waits for the element to exist before trying to return it. that uniquely identifies the element.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/awetestlib/regression/find.rb', line 194 def get_span(browser, how, what, desc = '') begin Watir::Wait.until { browser.span(how, what).exists? } rescue => e unless rescue_me(e, __method__, rescue_me_command(:span, how, what, :exists?), "#{browser.class}") raise e end end begin span = browser.span(how, what) rescue => e unless rescue_me(e, __method__, rescue_me_command(:span, how, what, :exists?), "#{browser.class}") raise e end end passed_to_log("Span #{how}='#{what}' found and returned. #{desc}") return span rescue failed_to_log("Unable to return span #{how}='#{what}'. #{desc}: '#{$!}' (#{__LINE__})") end |
#get_style(container, element, how, what, style, desc = '', refs = '') ⇒ Object Also known as: get_style_value
425 426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/awetestlib/regression/awetest_dsl.rb', line 425 def get_style(container, element, how, what, style, desc = '', refs = '') msg = (desc, "Get '#{style}' in :#{element.to_s.upcase} :#{how}='#{what}'.") code = build_webdriver_fetch(element, how, what) value = eval("#{code}.style('#{style}')") if value passed_to_log(with_caller(msg, "value=>'#{value}'", desc, refs)) value else failed_to_log(with_caller(msg, desc, refs)) end rescue failed_to_log(unable_to(msg)) end |
#highlight_element(element) ⇒ Object
719 720 721 722 723 724 725 726 727 728 729 730 |
# File 'lib/awetestlib/regression/awetest_dsl.rb', line 719 def highlight_element(element) # not really different than .flash unless the two js scripts are separated by a sleep. # probably needs to make sure that original color and border can be restored. #public void highlightElement(WebDriver driver, WebElement element) # { for (int i = 0; i < 2; i++) # { JavascriptExecutor js = (JavascriptExecutor) driver; # js.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: yellow; border: 2px solid yellow;"); # js.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, ""); # } # } # - See more at: http://selenium.polteq.com/en/highlight-elements-with-selenium-webdriver/#sthash.JShjPbsj.dpuf end |
#identify_active_element(container, desc = '', parent = false) ⇒ Object
706 707 708 709 710 711 712 713 714 715 716 717 |
# File 'lib/awetestlib/regression/awetest_dsl.rb', line 706 def identify_active_element(container, desc = '', parent = false) element = get_active_element(container) element = element.parent if parent tag = element.respond_to?(:tag_name) ? element.tag_name : 'no tag name' id = element.respond_to?(:id) ? element.id : 'no id' cls = element.respond_to?(:class_name) ? element.class_name : 'no class' text = element.respond_to?(:text) ? element.text : 'no text' debug_to_report(with_caller(desc, ":tag_name=>#{tag}", ":id=>#{id}", ":text=>'#{text}'", "is parent?=>#{parent}")) [element, tag.to_sym, :id, cls] rescue failed_to_log(unable_to) end |