Class: Applitools::Selenium::Target

Inherits:
Object
  • Object
show all
Includes:
FluentInterface, MatchLevelSetter
Defined in:
lib/applitools/selenium/target.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTarget

Initialize a Applitools::Selenium::Target instance.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/applitools/selenium/target.rb', line 29

def initialize
  self.frames = []
  self.options = {
    ignore_mismatch: false,
    script_hooks: { beforeCaptureScreenshot: '' },
    visual_grid_options: {}
  }
  self.regions = {}
  self.convert_coordinates_block = nil
  reset_for_fullscreen
end

Instance Attribute Details

#accessibility_regionsObject

Returns the value of attribute accessibility_regions.



22
23
24
# File 'lib/applitools/selenium/target.rb', line 22

def accessibility_regions
  @accessibility_regions
end

#content_regionsObject

Returns the value of attribute content_regions.



22
23
24
# File 'lib/applitools/selenium/target.rb', line 22

def content_regions
  @content_regions
end

#convert_coordinates_blockObject

Returns the value of attribute convert_coordinates_block.



22
23
24
# File 'lib/applitools/selenium/target.rb', line 22

def convert_coordinates_block
  @convert_coordinates_block
end

#coordinate_typeObject

Returns the value of attribute coordinate_type.



22
23
24
# File 'lib/applitools/selenium/target.rb', line 22

def coordinate_type
  @coordinate_type
end

#elementObject

Returns the value of attribute element.



22
23
24
# File 'lib/applitools/selenium/target.rb', line 22

def element
  @element
end

#floating_regionsObject

Returns the value of attribute floating_regions.



22
23
24
# File 'lib/applitools/selenium/target.rb', line 22

def floating_regions
  @floating_regions
end

#framesObject

Returns the value of attribute frames.



22
23
24
# File 'lib/applitools/selenium/target.rb', line 22

def frames
  @frames
end

#ignored_regionsObject

Returns the value of attribute ignored_regions.



22
23
24
# File 'lib/applitools/selenium/target.rb', line 22

def ignored_regions
  @ignored_regions
end

#layout_regionsObject

Returns the value of attribute layout_regions.



22
23
24
# File 'lib/applitools/selenium/target.rb', line 22

def layout_regions
  @layout_regions
end

#optionsObject

Returns the value of attribute options.



22
23
24
# File 'lib/applitools/selenium/target.rb', line 22

def options
  @options
end

#region_to_checkObject

Returns the value of attribute region_to_check.



22
23
24
# File 'lib/applitools/selenium/target.rb', line 22

def region_to_check
  @region_to_check
end

#regionsObject

Returns the value of attribute regions.



22
23
24
# File 'lib/applitools/selenium/target.rb', line 22

def regions
  @regions
end

#strict_regionsObject

Returns the value of attribute strict_regions.



22
23
24
# File 'lib/applitools/selenium/target.rb', line 22

def strict_regions
  @strict_regions
end

Class Method Details

.frame(element) ⇒ Object



9
10
11
# File 'lib/applitools/selenium/target.rb', line 9

def frame(element)
  new.frame(element)
end

.region(*args) ⇒ Object



17
18
19
# File 'lib/applitools/selenium/target.rb', line 17

def region(*args)
  new.region(*args)
end

.windowObject



13
14
15
# File 'lib/applitools/selenium/target.rb', line 13

def window
  new
end

Instance Method Details

#accessibility(*args) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/applitools/selenium/target.rb', line 325

def accessibility(*args)
  options = Applitools::Utils.extract_options! args
  unless options[:type]
    raise Applitools::EyesError,
      'You should call Target.accessibility(region, type: type). The :type option is required'
  end
  unless Applitools::AccessibilityRegionType.enum_values.include?(options[:type])
    raise Applitools::EyesIllegalArgument,
      "The region type should be one of [#{Applitools::AccessibilityRegionType.enum_values.join(', ')}]"
  end
  handle_frames
  padding_proc = proc do |region|
    Applitools::AccessibilityRegion.new(
      region, options[:type]
    )
  end

  accessibility_regions << case args.first
                           when ::Selenium::WebDriver::Element
                             proc do |driver, return_element = false|
                               element = applitools_element_from_selenium_element(driver, args.first)
                               next element, padding_proc if return_element
                               padding_proc.call(element)
                             end
                           when Applitools::Selenium::Element
                             proc do |_driver, return_element = false|
                               next args.first, padding_proc if return_element
                               padding_proc.call(args.first)
                             end
                           when Applitools::Region
                             Applitools::AccessibilityRegion.new(
                               args.first, options[:type]
                             )
                           when String
                             proc do |driver, return_element = false|
                               element = driver.find_element(name_or_id: args.first)
                               next element, padding_proc if return_element
                               padding_proc.call(element)
                             end
                           else
                             proc do |driver, return_element = false|
                               elements = driver.find_elements(*args)
                               next elements, padding_proc if return_element
                               elements.map { |e| padding_proc.call(e) }
                             end
                           end
  self
end

#before_render_screenshot_hook(hook) ⇒ Object Also known as: script_hook



311
312
313
314
# File 'lib/applitools/selenium/target.rb', line 311

def before_render_screenshot_hook(hook)
  options[:script_hooks][:beforeCaptureScreenshot] = hook
  self
end

#content(*args) ⇒ Object



177
178
179
180
181
182
# File 'lib/applitools/selenium/target.rb', line 177

def content(*args)
  return match_level(Applitools::MatchLevel::CONTENT) if args.empty?
  region = process_region(*args)
  content_regions << region
  self
end

#convert_coordinates(&block) ⇒ Object



391
392
393
# File 'lib/applitools/selenium/target.rb', line 391

def convert_coordinates(&block)
  self.convert_coordinates_block = block
end

#default_full_page_for_vgObject



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/applitools/selenium/target.rb', line 374

def default_full_page_for_vg
  if options[:stitch_content].nil?
    case region_to_check
    when nil
      fully(true)
    when Proc
      begin
        r = region_to_check.call
        fully(true) if r == Applitools::Region::EMPTY
      rescue StandardError
        fully(false)
      end
    end
  end
  nil
end

#exact(*args) ⇒ Object



191
192
193
# File 'lib/applitools/selenium/target.rb', line 191

def exact(*args)
  match_level(Applitools::MatchLevel::EXACT, *args)
end

#finalizeObject



318
319
320
321
322
323
# File 'lib/applitools/selenium/target.rb', line 318

def finalize
  return self unless frame_or_element
  region = frame_or_element
  self.frame_or_element = nil
  dup.region(region)
end

#floating(region_or_element, bounds, left, top, right, bottom, padding) ⇒ Object

Sets the wanted floating region

Examples:

target.floating(:id, 'my_id', 10, 10, 10, 10)
target.floating(:id, 'my_id', Applitools::FloatingBounds.new(10, 10, 10, 10))
target.floating(region, Applitools::FloatingBounds.new(10, 10, 10, 10))
target.floating(floating_region)
target.floating(floating_region, bounds)
target.floating(:id, 'my_id', Applitools::FloatingBounds.new(10, 10, 10, 10), Applitools::PaddingBounds.new(10, 10, 10, 10))

Parameters:

  • region_or_element (Applitools::FloatingRegion, Selenium::WebDriver::Element, Applitools::Selenium::Element, Applitools::Region)
  • bounds (Applitools::FloatingBounds)
  • left (Integer)
  • top (Integer)
  • right (Integer)
  • bottom (Integer)
  • padding (Applitools::PaddingBounds)


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/applitools/selenium/target.rb', line 122

def floating(*args)
  requested_padding = if args.last.is_a? Applitools::PaddingBounds
                        args.pop
                      else
                        Applitools::PaddingBounds::PIXEL_PADDING
                      end
  value = case args.first
          when Applitools::FloatingRegion
            args.first.padding(requested_padding)
          when ::Applitools::Region
            Applitools::FloatingRegion.any(args.shift, *args).padding(requested_padding)
          when ::Selenium::WebDriver::Element
            proc do |driver, return_element = false|
              args_dup = args.dup
              region = applitools_element_from_selenium_element(driver, args_dup.shift)
              padding_proc = proc do |reg|
                Applitools::FloatingRegion.any(reg, *args_dup).padding(requested_padding)
              end
              next region, padding_proc if return_element
              padding_proc.call(region)
            end
          when ::Applitools::Selenium::Element
            proc do |_driver, return_element = false|
              args_dup = args.dup
              region = args_dup.shift
              padding_proc = proc do |reg|
                Applitools::FloatingRegion.any(reg, *args_dup).padding(requested_padding)
              end
              next region, padding_proc if return_element
              padding_proc.call(region)
            end
          else
            proc do |driver, return_element = false|
              args_dup = args.dup
              region = driver.find_element(args_dup.shift, args_dup.shift)
              padding_proc = proc do |reg|
                Applitools::FloatingRegion.any(
                  reg, *args_dup
                ).padding(requested_padding)
              end
              next region, padding_proc if return_element
              padding_proc.call(region)
            end
          end
  floating_regions << value
  self
end

#frame(*args) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/applitools/selenium/target.rb', line 252

def frame(*args)
  element = case args.first
            when ::Selenium::WebDriver::Element, Applitools::Selenium::Element, String
              args.first
            else
              proc { |d| d.find_element(*args) }
            end
  frames << frame_or_element if frame_or_element
  self.frame_or_element = element
  reset_for_fullscreen
  self
end

#fully(value = true) ⇒ Object



246
247
248
249
250
# File 'lib/applitools/selenium/target.rb', line 246

def fully(value = true)
  options[:stitch_content] = value ? true : false
  handle_frames
  self
end

#ignore(region_or_element, how, what, padding = Applitools::PaddingBounds::PIXEL_PADDING) ⇒ Object

Add the wanted ignored regions.

Parameters:

  • region_or_element (Applitools::Selenium::Element, Applitools::Region, ::Selenium::WebDriver::Element)

    the region to ignore or an element representing the region to ignore

  • how (Symbol, String)

    A finder to be used (see Selenium::WebDriver documentation for complete list of available finders)

  • what (Symbol, String)

    An id or selector to find



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/applitools/selenium/target.rb', line 48

def ignore(*args)
  if args.empty?
    reset_ignore
  else
    requested_padding = if args.last.is_a? Applitools::PaddingBounds
                          args.pop
                        else
                          Applitools::PaddingBounds::PIXEL_PADDING
                        end
    ignored_regions << case args.first
                       when Applitools::Region
                         proc { args.first.padding(requested_padding) }
                       when ::Selenium::WebDriver::Element
                         proc do |driver, return_element = false|
                           region = applitools_element_from_selenium_element(driver, args.first)
                           padding_proc = proc do |reg|
                             Applitools::Region.from_location_size(
                               reg.location, reg.size
                             ).padding(requested_padding)
                           end
                           next region, padding_proc if return_element
                           padding_proc.call(region)
                         end
                       when Applitools::Selenium::Element
                         proc do |_driver, return_element = false|
                           region = args.first
                           padding_proc = proc do |reg|
                             Applitools::Region.from_location_size(
                               reg.location, reg.size
                             ).padding(requested_padding)
                           end
                           next region, padding_proc if return_element
                           padding_proc.call(region)
                         end
                       else
                         proc do |driver, return_element = false|
                           region = driver.find_element(*args)
                           padding_proc = proc do |reg|
                             Applitools::Region.from_location_size(
                               reg.location, reg.size
                             ).padding(requested_padding)
                           end
                           next region, padding_proc if return_element
                           padding_proc.call(region)
                         end
                       end

  end
  self
end

#layout(*args) ⇒ Object



170
171
172
173
174
175
# File 'lib/applitools/selenium/target.rb', line 170

def layout(*args)
  return match_level(Applitools::MatchLevel::LAYOUT) if args.empty?
  region = process_region(*args)
  layout_regions << region
  self
end

#process_region(*args) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/applitools/selenium/target.rb', line 201

def process_region(*args)
  r = args.first
  case r
  when ::Selenium::WebDriver::Element
    proc do |driver|
      applitools_element_from_selenium_element(driver, args.dup.first)
    end
  when Applitools::Region, Applitools::Selenium::Element
    proc { r }
  else
    proc do |driver|
      args_dup = args.dup
      driver.find_element(args_dup.shift, args_dup.shift)
    end
  end
end

#region(element, how, what) ⇒ Applitools::Selenium::Target

Add the desired region.

Examples:

Add region by element

target.region(an_element)

Add target region by finder

target.region(:id, 'target_region')

Parameters:

  • element (Applitools::Selenium::Element, Applitools::Region, ::Selenium::WebDriver::Element)

    the target region or an element representing the target region

  • how (Symbol, String)

    The finder to be used (:css, :id, etc. see Selenium::WebDriver documentation for complete list of available finders)

  • what (Symbol, String)

    Selector or id of an element

Returns:



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/applitools/selenium/target.rb', line 276

def region(*args)
  handle_frames
  self.region_to_check = case args.first
                         when ::Selenium::WebDriver::Element
                           proc do |driver|
                             applitools_element_from_selenium_element(driver, args.first)
                           end
                         when Applitools::Selenium::Element, Applitools::Region
                           proc { args.first }
                         when String
                           proc do |driver|
                             driver.find_element(name_or_id: args.first)
                           end
                         else
                           proc do |driver|
                             driver.find_element(*args)
                           end
                         end
  self.coordinate_type = Applitools::EyesScreenshot::COORDINATE_TYPES[:context_relative]
  options[:timeout] = nil
  reset_ignore
  reset_floating
  self
end

#replace_element(original, new, array) ⇒ Object



235
236
237
238
239
240
241
242
243
244
# File 'lib/applitools/selenium/target.rb', line 235

def replace_element(original, new, array)
  case new
  when Array
    index = array.index(original)
    array.delete_at(index)
    array.insert(index, *new)
  when Applitools::Selenium::VGRegion
    array[array.index(original)] = new
  end
end

#replace_region(original_region, new_region, key) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/applitools/selenium/target.rb', line 218

def replace_region(original_region, new_region, key)
  case key
  when :content_regions
    replace_element(original_region, new_region, content_regions)
  when :strict_regions
    replace_element(original_region, new_region, strict_regions)
  when :layout_regions
    replace_element(original_region, new_region, layout_regions)
  when :floating
    replace_element(original_region, new_region, floating_regions)
  when :ignore
    replace_element(original_region, new_region, ignored_regions)
  when :accessibility_regions
    replace_element(original_region, new_region, accessibility_regions)
  end
end

#send_dom(value = true) ⇒ Object



301
302
303
304
# File 'lib/applitools/selenium/target.rb', line 301

def send_dom(value = true)
  options[:send_dom] = value ? true : false
  self
end

#strict(*args) ⇒ Object



184
185
186
187
188
189
# File 'lib/applitools/selenium/target.rb', line 184

def strict(*args)
  return match_level(Applitools::MatchLevel::STRICT) if args.empty?
  region = process_region(*args)
  strict_regions << region
  self
end

#use_dom(value = true) ⇒ Object



306
307
308
309
# File 'lib/applitools/selenium/target.rb', line 306

def use_dom(value = true)
  options[:use_dom] = value ? true : false
  self
end

#visual_grid_options(value) ⇒ Object



195
196
197
198
199
# File 'lib/applitools/selenium/target.rb', line 195

def visual_grid_options(value)
  Applitools::ArgumentGuard.hash(value, 'value')
  options[:visual_grid_options] = value
  self
end