Class: Sahi::ElementStub

Inherits:
Object
  • Object
show all
Defined in:
lib/sahi.rb

Overview

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: sahi.co.in/w/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

Constant Summary collapse

@@actions =
{"click"=>"click",
    "focus"=>"focus", "remove_focus"=>"removeFocus", 
    "check"=>"check", "uncheck"=>"uncheck", 
"dblclick"=>"doubleClick", "right_click"=>"rightClick",
"key_down"=>"keyDown", "key_up"=>"keyUp",
"mouse_over"=>"mouseOver", "mouse_down"=>"mouseDown", "mouse_up"=>"mouseUp"}

Instance Method Summary collapse

Constructor Details

#initialize(browser, type, identifiers) ⇒ ElementStub

Returns a new instance of ElementStub.



496
497
498
499
500
# File 'lib/sahi.rb', line 496

def initialize (browser, type,  identifiers)
  @type = type
  @browser  = browser      
  @identifiers = identifiers
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



502
503
504
505
506
507
# File 'lib/sahi.rb', line 502

def method_missing(m, *args, &block)  
  key = m.to_s
  if @@actions.key?(key)
    _perform(@@actions[key])
  end      
end

Instance Method Details

#_perform(type) ⇒ Object



509
510
511
512
# File 'lib/sahi.rb', line 509

def _perform(type)
  step = "_sahi._#{type}(#{self.to_s()})"
  @browser.execute_step(step)
end

#above(el2, offset = nil, limit_under = nil) ⇒ Object



621
622
623
624
625
# File 'lib/sahi.rb', line 621

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

#above_or_under(el2, offset = nil, limit_under = nil) ⇒ Object



627
628
629
630
631
# File 'lib/sahi.rb', line 627

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

#checked?Boolean

returns checked state of checkbox or radio button

Returns:

  • (Boolean)


585
586
587
# File 'lib/sahi.rb', line 585

def checked?()
  return fetch("checked") == "true";
end

#choose(val) ⇒ Object

choose option in a select box



524
525
526
# File 'lib/sahi.rb', line 524

def choose(val)
  @browser.execute_step("_sahi._setSelected(#{self.to_s()}, #{val.to_json})")
end

#collect_similarObject

returns array elements similar to this element



707
708
709
710
711
712
713
714
715
716
# File 'lib/sahi.rb', line 707

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

#concat_identifiers(ids) ⇒ Object



730
731
732
# File 'lib/sahi.rb', line 730

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

#contains_html?(html) ⇒ Boolean

returns true if the element contains this html

Returns:

  • (Boolean)


697
698
699
# File 'lib/sahi.rb', line 697

def contains_html?(html)
  return @browser.fetch("_sahi._containsHTML(#{self.to_s()}, #{Utils.quoted(html)})")
end

#contains_text?(text) ⇒ Boolean

returns true if the element contains this text

Returns:

  • (Boolean)


692
693
694
# File 'lib/sahi.rb', line 692

def contains_text?(text)
  return @browser.fetch("_sahi._containsText(#{self.to_s()}, #{Utils.quoted(text)})")
end

#count_similarObject

returns count of elements similar to this element



702
703
704
# File 'lib/sahi.rb', line 702

def count_similar()
	return Integer(@browser.fetch("_sahi._count(\"_#{@type}\", #{concat_identifiers(@identifiers).join(", ")})"))
end

#drag_and_drop_on(el2) ⇒ Object

drag element and drop on another element



519
520
521
# File 'lib/sahi.rb', line 519

def drag_and_drop_on(el2)
  @browser.execute_step("_sahi._dragDrop(#{self.to_s()}, #{el2.to_s()})")
end

#exists1?Boolean

Returns:

  • (Boolean)


674
675
676
# File 'lib/sahi.rb', line 674

def exists1?
  return "true".eql?(@browser.fetch("_sahi._exists(#{self.to_s()})"))
end

#exists?(optimistic = false) ⇒ Boolean

returns true if the element exists on the browser

Returns:

  • (Boolean)


666
667
668
669
670
671
672
# File 'lib/sahi.rb', line 666

def exists?(optimistic = false)
		return self.exists1?() if optimistic;
		(1..5).each do
			return true if self.exists1?();
		end
		return false;
end

#fetch(attr = nil) ⇒ Object

fetches value of specified attribute



557
558
559
560
561
562
563
564
565
566
567
# File 'lib/sahi.rb', line 557

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

#fetch_boolean(attr = nil) ⇒ Object

returns boolean value of attribute. returns true only if fetch returns “true”



570
571
572
# File 'lib/sahi.rb', line 570

def fetch_boolean(attr=nil)
  return @browser.fetch_boolean(attr)
end

#file=(val) ⇒ Object

Emulates setting filepath in a file input box.



575
576
577
# File 'lib/sahi.rb', line 575

def file=(val)
  @browser.execute_step("_sahi._setFile(#{self.to_s()}, #{Utils.quoted(val)})")
end

#in(el2) ⇒ Object

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"


605
606
607
608
# File 'lib/sahi.rb', line 605

def in(el2)
  @identifiers << ElementStub.new(@browser, "in", [el2])
  return self
end

#key_press(codes, combo = nil) ⇒ Object



514
515
516
# File 'lib/sahi.rb', line 514

def key_press(codes, combo=nil)
	@browser.execute_step("_sahi._keyPress(#{self.to_s()}, #{codes.to_json}, #{combo.to_json})")
end

#left_of(el2, offset = nil, limit_under = nil) ⇒ Object



639
640
641
642
643
# File 'lib/sahi.rb', line 639

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

#left_or_right_of(el2, offset = nil, limit_under = nil) ⇒ Object



645
646
647
648
649
# File 'lib/sahi.rb', line 645

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

#near(el2) ⇒ Object

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"


597
598
599
600
# File 'lib/sahi.rb', line 597

def near(el2)
  @identifiers << ElementStub.new(@browser, "near", [el2])
  return self
end

#parent_node(tag_name = "ANY", occurrence = 1) ⇒ Object

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.



661
662
663
# File 'lib/sahi.rb', line 661

def parent_node(tag_name="ANY", occurrence=1) 
  return ElementStub.new(@browser, "parentNode", [self]);
end

#right_of(el2, offset = nil, limit_under = nil) ⇒ Object



633
634
635
636
637
# File 'lib/sahi.rb', line 633

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_range(rangeStart, rangeEnd, type = nil) ⇒ Object

select text for manipulation



535
536
537
538
539
540
541
# File 'lib/sahi.rb', line 535

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

#select_text_range(searchText, position = nil) ⇒ Object



543
544
545
546
547
548
549
# File 'lib/sahi.rb', line 543

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

#selected_textObject

returns selected text from select box



590
591
592
# File 'lib/sahi.rb', line 590

def selected_text()
  return @browser.fetch("_sahi._getSelectedText(#{self.to_s()})")
end

#textObject

returns inner text of any element



580
581
582
# File 'lib/sahi.rb', line 580

def text()
  return @browser.fetch("_sahi._getText(#{self.to_s()})")
end

#to_identifiersObject



726
727
728
# File 'lib/sahi.rb', line 726

def to_identifiers
  return "#{concat_identifiers(@identifiers).join(", ") }"
end

#to_sObject



718
719
720
# File 'lib/sahi.rb', line 718

def to_s
  return "_sahi._#{@type}(#{concat_identifiers(@identifiers).join(", ") })"
end

#to_typeObject



722
723
724
# File 'lib/sahi.rb', line 722

def to_type
  return "_#{@type}"
end

#under(el2, offset = nil, limit_under = nil) ⇒ Object

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


615
616
617
618
619
# File 'lib/sahi.rb', line 615

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

#valueObject

returns value of textbox or textareas and other relevant input elements



552
553
554
# File 'lib/sahi.rb', line 552

def value()
  return @browser.fetch("_sahi._getValue(#{self.to_s()})")
end

#value=(val) ⇒ Object

sets the value for textboxes or textareas. Also triggers necessary events.



529
530
531
# File 'lib/sahi.rb', line 529

def value=(val)
  @browser.execute_step("_sahi._setValue(#{self.to_s()}, #{Utils.quoted(val)})")
end

#visible1?Boolean

Returns:

  • (Boolean)


687
688
689
# File 'lib/sahi.rb', line 687

def visible1?
  return "true".eql?(@browser.fetch("_sahi._isVisible(#{self.to_s()})"))
end

#visible?(optimistic = false) ⇒ Boolean

returns true if the element exists and is visible on the browser

Returns:

  • (Boolean)


679
680
681
682
683
684
685
# File 'lib/sahi.rb', line 679

def visible?(optimistic = false)
		return self.visible1?() if optimistic;
		(1..5).each do
			return true if self.visible1?();
		end
		return false;
end

#xy(x, y) ⇒ Object

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.



653
654
655
# File 'lib/sahi.rb', line 653

def xy(x, y)
  return ElementStub.new(@browser, "xy", [self, x, y])
end