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

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, mod = nil) ⇒ RenderTask

Returns a new instance of RenderTask.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 15

def initialize(name, script_result, visual_grid_manager, server_connector, region_selectors, size_mode,
  region, script_hooks, 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.dom_url_mod = mod
  self.running_tests = []
  super(name) do
    perform
  end
end

Instance Attribute Details

#all_blobsObject

Returns the value of attribute all_blobs.



11
12
13
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 11

def all_blobs
  @all_blobs
end

#discovered_resourcesObject

Returns the value of attribute discovered_resources.



11
12
13
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 11

def discovered_resources
  @discovered_resources
end

#dom_url_modObject

Returns the value of attribute dom_url_mod.



11
12
13
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 11

def dom_url_mod
  @dom_url_mod
end

#put_cacheObject

Returns the value of attribute put_cache.



11
12
13
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 11

def put_cache
  @put_cache
end

#region_selectorsObject

Returns the value of attribute region_selectors.



11
12
13
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 11

def region_selectors
  @region_selectors
end

#region_to_checkObject

Returns the value of attribute region_to_check.



11
12
13
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 11

def region_to_check
  @region_to_check
end

#rendering_infoObject

Returns the value of attribute rendering_info.



11
12
13
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 11

def rendering_info
  @rendering_info
end

#request_resourcesObject

Returns the value of attribute request_resources.



11
12
13
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 11

def request_resources
  @request_resources
end

#resource_cacheObject

Returns the value of attribute resource_cache.



11
12
13
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 11

def resource_cache
  @resource_cache
end

#resource_urlsObject

Returns the value of attribute resource_urls.



11
12
13
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 11

def resource_urls
  @resource_urls
end

#resultObject

Returns the value of attribute result.



11
12
13
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 11

def result
  @result
end

#running_testsObject

Returns the value of attribute running_tests.



11
12
13
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 11

def running_tests
  @running_tests
end

#scriptObject

Returns the value of attribute script.



11
12
13
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 11

def script
  @script
end

#script_hooksObject

Returns the value of attribute script_hooks.



11
12
13
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 11

def script_hooks
  @script_hooks
end

#server_connectorObject

Returns the value of attribute server_connector.



11
12
13
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 11

def server_connector
  @server_connector
end

#size_modeObject

Returns the value of attribute size_mode.



11
12
13
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 11

def size_mode
  @size_mode
end

#visual_grid_managerObject

Returns the value of attribute visual_grid_manager.



11
12
13
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 11

def visual_grid_manager
  @visual_grid_manager
end

Instance Method Details

#add_running_test(running_test) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 222

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

#performObject



37
38
39
40
41
42
43
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 37

def perform
  rq = prepare_data_for_rg(script_data)
  fetch_fails = 0
  begin
    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 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

      if 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
    end

    still_running = need_more_resources || need_more_dom || fetch_fails > MAX_FAILS_COUNT
  end while still_running
  statuses = poll_render_status(rq)
  if statuses.first['status'] == 'error'
    raise Applitools::EyesError, "Render failed for #{statuses.first['renderId']} with the message: " \
    "#{statuses.first['error']}"
  end
  self.result = statuses.first
  statuses
end

#poll_render_status(rq) ⇒ Object



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 100

def poll_render_status(rq)
  iterations = 0
  statuses = []
  begin
    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
    begin
      statuses = proc.call
      fails_count = 0
    rescue StandardError => _e
      sleep 1
      fails_count += 1
    ensure
      iterations += 1
      sleep 0.5
    end while(fails_count > 0 and fails_count < 3)
    finished = !statuses.map { |s| s['status'] }.uniq.include?('rendering') || iterations > MAX_ITERATIONS
  end while(!finished)
  statuses
end

#prepare_data_for_rg(data) ⇒ Object



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

def prepare_data_for_rg(data)
  self.all_blobs = data["blobs"]
  self.resource_urls = data["resourceUrls"].map { |u| URI(u) }
  self.request_resources = Applitools::Selenium::RenderResources.new
  discovered_resources = []

  @discovered_resources_lock = Mutex.new

  fetch_block = proc {}

  handle_css_block = proc do |urls_to_fetch, url|
    urls_to_fetch.each do |discovered_url|
      target_url = URI.parse(discovered_url)
      unless target_url.host
        target_with_base = url.is_a?(URI) ? url.dup : URI.parse(url)
        target_url = target_with_base.merge target_url
        target_url.freeze
      end
      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) }
    retry_count = 3
    begin
      retry_count -= 1
      response = resp_proc.call(key.dup)
    end while response.status != 200 && retry_count > 0
    Applitools::Selenium::VGResource.parse_response(key.dup, response, on_css_fetched: handle_css_block)
  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_css_fetched(handle_css_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::Selenium::RenderResources
    end
  end

  discovered_resources.each do |u|
    request_resources[u] = resource_cache[u]
  end

  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
      r.emulation_info = running_test.browser_info.emulation_info if running_test.browser_info.emulation_info
    end

    dom = Applitools::Selenium::RGridDom.new(
      url: script_data["url"], dom_nodes: script_data['cdt'], resources: request_resources
    )

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

#script_dataObject



127
128
129
130
# File 'lib/applitools/selenium/visual_grid/render_task.rb', line 127

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