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!, #document, #enabled?, #exists?, #fire_event, #flash, #focus, #inspect, #method_missing, #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, #element_by_css, #elements, #ems, #file_field, #file_fields, #form, #forms, #frame, #frames, #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

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



48
49
50
51
# File 'lib/watir/image.rb', line 48

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



54
55
56
57
# File 'lib/watir/image.rb', line 54

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

#heightObject

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



66
67
68
69
# File 'lib/watir/image.rb', line 66

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)


75
76
77
78
79
80
# File 'lib/watir/image.rb', line 75

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
23
24
# File 'lib/watir/image.rb', line 16

def locate
  if @how == :xpath
    @o = @container.element_by_xpath(@what)
  elsif @how == :css
    @o = @container.element_by_css(@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



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

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



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

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



60
61
62
63
# File 'lib/watir/image.rb', line 60

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