Class: Applitools::Selenium::RenderTask

Inherits:
VGTask
  • Object
show all
Includes:
Jsonable
Defined in:
lib/applitools/selenium/visual_grid/render_task.rb

Constant Summary collapse

MAX_FAILS_COUNT =
5
MAX_ITERATIONS =
100

Instance Attribute Summary collapse

Attributes inherited from VGTask

#name, #uuid

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from VGTask

#call, #on_task_completed, #on_task_error, #on_task_succeeded

Constructor Details

#initialize(name, script_result, visual_grid_manager, server_connector, region_selectors, size_mode, region, script_hooks, force_put, ua_string, mod = nil) ⇒ RenderTask

Returns a new instance of RenderTask.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 26

def initialize(name, script_result, visual_grid_manager, server_connector, region_selectors, size_mode,
  region, script_hooks, force_put, ua_string, mod = nil)

  self.result = nil
  self.script = script_result
  self.visual_grid_manager = visual_grid_manager
  self.server_connector = server_connector
  self.resource_cache = visual_grid_manager.resource_cache
  self.put_cache = visual_grid_manager.put_cache
  self.rendering_info = visual_grid_manager.rendering_info(server_connector)
  self.region_selectors = region_selectors
  self.size_mode = size_mode
  self.region_to_check = region
  self.script_hooks = script_hooks if script_hooks.is_a?(Hash)
  self.ua_string = ua_string
  self.dom_url_mod = mod
  self.running_tests = []
  self.force_put = force_put
  @discovered_resources_lock = Mutex.new
  super(name) do
    perform
  end
end

Instance Attribute Details

#discovered_resourcesObject

Returns the value of attribute discovered_resources.



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

def discovered_resources
  @discovered_resources
end

#dom_url_modObject

Returns the value of attribute dom_url_mod.



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

def dom_url_mod
  @dom_url_mod
end

#force_putObject

Returns the value of attribute force_put.



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

def force_put
  @force_put
end

#put_cacheObject

Returns the value of attribute put_cache.



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

def put_cache
  @put_cache
end

#region_selectorsObject

Returns the value of attribute region_selectors.



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

def region_selectors
  @region_selectors
end

#region_to_checkObject

Returns the value of attribute region_to_check.



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

def region_to_check
  @region_to_check
end

#rendering_infoObject

Returns the value of attribute rendering_info.



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

def rendering_info
  @rendering_info
end

#request_resourcesObject

Returns the value of attribute request_resources.



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

def request_resources
  @request_resources
end

#resource_cacheObject

Returns the value of attribute resource_cache.



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

def resource_cache
  @resource_cache
end

#resultObject

Returns the value of attribute result.



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

def result
  @result
end

#running_testsObject

Returns the value of attribute running_tests.



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

def running_tests
  @running_tests
end

#scriptObject

Returns the value of attribute script.



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

def script
  @script
end

#script_hooksObject

Returns the value of attribute script_hooks.



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

def script_hooks
  @script_hooks
end

#server_connectorObject

Returns the value of attribute server_connector.



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

def server_connector
  @server_connector
end

#size_modeObject

Returns the value of attribute size_mode.



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

def size_mode
  @size_mode
end

#ua_stringObject

Returns the value of attribute ua_string.



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

def ua_string
  @ua_string
end

#visual_grid_managerObject

Returns the value of attribute visual_grid_manager.



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

def visual_grid_manager
  @visual_grid_manager
end

Class Method Details

.apply_base_url(discovered_url, base_url) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 13

def apply_base_url(discovered_url, base_url)
  target_url = discovered_url.is_a?(URI) ? discovered_url : URI.parse(discovered_url)
  return target_url.freeze if target_url.host
  target_with_base = base_url.is_a?(URI) ? base_url.dup : URI.parse(base_url)
  target_url = target_with_base.merge target_url
  target_url.freeze
end

Instance Method Details

#add_running_test(running_test) ⇒ Object



279
280
281
282
283
284
285
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 279

def add_running_test(running_test)
  if running_tests.include?(running_test)
    raise Applitools::EyesError, "The running test #{running_test} already exists in the render task"
  end
  running_tests << running_test
  running_tests.length - 1
end

#parse_frame_dom_resources(data) ⇒ Object



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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 169

def parse_frame_dom_resources(data)
  all_blobs = data['blobs']
  resource_urls = data['resourceUrls'].map { |u| URI(u) }
  discovered_resources = []

  fetch_block = proc {}

  handle_resources_block = proc do |urls_to_fetch, url|
    urls_to_fetch.each do |discovered_url|
      target_url = self.class.apply_base_url(URI.parse(discovered_url), url)
      next unless /^http/i =~ target_url.scheme
      @discovered_resources_lock.synchronize do
        discovered_resources.push target_url
      end
      resource_cache.fetch_and_store(target_url, &fetch_block)
    end
  end

  fetch_block = proc do |_s, key|
    resp_proc = proc { |u| server_connector.download_resource(u, ua_string) }
    retry_count = 3
    response = nil
    loop do
      retry_count -= 1
      response = resp_proc.call(key.dup)
      break unless response.status != 200 && retry_count > 0
    end
    Applitools::Selenium::VGResource.parse_response(
      key.dup,
      response,
      on_resources_fetched: handle_resources_block
    )
  end

  data['frames'].each do |f|
    f['url'] = self.class.apply_base_url(f['url'], data['url'])
    request_resources[f['url']] = parse_frame_dom_resources(f).resource
  end

  blobs = all_blobs.map { |blob| Applitools::Selenium::VGResource.parse_blob_from_script(blob) }.each do |blob|
    request_resources[blob.url] = resource_cache[blob.url] = blob
  end

  blobs.each do |blob|
    blob.on_resources_fetched(handle_resources_block)
    blob.lookup_for_resources
  end

  resource_urls.each do |url|
    resource_cache.fetch_and_store(url, &fetch_block)
  end

  resource_urls.each do |u|
    begin
      request_resources[u] = resource_cache[u]
    rescue Applitools::EyesError => e
      Applitools::EyesLogger.error(e.message)
    end
  end

  discovered_resources.each do |u|
    begin
      request_resources[u] = resource_cache[u]
    rescue Applitools::EyesError => e
      Applitools::EyesLogger.error(e.message)
    end
  end

  Applitools::Selenium::RGridDom.new(
    url: data['url'], dom_nodes: data['cdt'], resources: request_resources
  )
end

#performObject



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
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
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 50

def perform
  rq = prepare_data_for_rg(script_data)
  fetch_fails = 0
  # rubocop:disable Metrics/BlockLength
  loop do
    response = nil
    begin
      response = server_connector.render(rendering_info['serviceUrl'], rendering_info['accessToken'], rq)
    rescue StandardError => e
      Applitools::EyesLogger.error(e.message)
      fetch_fails += 1
      sleep 2
    end
    next unless response
    need_more_dom = false
    need_more_resources = false

    response.each_with_index do |running_render, index|
      rq[index].render_id = running_render['renderId']
      need_more_dom = running_render['needMoreDom']
      need_more_resources = running_render['renderStatus'] == 'need-more-resources'

      dom_resource = rq[index].dom.resource

      cache_key = URI(dom_resource.url)
      cache_key.query = "modifier=#{dom_url_mod}" if dom_url_mod

      if force_put
        request_resources.each do |r, _v|
          put_cache.fetch_and_store(URI(r)) do |_s|
            server_connector.render_put_resource(
              rendering_info['serviceUrl'],
              rendering_info['accessToken'],
              request_resources[r],
              running_render
            )
          end
        end
        request_resources.each do |r, _v|
          put_cache[r]
        end
      end

      if need_more_resources
        running_render['needMoreResources'].each do |resource_url|
          put_cache.fetch_and_store(URI(resource_url)) do |_s|
            server_connector.render_put_resource(
              rendering_info['serviceUrl'],
              rendering_info['accessToken'],
              request_resources[URI(resource_url)],
              running_render
            )
          end
        end
      end

      next unless need_more_dom
      put_cache.fetch_and_store(cache_key) do |_s|
        server_connector.render_put_resource(
          rendering_info['serviceUrl'],
          rendering_info['accessToken'],
          dom_resource,
          running_render
        )
      end
      put_cache[cache_key]
    end

    still_running = need_more_resources || need_more_dom || fetch_fails > MAX_FAILS_COUNT
    break unless still_running
  end
  # rubocop:enable Metrics/BlockLength
  statuses = poll_render_status(rq)
  self.result = statuses.first
  statuses
end

#poll_render_status(rq) ⇒ Object



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
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 127

def poll_render_status(rq)
  iterations = 0
  statuses = []
  loop do
    fails_count = 0
    proc = proc do
      server_connector.render_status_by_id(
        rendering_info['serviceUrl'],
        rendering_info['accessToken'],
        Oj.dump(json_value(rq.map(&:render_id)))
      )
    end
    loop do
      begin
        statuses = proc.call
        fails_count = 0
      rescue StandardError => _e
        sleep 1
        fails_count += 1
      ensure
        iterations += 1
        sleep 0.5
      end
      break unless fails_count > 0 && fails_count < 3
    end
    finished = !statuses.map { |s| s['status'] }.uniq.include?('rendering') || iterations > MAX_ITERATIONS
    break if finished
  end
  statuses
end

#prepare_data_for_rg(data) ⇒ Object



163
164
165
166
167
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 163

def prepare_data_for_rg(data)
  self.request_resources = Applitools::Selenium::RenderResources.new
  dom = parse_frame_dom_resources(data)
  prepare_rg_requests(running_tests, dom)
end

#prepare_rg_requests(running_tests, dom) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 242

def prepare_rg_requests(running_tests, dom)
  requests = Applitools::Selenium::RenderRequests.new

  running_tests.each do |running_test|
    r_info = Applitools::Selenium::RenderInfo.new.tap do |r|
      r.width = running_test.browser_info.viewport_size.width
      r.height = running_test.browser_info.viewport_size.height
      r.size_mode = size_mode
      r.region = region_to_check
      if running_test.browser_info.respond_to?(:emulation_info) && running_test.browser_info.emulation_info
        r.emulation_info = running_test.browser_info.emulation_info
      elsif running_test.browser_info.respond_to?(:ios_device_info) && running_test.browser_info.ios_device_info
        r.ios_device_info = running_test.browser_info.ios_device_info
      end
    end

    requests << Applitools::Selenium::RenderRequest.new(
      webhook: rendering_info['resultsUrl'],
      url: script_data['url'],
      stitching_service_url: rendering_info['stitchingServiceUrl'],
      dom: dom,
      resources: request_resources,
      render_info: r_info,
      browser: { name: running_test.browser_info.browser_type },
      platform: { name: running_test.browser_info.platform },
      script_hooks: script_hooks,
      selectors_to_find_regions_for: region_selectors,
      send_dom: if running_test.eyes.configuration.send_dom.nil?
                  false.to_s
                else
                  running_test.eyes.configuration.send_dom.to_s
                end
    )
  end
  requests
end

#script_dataObject



158
159
160
161
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 158

def script_data
  # @script_data ||= Oj.load script
  @script_data ||= script
end