Class: Watir::Image

Inherits:
Element show all
Defined in:
lib/watir/image.rb,
lib/watir/camel_case.rb

Overview

This class is the means of accessing an image on a page. Normally a user would not need to create this object as it is returned by the Watir::Container#image method

many of the methods available to this object are inherited from the Element class

Constant Summary collapse

TAG =
"IMG"

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) ⇒ Image

Returns a new instance of Image.



11
12
13
14
15
16
# File 'lib/watir/image.rb', line 11

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

#file_created_dateObject Also known as: fileCreatedDate

this method returns the file created date of the image



40
41
42
43
# File 'lib/watir/image.rb', line 40

def file_created_date
  assert_exists
  return @o.invoke("fileCreatedDate")
end

#file_sizeObject Also known as: fileSize

this method returns the filesize of the image



46
47
48
49
# File 'lib/watir/image.rb', line 46

def file_size
  assert_exists
  return @o.invoke("fileSize").to_s
end

#heightObject

returns the height in pixels of the image, as a string



58
59
60
61
# File 'lib/watir/image.rb', line 58

def height
  assert_exists
  return @o.invoke("height").to_s
end

#loaded?Boolean Also known as: hasLoaded?

This method attempts to find out if the image was actually loaded by the web browser. If the image was not loaded, the browser is unable to determine some of the properties. We look for these missing properties to see if the image is really there or not. If the Disk cache is full (tools menu -> Internet options -> Temporary Internet Files), it may produce incorrect responses.

Returns:

  • (Boolean)

Raises:

  • (UnknownObjectException)


67
68
69
70
71
72
# File 'lib/watir/image.rb', line 67

def loaded?
  locate
  raise UnknownObjectException, "Unable to locate image using #{@how} and #{@what}" if @o == nil
  return false if @o.fileCreatedDate == "" and @o.fileSize.to_i == -1
  return true
end

#save(path) ⇒ Object

This method saves the image to the file path that is given. The path must be in windows format (c:\dirname\somename.gif). This method will not overwrite a previously existing image. If an image already exists at the given path then a dialog will be displayed prompting for overwrite. path - directory path and file name of where image should be saved



103
104
105
106
107
108
109
110
111
112
# File 'lib/watir/image.rb', line 103

def save(path)
  @container.goto(src)
  begin
    thrd = fill_save_image_dialog(path)
    @container.document.execCommand("SaveAs")
    thrd.join(5)
  ensure
    @container.back
  end
end

#to_sObject

returns a string representation of the object



32
33
34
35
36
37
# File 'lib/watir/image.rb', line 32

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

#widthObject

returns the width in pixels of the image, as a string



52
53
54
55
# File 'lib/watir/image.rb', line 52

def width
  assert_exists
  return @o.invoke("width").to_s
end