Class: Watir::Link

Inherits:
Element show all
Defined in:
lib/watir/link.rb

Overview

This class is the means of accessing a link on a page Normally a user would not need to create this object as it is returned by the Watir::Container#link method many of the methods available to this object are inherited from the Element class

Constant Summary collapse

TAG =
"A"

Constants inherited from Element

Element::TO_S_SIZE

Instance Attribute Summary

Attributes inherited from Element

#container

Attributes included from Container

#activeObjectHighLightColor, #page_container, #type_keys, #typingspeed

Instance Method Summary collapse

Methods inherited from Element

#<=>, #__ole_inner_elements, #activeObjectHighLightColor, #after_text, #assert_enabled, #assert_exists, #attribute_value, #before_text, #click, #click!, #create_event, #dispatch_event, #document, #enabled?, #exists?, #fire_event, #flash, #focus, inherited, #inspect, #locate, #method_missing, #name, #ole_object, #ole_object=, #parent, #text, #type_keys, #typingspeed, #visible?

Methods included from Container

#__ole_inner_elements, #input_element_locator, #locator_for, #log, #set_container, #show_all_objects, #tagged_element_locator, #wait

Constructor Details

#initialize(container, how, what) ⇒ Link

Returns an initialized instance of a link object

* container  - an instance of a container
* how         - symbol - how we access the link
* what         - what we use to access the link, text, url, index etc


14
15
16
17
18
19
# File 'lib/watir/link.rb', line 14

def initialize(container, how, what)
  set_container container
  @how = how
  @what = what
  super(nil)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Watir::Element

Instance Method Details

if an image is used as part of the link, this will return true



22
23
24
25
26
# File 'lib/watir/link.rb', line 22

def link_has_image
  assert_exists
  return true if @o.getElementsByTagName("IMG").length > 0
  return false
end


38
39
40
41
42
43
44
# File 'lib/watir/link.rb', line 38

def link_string_creator
  n = []
  n <<   "href:".ljust(TO_S_SIZE) + self.href
  n <<   "inner text:".ljust(TO_S_SIZE) + self.text
  n <<   "img src:".ljust(TO_S_SIZE) + self.src if self.link_has_image
  return n
end

#srcObject

this method returns the src of an image, if an image is used as part of the link



29
30
31
32
33
34
35
36
# File 'lib/watir/link.rb', line 29

def src # BUG?
  assert_exists
  if @o.getElementsByTagName("IMG").length > 0
    return @o.getElementsByTagName("IMG")[0.to_s].src
  else
    return ""
  end
end

#to_sObject

returns a textual description of the link



47
48
49
50
51
52
# File 'lib/watir/link.rb', line 47

def to_s
  assert_exists
  r = string_creator
  r = r + link_string_creator
  return r.join("\n")
end