bret
|
1286
|
1
|
module Watir
|
|
2
|
|
|
3
|
# This class is the means of accessing an image on a page.
|
|
4
|
# Normally a user would not need to create this object as it is returned by the Watir::Container#image method
|
|
5
|
#
|
|
6
|
# many of the methods available to this object are inherited from the Element class
|
|
7
|
#
|
|
8
|
class Image < Element
|
|
9
|
def initialize(container, how, what)
|
|
10
|
set_container container
|
|
11
|
@how = how
|
|
12
|
@what = what
|
|
13
|
super nil
|
|
14
|
end
|
|
15
|
|
|
16
|
def locate
|
|
17
|
if @how == :xpath
|
|
18
|
@o = @container.element_by_xpath(@what)
|
|
19
|
else
|
|
20
|
@o = @container.locate_tagged_element('IMG', @how, @what)
|
|
21
|
end
|
|
22
|
end
|
|
23
|
|
|
24
|
# this method produces the properties for an image as an array
|
|
25
|
def image_string_creator
|
|
26
|
n = []
|
|
27
|
n << "src:".ljust(TO_S_SIZE) + self.src.to_s
|
|
28
|
n << "file date:".ljust(TO_S_SIZE) + self.fileCreatedDate.to_s
|
|
29
|
n << "file size:".ljust(TO_S_SIZE) + self.fileSize.to_s
|
|
30
|
n << "width:".ljust(TO_S_SIZE) + self.width.to_s
|
|
31
|
n << "height:".ljust(TO_S_SIZE) + self.height.to_s
|
|
32
|
n << "alt:".ljust(TO_S_SIZE) + self.alt.to_s
|
|
33
|
return n
|
|
34
|
end
|
|
35
|
private :image_string_creator
|
|
36
|
|
|
37
|
# returns a string representation of the object
|
|
38
|
def to_s
|
|
39
|
assert_exists
|
|
40
|
r = string_creator
|
|
41
|
r += image_string_creator
|
|
42
|
return r.join("\n")
|
|
43
|
end
|
|
44
|
|
|
45
|
# this method returns the file created date of the image
|
jarib
|
1662
|
46
|
def file_created_date
|
bret
|
1286
|
47
|
assert_exists
|
|
48
|
return @o.invoke("fileCreatedDate")
|
|
49
|
end
|
|
50
|
|
|
51
|
# this method returns the filesize of the image
|
jarib
|
1662
|
52
|
def file_size
|
bret
|
1286
|
53
|
assert_exists
|
|
54
|
return @o.invoke("fileSize").to_s
|
|
55
|
end
|
|
56
|
|
|
57
|
# returns the width in pixels of the image, as a string
|
|
58
|
def width
|
|
59
|
assert_exists
|
|
60
|
return @o.invoke("width").to_s
|
|
61
|
end
|
|
62
|
|
|
63
|
# returns the height in pixels of the image, as a string
|
|
64
|
def height
|
|
65
|
assert_exists
|
|
66
|
return @o.invoke("height").to_s
|
|
67
|
end
|
|
68
|
|
|
69
|
# This method attempts to find out if the image was actually loaded by the web browser.
|
|
70
|
# If the image was not loaded, the browser is unable to determine some of the properties.
|
|
71
|
# We look for these missing properties to see if the image is really there or not.
|
|
72
|
# If the Disk cache is full (tools menu -> Internet options -> Temporary Internet Files), it may produce incorrect responses.
|
jarib
|
1662
|
73
|
def loaded?
|
bret
|
1286
|
74
|
locate
|
|
75
|
raise UnknownObjectException, "Unable to locate image using #{@how} and #{@what}" if @o == nil
|
|
76
|
return false if @o.fileCreatedDate == "" and @o.fileSize.to_i == -1
|
|
77
|
return true
|
|
78
|
end
|
|
79
|
|
|
80
|
# this method highlights the image (in fact it adds or removes a border around the image)
|
|
81
|
# * set_or_clear - symbol - :set to set the border, :clear to remove it
|
|
82
|
def highlight(set_or_clear)
|
|
83
|
if set_or_clear == :set
|
|
84
|
begin
|
|
85
|
@original_border = @o.border
|
|
86
|
@o.border = 1
|
|
87
|
rescue
|
|
88
|
@original_border = nil
|
|
89
|
end
|
|
90
|
else
|
|
91
|
begin
|
|
92
|
@o.border = @original_border
|
|
93
|
@original_border = nil
|
|
94
|
rescue
|
|
95
|
# we could be here for a number of reasons...
|
|
96
|
ensure
|
|
97
|
@original_border = nil
|
|
98
|
end
|
|
99
|
end
|
|
100
|
end
|
|
101
|
private :highlight
|
|
102
|
|
|
103
|
# This method saves the image to the file path that is given. The
|
|
104
|
# path must be in windows format (c:\\dirname\\somename.gif). This method
|
|
105
|
# will not overwrite a previously existing image. If an image already
|
|
106
|
# exists at the given path then a dialog will be displayed prompting
|
|
107
|
# for overwrite.
|
|
108
|
# Raises a WatirException if AutoIt is not correctly installed
|
|
109
|
# path - directory path and file name of where image should be saved
|
|
110
|
def save(path)
|
|
111
|
require 'watir/windowhelper'
|
|
112
|
WindowHelper.check_autoit_installed
|
|
113
|
@container.goto(src)
|
|
114
|
begin
|
|
115
|
thrd = fill_save_image_dialog(path)
|
|
116
|
@container.document.execCommand("SaveAs")
|
|
117
|
thrd.join(5)
|
|
118
|
ensure
|
|
119
|
@container.back
|
|
120
|
end
|
|
121
|
end
|
|
122
|
|
|
123
|
def fill_save_image_dialog(path)
|
|
124
|
Thread.new do
|
|
125
|
system("ruby -e \"require 'win32ole'; @autoit=WIN32OLE.new('AutoItX3.Control'); waitresult=@autoit.WinWait 'Save Picture', '', 15; if waitresult == 1\" -e \"@autoit.ControlSetText 'Save Picture', '', '1148', '#{path}'; @autoit.ControlSend 'Save Picture', '', '1', '{ENTER}';\" -e \"end\"")
|
|
126
|
end
|
|
127
|
end
|
|
128
|
private :fill_save_image_dialog
|
|
129
|
end
|
|
130
|
|
|
131
|
end
|
| | | |
\ No newline at end of file |