Module: MobyBehaviour::QT::ScreenCapture

Includes:
Behaviour
Defined in:
lib/testability-driver-plugins/testability-driver-qt-sut-plugin/behaviours/screen_capture.rb

Overview

description

ScreenCapture specific behaviours

behaviour

QtScreenCapture

requires

testability-driver-qt-sut-plugin

input_type

*

sut_type

QT

sut_version

*

objects

*

Instance Method Summary collapse

Methods included from Behaviour

#command_params

Instance Method Details

#capture_screen(format = "PNG", file_name = nil, draw = false) ⇒ Object

description: If the text is not a string.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/testability-driver-plugins/testability-driver-qt-sut-plugin/behaviours/screen_capture.rb', line 84

def capture_screen( format = "PNG", file_name = nil, draw = false )

	ret = nil

	begin

		# convert string representation of draw value to boolean
		draw = ( draw.downcase == 'true' ? true : false ) if draw.kind_of?( String )

		# verify that image format is type of string
		raise ArgumentError.new( "Unexpected argument type (%s) for image format, expected %s" % [ format.class, "String" ] ) unless format.kind_of?( String )

		# verify that filename is type of string
		raise ArgumentError.new( "Unexpected argument type (%s) for filename, expected %s" % [ file_name.class, "String" ] ) unless file_name.nil? || file_name.kind_of?( String )

		# verify that draw flag is type of boolean
		raise ArgumentError.new( "Unexpected argument type (%s) for draw flag, expected %s" % [ draw.class, "boolean (TrueClass or FalseClass)" ] ) unless [ TrueClass, FalseClass ].include?( draw.class )

		command = command_params #in qt_behaviour
		command.command_name( 'Screenshot' )
		command.command_params( 'format'=>format, 'draw' => draw.to_s )
		command.set_require_response( true )
		command.transitions_off
		command.service( 'screenShot' )

		image_binary = @sut.execute_command( command )

		File.open(file_name, 'wb:binary'){ | image_file | image_file << image_binary } if ( file_name )

	rescue Exception => e

		#$logger.behaviour "FAIL;Failed capture_screen with format \"#{format}\", file_name \"#{file_name}\".;#{ identity };capture_screen;"

		$logger.behaviour "FAIL;Failed capture_screen with format \"%s\", file_name \"%s\".;%s;capture_screen;" % [ format, file_name, identity ]

		raise e

	end

	#$logger.behaviour "PASS;Operation capture_screen executed successfully with format \"#{format}\", file_name \"#{file_name}\".;#{ identity };capture_screen;"
	$logger.behaviour "PASS;Operation capture_screen executed successfully with format \"%s\", file_name \"%s\".;%s;capture_screen;" % [ format, file_name, identity ]

	image_binary

end

#find_on_screen(image_or_path, tolerance = 0) ⇒ Object

description: image_or_path was not of one of the allowed image types or a non empty String, or tolerance was not an Integer in the [0,100] range. RuntimeError

description: No image could be loaded from the path given in image_or_path


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/testability-driver-plugins/testability-driver-qt-sut-plugin/behaviours/screen_capture.rb', line 165

def find_on_screen( image_or_path, tolerance = 0 )

# RuntimeError:: No image could be loaded from the path given in image_or_path
	begin

		require 'rmagick'

		raise ArgumentError.new("The tolerance argument was not an Integer in the [0,100] range.") unless tolerance.kind_of? Integer and tolerance >= 0 and tolerance <= 100

		target = nil

		if image_or_path.kind_of? Magick::Image

			target = image_or_path

		elsif image_or_path.kind_of? Magick::ImageList

			raise ArgumentError.new("The supplied ImageList argument did not contain any images.") unless image_or_path.length > 0
			target = image_or_path

		elsif image_or_path.kind_of? String and !image_or_path.empty?

			begin
				target = Magick::ImageList.new(image_or_path)
			rescue        
				raise RuntimeError.new("Could not load target for image comparison from path: \"#{image_or_path.to_s}\".")
			end

		else
			raise ArgumentError.new("The image_or_path argument was not of one of the allowed image types or a non empty String.")
		end

		begin      
			screen = Magick::Image.from_blob(capture_screen){ self.format = "PNG" }.first
			screen.fuzz = tolerance.to_s + "%"
		rescue
			raise RuntimeError.new("Failed to capture SUT screen for comparison. Details:\n" << $!.message)  
		end

		result = screen.find_similar_region( target )

	rescue Exception => e

		$logger.behaviour "FAIL;Failed when searching for image on the screen.;#{ identity };find_on_screen;#{(image_or_path.respond_to?(:filename) ? image_or_path.filename : image_or_path.to_s)},#{tolerance.to_s}"  

		raise e

	end

	$logger.behaviour "PASS;Image search completed successfully.;#{ identity };find_on_screen;#{(image_or_path.respond_to?(:filename) ? image_or_path.filename : image_or_path.to_s)},#{tolerance.to_s}"

	result

end

#screen_contains?(image_or_path, tolerance = 0) ⇒ Boolean

description: No image could be loaded from the path given in image_or_path

Returns:

  • (Boolean)


252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/testability-driver-plugins/testability-driver-qt-sut-plugin/behaviours/screen_capture.rb', line 252

def screen_contains?( image_or_path, tolerance = 0 )

	begin
		# find_on_screen returns nil if the image is not found on the device screen
		result = !find_on_screen(image_or_path, tolerance).nil?
	rescue Exception => exc

		$logger.behaviour	"FAIL;Failed when searching for image on the screen.;#{ identity };screen_contains?;#{(image_or_path.respond_to?(:filename) ? image_or_path.filename : image_or_path.to_s)},#{tolerance.to_s}"

		raise exc


	end      

	$logger.behaviour "PASS;Image search completed successfully.;#{ identity };screen_contains?;#{(image_or_path.respond_to?(:filename) ? image_or_path.filename : image_or_path.to_s)},#{tolerance.to_s}"

	result

end