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

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

#<=>, #activeObjectHighLightColor, #after_text, #assert_enabled, #assert_exists, #attribute_value, #before_text, #click, #click!, #click_no_wait, #document, #enabled?, #exists?, #fire_event, #flash, #focus, #inspect, #ole_object, #ole_object=, #parent, #text, #type_keys, #typingspeed, #visible?

Methods included from Container

#area, #areas, #button, #buttons, #cell, #cells, #checkbox, #checkboxes, #dds, #divs, #dls, #dts, #element, #elements, #ems, #file_field, #file_fields, #form, #forms, #frame, #hidden, #hiddens, #image, #images, #labels, #link, #links, #lis, #locate_all_elements, #locate_input_element, #locate_tagged_element, #log, #map, #maps, #modal_dialog, #popup, #pres, #ps, #radio, #radios, #row, #rows, #select_list, #select_lists, #set_container, #show_all_objects, #spans, #strongs, #table, #tables, #text_field, #text_fields, #wait

Constructor Details

#initialize(container, how, what) ⇒ Image

Returns a new instance of Image



9
10
11
12
13
14
# File 'lib/watir/image.rb', line 9

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

Instance Method Details

#file_created_dateObject Also known as: fileCreatedDate

this method returns the file created date of the image



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

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



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

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

#heightObject

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



64
65
66
67
# File 'lib/watir/image.rb', line 64

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)


73
74
75
76
77
78
# File 'lib/watir/image.rb', line 73

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

#locateObject



16
17
18
19
20
21
22
# File 'lib/watir/image.rb', line 16

def locate
  if @how == :xpath
    @o = @container.element_by_xpath(@what)
  else
    @o = @container.locate_tagged_element('IMG', @how, @what)
  end
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. Raises a WatirException if AutoIt is not correctly installed path - directory path and file name of where image should be saved



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/watir/image.rb', line 110

def save(path)
  require 'watir/windowhelper'
  WindowHelper.check_autoit_installed
  @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



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

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



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

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