Class: Applitools::Selenium::VisualGridEyes

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/applitools/selenium/visual_grid/visual_grid_eyes.rb

Constant Summary collapse

DOM_EXTRACTION_TIMEOUT =

seconds or 5 minutes

300
USE_DEFAULT_MATCH_TIMEOUT =
-1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(visual_grid_manager, server_url = nil) ⇒ VisualGridEyes

Returns a new instance of VisualGridEyes.



25
26
27
28
29
30
31
32
33
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 25

def initialize(visual_grid_manager, server_url = nil)
  ensure_config
  @server_connector = Applitools::Connectivity::ServerConnector.new(server_url)
  self.server_url = server_url if server_url
  self.visual_grid_manager = visual_grid_manager
  self.test_list = Applitools::Selenium::TestList.new
  self.opened = false
  self.driver_lock = Mutex.new
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#current_configObject

Returns the value of attribute current_config.



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

def current_config
  @current_config
end

#current_urlObject

Returns the value of attribute current_url.



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

def current_url
  @current_url
end

#driverObject

Returns the value of attribute driver.



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

def driver
  @driver
end

#driver_lockObject

Returns the value of attribute driver_lock.



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

def driver_lock
  @driver_lock
end

#fetched_cache_mapObject

Returns the value of attribute fetched_cache_map.



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

def fetched_cache_map
  @fetched_cache_map
end

#openedObject

Returns the value of attribute opened.



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

def opened
  @opened
end

#proxyObject

Returns the value of attribute proxy.



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

def proxy
  @proxy
end

#server_urlObject

Returns the value of attribute server_url.



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

def server_url
  @server_url
end

#test_listObject

Returns the value of attribute test_list.



15
16
17
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 15

def test_list
  @test_list
end

#visual_grid_managerObject

Returns the value of attribute visual_grid_manager.



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

def visual_grid_manager
  @visual_grid_manager
end

Instance Method Details

#abort_if_not_closedObject



250
251
252
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 250

def abort_if_not_closed
  test_list.each(&:abort_if_not_closed)
end

#check(tag, target) ⇒ Object



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
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 91

def check(tag, target)
  script = <<-END
    #{Applitools::Selenium::Scripts::PROCESS_PAGE_AND_POLL} return __processPageAndPoll();
  END
  begin
    sleep wait_before_screenshots
    Applitools::EyesLogger.info 'Trying to get DOM snapshot...'

    script_thread = Thread.new do
      result = {}
      while result['status'] != 'SUCCESS'
        Thread.current[:script_result] = driver.execute_script(script)
        begin
          Thread.current[:result] = result = Oj.load(Thread.current[:script_result])
          sleep 0.5
        rescue Oj::ParseError => e
          Applitools::EyesLogger.warn e.message
        end
      end
    end
    sleep 0.5
    script_thread_result = script_thread.join(DOM_EXTRACTION_TIMEOUT)
    raise ::Applitools::EyesError.new 'Timeout error while getting dom snapshot!' unless script_thread_result
    Applitools::EyesLogger.info 'Done!'

    mod = Digest::SHA2.hexdigest(script_thread_result[:script_result])

    region_x_paths = get_regions_x_paths(target)
    render_task = RenderTask.new(
      "Render #{config.short_description} - #{tag}",
      script_thread_result[:result]['value'],
      visual_grid_manager,
      server_connector,
      region_x_paths,
      size_mod,
      region_to_check,
      target.options[:script_hooks],
      mod
    )
    test_list.each do |t|
      t.check(tag, target, render_task)
    end
    test_list.each { |t| t.becomes_not_rendered }
    test_list.each { |t| t.becomes_not_rendered }
    visual_grid_manager.enqueue_render_task render_task
  rescue StandardError => e
    test_list.each { |t| t.becomes_tested }
    Applitools::EyesLogger.error e.class.to_s
    Applitools::EyesLogger.error e.message
  end
end

#check_frame(options = {}) ⇒ Applitools::MatchResult

Validates the contents of an iframe and matches it with the expected output.

Parameters:

  • options (Hash) (defaults to: {})

    The specific parameters of the desired screenshot.

Options Hash (options):

  • :timeout (Fixnum)

    The amount of time to retry matching. (Seconds)

  • :tag (String)

    An optional tag to be associated with the snapshot.

  • :frame (String)

    Frame element or frame name or frame id.

  • :name_or_id (String)

    The name or id of the target frame (deprecated. use :frame instead).

  • :frame_element (String)

    The frame element (deprecated. use :frame instead).

Returns:

  • (Applitools::MatchResult)

    The match results.



349
350
351
352
353
354
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 349

def check_frame(options = {})
  options = { timeout: USE_DEFAULT_MATCH_TIMEOUT, tag: nil }.merge!(options)
  frame = options[:frame] || options[:frame_element] || options[:name_or_id]
  target = Applitools::Selenium::Target.frame(frame).timeout(options[:timeout]).fully
  check(options[:tag], target)
end

#check_region(element, how = nil, what = nil, options = {}) ⇒ Object

Takes a snapshot of the application under test and matches a region of a specific element with the expected region output.

Examples:

Check region by element

check_region(element, tag: 'Check a region by element', match_timeout: 3, stitch_content: false)

Check region by css selector

check_region(:css, '.form-row .input#e_mail', tag: 'Check a region by element', match_timeout: 3,
stitch_content: false)

Parameters:

  • element (Applitools::Selenium::Element)

    Represents a region to check.

  • how (Symbol) (defaults to: nil)

    a finder, such :css or :id. Selects a finder will be used to find an element See Selenium::Webdriver::Element#find_element documentation for full list of possible finders.

  • what (String) (defaults to: nil)

    The value will be passed to a specified finder. If finder is :css it must be a css selector.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :tag (String)

    An optional tag to be associated with the snapshot.

  • :match_timeout (Fixnum)

    The amount of time to retry matching. (Seconds)

  • :stitch_content (Boolean)

    If set to true, will try to get full content of the element (including hidden content due overflow settings) by scrolling the element, taking and stitching partial screenshots.



332
333
334
335
336
337
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 332

def check_region(*args)
  options = { timeout: USE_DEFAULT_MATCH_TIMEOUT, tag: nil }.merge! Applitools::Utils.extract_options!(args)
  target = Applitools::Selenium::Target.new.region(*args).timeout(options[:match_timeout])
  target.fully if options[:stitch_content]
  check(options[:tag], target)
end

#check_region_in_frame(options = {}) ⇒ Applitools::MatchResult

Validates the contents of a region in an iframe and matches it with the expected output.

Parameters:

  • options (Hash) (defaults to: {})

    The specific parameters of the desired screenshot.

Options Hash (options):

  • :name_or_id (String)

    The name or id of the target frame (deprecated. use :frame instead).

  • :frame_element (String)

    The frame element (deprecated. use :frame instead).

  • :frame (String)

    Frame element or frame name or frame id.

  • :tag (String)

    An optional tag to be associated with the snapshot.

  • :by (Symbol)

    By which identifier to find the region (e.g :css, :id).

  • :timeout (Fixnum)

    The amount of time to retry matching. (Seconds)

  • :stitch_content (Boolean)

    Whether to stitch the content or not.

Returns:

  • (Applitools::MatchResult)

    The match results.



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 367

def check_region_in_frame(options = {})
  options = { timeout: USE_DEFAULT_MATCH_TIMEOUT, tag: nil, stitch_content: false }.merge!(options)
  Applitools::ArgumentGuard.not_nil options[:by], 'options[:by]'
  Applitools::ArgumentGuard.is_a? options[:by], 'options[:by]', Array

  how_what = options.delete(:by)
  frame = options[:frame] || options[:frame_element] || options[:name_or_id]

  target = Applitools::Selenium::Target.new.timeout(options[:timeout])
  target.frame(frame) if frame
  target.fully if options[:stitch_content]
  target.region(*how_what)

  check(options[:tag], target)
end

#check_window(tag = nil, match_timeout = USE_DEFAULT_MATCH_TIMEOUT) ⇒ Object

Takes a snapshot of the application under test and matches it with the expected output.

Parameters:

  • tag (String) (defaults to: nil)

    An optional tag to be assosiated with the snapshot.

  • match_timeout (Fixnum) (defaults to: USE_DEFAULT_MATCH_TIMEOUT)

    The amount of time to retry matching (seconds)



305
306
307
308
309
310
311
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 305

def check_window(tag = nil, match_timeout = USE_DEFAULT_MATCH_TIMEOUT)
  target = Applitools::Selenium::Target.window.tap do |t|
    t.timeout(match_timeout)
    t.fully if force_full_page_screenshot
  end
  check(tag, target)
end

#close(throw_exception = true) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 224

def close(throw_exception = true)
  return false if test_list.empty?
  test_list.each(&:close)

  until ((states = test_list.map(&:state_name).uniq).count == 1 && states.first == :completed) do
    sleep 0.5
  end
  self.opened = false

  test_list.select { |t| t.pending_exceptions && !t.pending_exceptions.empty? }.each do |t|
    t.pending_exceptions.each do |e|
      raise e
    end
  end

  if throw_exception
    test_list.map(&:test_result).compact.each do |r|
      raise Applitools::NewTestError.new new_test_error_message(r), r if r.new?
      raise Applitools::DiffsFoundError.new diffs_found_error_message(r), r if r.unresolved? && !r.new?
      raise Applitools::TestFailedError.new test_failed_error_message(r), r if r.failed?
    end
  end
  failed_results = test_list.map(&:test_result).compact.select { |r| !r.as_expected? }
  failed_results.empty? ? test_list.map(&:test_result).compact.first : failed_results
end

#collect_selenium_regions(target) ⇒ Object



157
158
159
160
161
162
163
164
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
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 157

def collect_selenium_regions(target)
  selenium_regions = {}
  target_element = target.region_to_check
  setup_size_mode(target_element, target, :none)
  target.ignored_regions.each do |r|
    selenium_regions[element_or_region(r, target, :ignore)] = :ignore
  end
  target.floating_regions.each do |r|
    selenium_regions[element_or_region(r, target, :floating)] = :floating
  end
  target.layout_regions.each do |r|
    selenium_regions[element_or_region(r, target, :layout_regions)] = :layout
  end
  target.strict_regions.each do |r|
    selenium_regions[element_or_region(r, target, :strict_regions)] = :strict
  end
  target.content_regions.each do |r|
    selenium_regions[element_or_region(r, target, :content_regions)] = :content
  end
  target.accessibility_regions.each do |r|
    case (r = element_or_region(r, target, :accessibility_regions))
    when Array
      r.each do |rr|
        selenium_regions[rr] = :accessibility
      end
    else
      selenium_regions[r] = :accessibility
    end
  end
  selenium_regions[region_to_check] = :target if size_mod == 'selector'

  selenium_regions
end

#configure {|config| ... } ⇒ Object

Yields:



39
40
41
42
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 39

def configure
  return unless block_given?
  yield(config)
end

#element_or_region(target_element, target, options_key) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 208

def element_or_region(target_element, target, options_key)
  if target_element.respond_to?(:call)
    region, padding_proc = target_element.call(driver, true)
    case region
    when Array
      regions_to_replace = region.map { |r| Applitools::Selenium::VGRegion.new(r, padding_proc) }
      target.replace_region(target_element, regions_to_replace, options_key)
    else
      target.replace_region(target_element, Applitools::Selenium::VGRegion.new(region, padding_proc), options_key)
    end
    region
  else
    target_element
  end
end

#ensure_configObject



35
36
37
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 35

def ensure_config
  self.config = Applitools::Selenium::Configuration.new
end

#eyes_connectorObject



83
84
85
86
87
88
89
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 83

def eyes_connector
  logger.info("creating VisualGridEyes server connector")
  ::Applitools::Selenium::EyesConnector.new(server_url, driver_lock: driver_lock).tap do |connector|
    connector.batch = batch
    connector.config = config.deep_clone
  end
end

#get_all_test_resultsObject



258
259
260
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 258

def get_all_test_results
  test_list.map(&:test_result)
end

#get_regions_x_paths(target) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 143

def get_regions_x_paths(target)
  result = []
  collect_selenium_regions(target).each do |el, v|
    if [::Selenium::WebDriver::Element, Applitools::Selenium::Element].include?(el.class)
      xpath = driver.execute_script(Applitools::Selenium::Scripts::GET_ELEMENT_XPATH_JS, el)
      web_element_region = Applitools::Selenium::WebElementRegion.new(xpath, v)
      self.region_to_check = web_element_region if v == :target && size_mod == 'selector'
      result << web_element_region
      target.regions[el] = result.size - 1
    end
  end
  result
end

#get_viewport_size(web_driver = driver) ⇒ Object



78
79
80
81
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 78

def get_viewport_size(web_driver = driver)
  Applitools::ArgumentGuard.not_nil 'web_driver', web_driver
  Applitools::Utils::EyesSeleniumUtils.extract_viewport_size(driver)
end

#open(*args) ⇒ Object



44
45
46
47
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
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 44

def open(*args)
  self.test_list = Applitools::Selenium::TestList.new
  options = Applitools::Utils.extract_options!(args)
  Applitools::ArgumentGuard.hash(options, 'options', [:driver])

  config.app_name = options[:app_name] if config.app_name.nil? || config.app_name && config.app_name.empty?
  config.test_name = options[:test_name] if config.test_name.nil? || config.test_name && config.test_name.empty?
  config.viewport_size = Applitools::RectangleSize.from_any_argument(options[:viewport_size]) if config.viewport_size.nil? || config.viewport_size && config.viewport_size.empty?

  self.driver = options.delete(:driver)
  self.current_url = driver.current_url

  if viewport_size
    set_viewport_size(viewport_size)
  else
    self.viewport_size = get_viewport_size
  end

  visual_grid_manager.open(self)
  visual_grid_manager.add_batch(batch.id) do
    server_connector.close_batch(batch.id)
  end

  logger.info("getting all browsers info...")
  browsers_info_list = config.browsers_info
  logger.info("creating test descriptors for each browser info...")
  browsers_info_list.each(viewport_size) do |bi|
    test_list.push Applitools::Selenium::RunningTest.new(eyes_connector, bi, driver)
  end
  self.opened = true

  driver
end

#open?Boolean

Returns:

  • (Boolean)


254
255
256
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 254

def open?
  opened
end

#server_connectorObject



292
293
294
295
296
297
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 292

def server_connector
  @server_connector.server_url = config.server_url
  @server_connector.api_key = config.api_key
  @server_connector.proxy = config.proxy if config.proxy
  @server_connector
end

#set_viewport_size(value) ⇒ Object



262
263
264
265
266
267
268
269
270
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 262

def set_viewport_size(value)
  begin
    Applitools::Utils::EyesSeleniumUtils.set_viewport_size driver, value
  rescue => e
    logger.error e.class.to_s
    logger.error e.message
    raise Applitools::TestFailedError.new "#{e.class} - #{e.message}"
  end
end

#setup_size_mode(target_element, target, key) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 191

def setup_size_mode(target_element, target, key)
  self.size_mod = 'full-page'

  element_or_region = element_or_region(target_element, target, key)

  case element_or_region
  when ::Selenium::WebDriver::Element, Applitools::Selenium::Element
    self.size_mod = 'selector'
  when Applitools::Region
    self.size_mod = 'region' unless element_or_region == Applitools::Region::EMPTY
  else
    self.size_mod = 'full-page'
  end

  self.region_to_check = element_or_region
end

#test(options = {}) {|driver| ... } ⇒ Object

Use this method to perform seamless testing with selenium through eyes driver. It yields a block and passes to it an Applitools::Selenium::Driver instance, which wraps standard driver. Using Selenium methods inside the ‘test’ block will send the messages to Selenium after creating the Eyes triggers for them. Options are similar to #open

Examples:

eyes.test(app_name: 'my app', test_name: 'my test') do |driver|
   driver.get "http://www.google.com"
   driver.check_window("initial")
end

Yield Parameters:



394
395
396
397
398
399
400
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 394

def test(options = {}, &_block)
  open(options)
  yield(driver)
  close
ensure
  abort_if_not_closed
end