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", "mouse_over"=>"mouseOver", 
"focus"=>"focus", "remove_focus"=>"removeFocus", 
"check"=>"check", "uncheck"=>"uncheck", 
"dblclick"=>"doubleClick", "right_click"=>"rightClick"}

Instance Method Summary collapse

Constructor Details

#initialize(browser, type, identifiers) ⇒ ElementStub

Returns a new instance of ElementStub.



303
304
305
306
307
# File 'lib/sahi.rb', line 303

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



309
310
311
312
313
314
# File 'lib/sahi.rb', line 309

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



316
317
318
319
# File 'lib/sahi.rb', line 316

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

#checked?Boolean

returns checked state of checkbox or radio button

Returns:

  • (Boolean)


357
358
359
# File 'lib/sahi.rb', line 357

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

#choose(val) ⇒ Object

choose option in a select box



327
328
329
# File 'lib/sahi.rb', line 327

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

#concat_identifiers(ids) ⇒ Object



437
438
439
# File 'lib/sahi.rb', line 437

def concat_identifiers(ids)
  return ids.collect {|id| id.kind_of?(String) ? Utils.quoted(id) : id.to_s()}
end

#contains_html?(html) ⇒ Boolean

returns true if the element contains this html

Returns:

  • (Boolean)


429
430
431
# File 'lib/sahi.rb', line 429

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)


424
425
426
# File 'lib/sahi.rb', line 424

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

#drag_and_drop_on(el2) ⇒ Object

drag element and drop on another element



322
323
324
# File 'lib/sahi.rb', line 322

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

#exists1?Boolean

Returns:

  • (Boolean)


406
407
408
# File 'lib/sahi.rb', line 406

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)


398
399
400
401
402
403
404
# File 'lib/sahi.rb', line 398

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



342
343
344
# File 'lib/sahi.rb', line 342

def fetch(attr=nil)
  return attr ? @browser.fetch("#{self.to_s()}.#{attr}") : @browser.fetch("#{self.to_s()}")
end

#file=(val) ⇒ Object

Emulates setting filepath in a file input box.



347
348
349
# File 'lib/sahi.rb', line 347

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"


377
378
379
380
# File 'lib/sahi.rb', line 377

def in(el2)
  @identifiers << ElementStub.new(@browser, "in", [el2])
  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"


369
370
371
372
# File 'lib/sahi.rb', line 369

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.



393
394
395
# File 'lib/sahi.rb', line 393

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

#selected_textObject

returns selected text from select box



362
363
364
# File 'lib/sahi.rb', line 362

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

#textObject

returns inner text of any element



352
353
354
# File 'lib/sahi.rb', line 352

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

#to_sObject



433
434
435
# File 'lib/sahi.rb', line 433

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

#valueObject

returns value of textbox or textareas and other relevant input elements



337
338
339
# File 'lib/sahi.rb', line 337

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

#value=(val) ⇒ Object

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



332
333
334
# File 'lib/sahi.rb', line 332

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

#visible1?Boolean

Returns:

  • (Boolean)


419
420
421
# File 'lib/sahi.rb', line 419

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)


411
412
413
414
415
416
417
# File 'lib/sahi.rb', line 411

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.



385
386
387
# File 'lib/sahi.rb', line 385

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