This class is a stub representation of various elements on the browser Most of the methods are implemented via method missing.
All APIs available in Sahi are available in ruby. The full list is available here: sahipro.com/docs/sahi-apis/index.html#Browser Accessor APIs
Most commonly used action methods are: click - for all elements mouse_over - for all elements focus - for all elements remove_focus - for all elements check - for checkboxes or radio buttons uncheck - for checkboxes
# File sahi.rb, line 424 def initialize (browser, type, identifiers) @type = type @browser = browser @identifiers = identifiers end
# File sahi.rb, line 437 def _perform(type) step = "_sahi._#{type}(#{self.to_s()})" @browser.execute_step(step) end
# File sahi.rb, line 549 def above(el2, offset=nil, limit_under=nil) ar = (offset!=nil || limit_under!=nil) ? [el2, offset, limit_under] : [el2]; @identifiers << ElementStub.new(@browser, "above", ar) return self end
# File sahi.rb, line 555 def above_or_under(el2, offset=nil, limit_under=nil) ar = (offset!=nil || limit_under!=nil) ? [el2, offset, limit_under] : [el2]; @identifiers << ElementStub.new(@browser, "aboveOrUnder", ar) return self end
returns checked state of checkbox or radio button
# File sahi.rb, line 513 def checked?() return fetch("checked") == "true"; end
choose option in a select box
# File sahi.rb, line 452 def choose(val) @browser.execute_step("_sahi._setSelected(#{self.to_s()}, #{val.to_json})") end
returns array elements similar to this element
# File sahi.rb, line 635 def collect_similar() count = self.count_similar() els = Array.new(count) for i in (0..count-1) copy = Array.new(@identifiers) copy[0] = "#{copy[0]}[#{i}]" els[i] = ElementStub.new(@browser, @type, copy); end return els end
# File sahi.rb, line 658 def concat_identifiers(ids) return ids.collect {|id| id.kind_of?(String) ? Utils.quoted(id) : (id.is_a?(ElementStub) ? id.to_s() : id.to_json())} end
returns true if the element contains this html
# File sahi.rb, line 625 def contains_html?(html) return @browser.fetch("_sahi._containsHTML(#{self.to_s()}, #{Utils.quoted(html)})") end
returns true if the element contains this text
# File sahi.rb, line 620 def contains_text?(text) return @browser.fetch("_sahi._containsText(#{self.to_s()}, #{Utils.quoted(text)})") end
returns count of elements similar to this element
# File sahi.rb, line 630 def count_similar() return Integer(@browser.fetch("_sahi._count(\"_#{@type}\", #{concat_identifiers(@identifiers).join(", ")})")) end
drag element and drop on another element
# File sahi.rb, line 447 def drag_and_drop_on(el2) @browser.execute_step("_sahi._dragDrop(#{self.to_s()}, #{el2.to_s()})") end
# File sahi.rb, line 602 def exists1? return "true".eql?(@browser.fetch("_sahi._exists(#{self.to_s()})")) end
returns true if the element exists on the browser
# File sahi.rb, line 594 def exists?(optimistic = false) return self.exists1?() if optimistic; (1..5).each do return true if self.exists1?(); end return false; end
fetches value of specified attribute
# File sahi.rb, line 485 def fetch(attr=nil) if attr if attr.include? "." return @browser.fetch("#{self.to_s()}.#{attr}") else return @browser.fetch("_sahi.getAttribute(#{self.to_s()}, #{Utils.quoted(attr)})") end else return @browser.fetch("#{self.to_s()}") end end
returns boolean value of attribute. returns true only if fetch returns “true”
# File sahi.rb, line 498 def fetch_boolean(attr=nil) return @browser.fetch_boolean(attr) end
Emulates setting filepath in a file input box.
# File sahi.rb, line 503 def file=(val) @browser.execute_step("_sahi._setFile(#{self.to_s()}, #{Utils.quoted(val)})") end
returns a stub with a DOM “in” relation to another element Eg.
browser.image("plus.gif").in(browser.div("Tree Node 2")) will denote the plus icon inside a tree node with text "Tree Node 2"
# File sahi.rb, line 533 def in(el2) @identifiers << ElementStub.new(@browser, "in", [el2]) return self end
# File sahi.rb, line 442 def key_press(codes, combo=nil) @browser.execute_step("_sahi._keyPress(#{self.to_s()}, #{codes.to_json}, #{combo.to_json})") end
# File sahi.rb, line 567 def left_of(el2, offset=nil, limit_under=nil) ar = (offset!=nil || limit_under!=nil) ? [el2, offset, limit_under] : [el2]; @identifiers << ElementStub.new(@browser, "leftOf", ar) return self end
# File sahi.rb, line 573 def left_or_right_of(el2, offset=nil, limit_under=nil) ar = (offset!=nil || limit_under!=nil) ? [el2, offset, limit_under] : [el2]; @identifiers << ElementStub.new(@browser, "leftOrRightOf", ar) return self end
# File sahi.rb, line 430 def method_missing(m, *args, &block) key = m.to_s if @@actions.key?(key) _perform(@@actions[key]) end end
returns a stub with a DOM “near” relation to another element Eg.
browser.button("delete").near(browser.cell("User One")) will denote the delete button near the table cell with text "User One"
# File sahi.rb, line 525 def near(el2) @identifiers << ElementStub.new(@browser, "near", [el2]) return self end
denotes the DOM parentNode of element. If tag_name is specified, returns the parent element which matches the tag_name occurrence finds the nth parent of a particular tag_name eg. browser.cell(“inner nested cell”).parent_node(“TABLE”, 3) will return the 3rd encapsulating table of the given cell.
# File sahi.rb, line 589 def parent_node(tag_name="ANY", occurrence=1) return ElementStub.new(@browser, "parentNode", [self]); end
# File sahi.rb, line 561 def right_of(el2, offset=nil, limit_under=nil) ar = (offset!=nil || limit_under!=nil) ? [el2, offset, limit_under] : [el2]; @identifiers << ElementStub.new(@browser, "rightOf", ar) return self end
select text for manipulation
# File sahi.rb, line 463 def select_range(rangeStart, rangeEnd, type=nil) if(type!= nil) @browser.execute_step("_sahi._selectRange(#{self.to_s()}, rangeStart, rangeEnd, #{Utils.quoted(type)})") else @browser.execute_step("_sahi._selectRange(#{self.to_s()}, #{rangeStart}, #{rangeEnd})") end end
# File sahi.rb, line 471 def select_text_range(searchText, position=nil) if(position != nil) @browser.execute_step("_sahi._selectTextRange(#{self.to_s()}, #{Utils.quoted(searchText)}, #{Utils.quoted(position)})") else @browser.execute_step("_sahi._selectTextRange(#{self.to_s()}, #{Utils.quoted(searchText)})") end end
returns selected text from select box
# File sahi.rb, line 518 def selected_text() return @browser.fetch("_sahi._getSelectedText(#{self.to_s()})") end
returns inner text of any element
# File sahi.rb, line 508 def text() return @browser.fetch("_sahi._getText(#{self.to_s()})") end
# File sahi.rb, line 654 def to_identifiers return "#{concat_identifiers(@identifiers).join(", ") }" end
# File sahi.rb, line 646 def to_s return "_sahi._#{@type}(#{concat_identifiers(@identifiers).join(", ") })" end
# File sahi.rb, line 650 def to_type return "_#{@type}" end
returns a stub with a POSITIONAL “under” relation to another element. Eg.
browser.cell(0).under(browser.cell("Header")) will denote the cell visually under "Header" browser.cell(0).near(browser.cell("Book")).under(browser.cell("Cost")) may be used to denote the Cost of Book in a grid
# File sahi.rb, line 543 def under(el2, offset=nil, limit_under=nil) ar = (offset!=nil || limit_under!=nil) ? [el2, offset, limit_under] : [el2]; @identifiers << ElementStub.new(@browser, "under", ar) return self end
returns value of textbox or textareas and other relevant input elements
# File sahi.rb, line 480 def value() return @browser.fetch("_sahi._getValue(#{self.to_s()})") end
sets the value for textboxes or textareas. Also triggers necessary events.
# File sahi.rb, line 457 def value=(val) @browser.execute_step("_sahi._setValue(#{self.to_s()}, #{Utils.quoted(val)})") end
# File sahi.rb, line 615 def visible1? return "true".eql?(@browser.fetch("_sahi._isVisible(#{self.to_s()})")) end
returns true if the element exists and is visible on the browser
# File sahi.rb, line 607 def visible?(optimistic = false) return self.visible1?() if optimistic; (1..5).each do return true if self.visible1?(); end return false; end
specifies exacts coordinates to click inside an element. The coordinates are relative to the element. x is from left and y is from top. Can be negative to specify other direction browser.button(“Menu Button with Arrow on side”).xy(-5, 10).click will click on the button, 5 pixels from right and 10 pixels from top.
# File sahi.rb, line 581 def xy(x, y) return ElementStub.new(@browser, "xy", [self, x, y]) end