Class: Applitools::Connectivity::UniversalClient

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/applitools/universal_sdk/universal_client.rb

Constant Summary collapse

CORE_GET_EC_CLIENT =
'Core.getECClient'
SESSION_INIT =
'Core.makeCore'
CORE_MAKE_MANAGER =
'Core.makeManager'
CORE_GET_VIEWPORT_SIZE =
'Core.getViewportSize'
CORE_SET_VIEWPORT_SIZE =
'Core.setViewportSize'
CORE_CLOSE_BATCHES =
'Core.closeBatch'
CORE_DELETE_TEST =
'Core.deleteTest'
EYES_MANAGER_MAKE_EYES =
'EyesManager.openEyes'
EYES_MANAGER_CLOSE_ALL_EYES =
'EyesManager.getResults'
EYES_CHECK =
'Eyes.check'
EYES_CHECK_AND_CLOSE =

'Eyes.checkAndClose'
EYES_LOCATE =
'Core.locate'
EYES_EXTRACT_TEXT_REGIONS =
'Core.locateText'
EYES_EXTRACT_TEXT =
'Core.extractText'
EYES_CLOSE =
'Eyes.close'
EYES_ABORT =
'Eyes.abort'
EYES_GET_RESULTS =
'Eyes.getResults'
SERVER_LOG_EVENT_NAME =
'Logger.log'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(eyes = nil, mask_log = nil) ⇒ UniversalClient

Returns a new instance of UniversalClient.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/applitools/universal_sdk/universal_client.rb', line 42

def initialize(eyes = nil, mask_log = nil)
  # @socket = Applitools::Connectivity::UniversalClientSocket.new
  prepare_socket(eyes, mask_log)
  # store on open for next check calls
  @open_config = nil
  @commands_config = {
    open: {},
    screenshot: {},
    check: {},
    close: {}
  }
end

Instance Attribute Details

#commands_configObject

Returns the value of attribute commands_config.



40
41
42
# File 'lib/applitools/universal_sdk/universal_client.rb', line 40

def commands_config
  @commands_config
end

Instance Method Details

#config_mapping(old_config, command_config, name) ⇒ Object



109
110
111
112
# File 'lib/applitools/universal_sdk/universal_client.rb', line 109

def config_mapping(old_config, command_config, name)
  return if old_config[name].nil?
  command_config[name] = old_config.delete(name)
end

#core_close_batches(close_batch_settings = nil) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/applitools/universal_sdk/universal_client.rb', line 317

def core_close_batches(close_batch_settings = nil)
  # interface CloseBatchRequestPayload {
  #   settings: CloseBatchSettings | CloseBatchSettings[]
  # }
  unless close_batch_settings.is_a?(Hash)
    batch_ids = [@open_config[:batch][:id]]
    batch_ids = [close_batch_settings] if close_batch_settings.is_a?(String)
    batch_ids = close_batch_settings if close_batch_settings.is_a?(Array)
    optional = [:serverUrl, :apiKey, :proxy].map { |k| [k, @open_config[k]] }.to_h
    optional[:eyesServerUrl] = optional.delete(:serverUrl) if optional[:serverUrl] # v4
    close_batch_settings = { settings: ({ batchIds: batch_ids }.merge(optional).compact) }
  end
  command_with_result(CORE_CLOSE_BATCHES, close_batch_settings)
end

#core_delete_test(delete_test_settings) ⇒ Object



332
333
334
335
336
337
# File 'lib/applitools/universal_sdk/universal_client.rb', line 332

def core_delete_test(delete_test_settings)
  # interface DeleteTestRequestPayload {
  #   settings: DeleteTestSettings | DeleteTestSettings[]
  # }
  command_with_result(CORE_DELETE_TEST, delete_test_settings)
end

#core_get_viewport_size(driver) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
# File 'lib/applitools/universal_sdk/universal_client.rb', line 297

def core_get_viewport_size(driver)
  # interface GetViewportSizeRequestPayload {
  #   target: DriverTarget
  # }
  #
  # interface GetViewportSizeResponsePayload {
  #   width: number
  #   height: number
  # }
  command_with_result(CORE_GET_VIEWPORT_SIZE, { target: driver })
end

#core_make_ec_client(server_url, api_key, proxy) ⇒ Object



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
# File 'lib/applitools/universal_sdk/universal_client.rb', line 55

def core_make_ec_client(server_url, api_key, proxy)
  # ECClientSettings
  #
  # {
  #   "options": { // default options that will be used if user do not provide `applitools:` caps
  #     "serverUrl": "https://eyesapi.applitools.com/",
  #     "apiKey": "DFH$HJD%77333J355",
  #   },
  #   "proxy": {
  #     "url": "http://localhost:8080",
  #     "username": "username",
  #     "password": "Pa$$w0rD"
  #   },
  # }
  ec_client_capabilities = {}
  ec_client_capabilities[:ecServerUrl] = server_url unless server_url.nil?
  ec_client_capabilities[:apiKey] = api_key unless api_key.nil?
  ec_client_settings = {}
  ec_client_settings[:options] = ec_client_capabilities unless ec_client_capabilities.empty?
  ec_client_settings[:proxy] = proxy unless proxy.nil?
  make_ec_client_request_payload = { settings: ec_client_settings }
  # interface GetECClientRequestPayload {
  #   settings?: ECClientSettings
  # }
  #
  # interface GetECClientResponsePayload {
  #   url: string
  # }
  command_with_result(CORE_GET_EC_CLIENT, make_ec_client_request_payload)
end

#core_make_manager(eyes_manager_config) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/applitools/universal_sdk/universal_client.rb', line 93

def core_make_manager(eyes_manager_config)
  # interface MakeManagerRequestPayload {
  #   type: 'ufg' | 'classic'
  #   settings?: EyesManagerSettings
  # }
  # ### EyesManagerSettings
  # {
  #   "concurrency": 10,
  #   "legacyConcurrency": 50,
  #   "agentId": "js/eyes/1.0.0"
  # }
  #
  # type MakeManagerResponsePayload = Ref<EyesManager>
  command_with_result(CORE_MAKE_MANAGER, eyes_manager_config)
end

#core_set_viewport_size(driver, size) ⇒ Object



309
310
311
312
313
314
315
# File 'lib/applitools/universal_sdk/universal_client.rb', line 309

def core_set_viewport_size(driver, size)
  # interface SetViewportSizeRequestPayload {
  #   target: DriverTarget
  #   size: {width: number, height: number}
  # }
  command_with_result(CORE_SET_VIEWPORT_SIZE, { target: driver, size: size })
end

#eyes_abort(eyes) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/applitools/universal_sdk/universal_client.rb', line 280

def eyes_abort(eyes)
  # interface AbortPayload {
  #   eyes: Ref<Eyes>
  #   settings?: CloseSettings
  # }
  #
  # type AbortResponsePayload = void
  settings = commands_config[:close]
  # CloseSettings
  #
  # {
  #   "updateBaselineIfNew": true,
  #   "updateBaselineIfDifferent": true
  # }
  command_with_result(EYES_ABORT, { eyes: eyes, settings: settings })
end

#eyes_check(eyes, settings, image_target = {}) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/applitools/universal_sdk/universal_client.rb', line 206

def eyes_check(eyes, settings, image_target = {})
  # interface CheckRequestPayload {
  #   eyes: Ref<Eyes>
  #   target?: ImageTarget | DriverTarget
  #   settings?: CheckSettings
  #   config?: Config
  # }
  #
  # type CheckResponsePayload = CheckResult[]
  payload = { eyes: eyes, settings: settings, config: commands_config }
  payload[:target] = image_target unless image_target.empty?
  command_with_result(EYES_CHECK, payload)
end

#eyes_close(eyes) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/applitools/universal_sdk/universal_client.rb', line 262

def eyes_close(eyes)
  # interface CloseResponsePayload {
  #   eyes: Ref<Eyes>
  #   settings?: CloseSettings
  #   config?: Config
  # }
  #
  # type CloseResponsePayload = void
  settings = commands_config[:close]
  # CloseSettings
  #
  # {
  #   "updateBaselineIfNew": true,
  #   "updateBaselineIfDifferent": true
  # }
  command_with_result(EYES_CLOSE, { eyes: eyes, settings: settings, config: commands_config })
end

#eyes_extract_text(eyes, regions, driver_target) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/applitools/universal_sdk/universal_client.rb', line 247

def eyes_extract_text(eyes, regions, driver_target)
  # interface ExtractTextRequestPayload {
  #   target?: ImageTarget | DriverTarget
  #   settings?: ExtractTextSettings | ExtractTextSettings[]
  #   config?: Config
  # }
  #
  # type ExtractTextResponsePayload = string[]
  payload = { target: driver_target, settings: regions, config: commands_config }
  payload.delete(:settings) if regions.empty?
  result = command_with_result(EYES_EXTRACT_TEXT, payload)
  # Ensure we return the result array
  result.is_a?(Hash) && result['result'] ? result['result'] : result
end

#eyes_extract_text_regions(eyes, settings, driver_target) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/applitools/universal_sdk/universal_client.rb', line 234

def eyes_extract_text_regions(eyes, settings, driver_target)
  # interface LocateTextRequestPayload {
  #   target?: ImageTarget | DriverTarget
  #   settings?: LocateTextSettings
  #   config?: Config
  # }
  #
  # type LocateTextResponsePayload = Record<string, {text: string, x: number, y: number, width: number, height: number}[]>
  payload = { target: driver_target, settings: settings, config: commands_config }
  payload.delete(:target) if driver_target.nil? || driver_target.empty?
  command_with_result(EYES_EXTRACT_TEXT_REGIONS, payload)
end

#eyes_get_results(eyes) ⇒ Object



195
196
197
198
199
200
201
202
203
204
# File 'lib/applitools/universal_sdk/universal_client.rb', line 195

def eyes_get_results(eyes)
  # interface GetEyesResultsPayload {
  #   eyes: Ref<Eyes>
  #   settings?: GetResultsSettings
  # }
  #
  # type GetEyesResultsResponsePayload = TestResult[]
  settings = { throwErr: false }
  command_with_result(EYES_GET_RESULTS, { eyes: eyes, settings: settings })
end

#eyes_locate(eyes, settings, driver_target) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/applitools/universal_sdk/universal_client.rb', line 220

def eyes_locate(eyes, settings, driver_target)
  # interface LocateRequestPayload {
  #   target?: ImageTarget | DriverTarget
  #   settings?: LocateSettings
  #   config?: Config
  # }
  #
  # interface LocateResponsePayload {
  #   [key: string]: Array<{x: number, y: number, width: number, height: number}>
  # }

  command_with_result(EYES_LOCATE, { target: driver_target, settings: settings, config: commands_config })
end

#eyes_manager_close_all_eyes(manager, remove_duplicate_tests) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/applitools/universal_sdk/universal_client.rb', line 170

def eyes_manager_close_all_eyes(manager, remove_duplicate_tests)
  # interface GetManagerResultsRequestPayload {
  #   manager: Ref<EyesManager>
  #   settings?: {throwErr?: boolean}
  # }
  #
  # interface GetManagerResultsResponsePayload {
  #   results: {
  #     error?: Error
  #     result?: TestResult
  #     renderer?: TType extends 'ufg' ? Renderer : never
  #     userTestId: string
  #   }[]
  #   passed: number
  #   unresolved: number
  #   failed: number
  #   exceptions: number
  #   mismatches: number
  #   missing: number
  #   matches: number
  # }
  settings = { throwErr: false, removeDuplicateTests: remove_duplicate_tests }
  command_with_result(EYES_MANAGER_CLOSE_ALL_EYES, { manager: manager, settings: settings })
end

#eyes_manager_make_eyes(manager, driver_config, config) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/applitools/universal_sdk/universal_client.rb', line 155

def eyes_manager_make_eyes(manager, driver_config, config)
  @open_config = config

  map_open_eyes_config_to_commands_config(config)
  # interface OpenEyesRequestPayload {
  #   manager: Ref<EyesManager>
  #   target?: DriverTarget
  #   settings?: OpenSettings
  #   config?: Config
  # }
  #
  # type OpenEyesResponsePayload = Ref<Eyes>
  command_with_result(EYES_MANAGER_MAKE_EYES, { manager: manager, target: driver_config, settings: commands_config[:open], config: commands_config })
end

#make_manager(eyes_manager_config) ⇒ Object



86
87
88
89
90
91
# File 'lib/applitools/universal_sdk/universal_client.rb', line 86

def make_manager(eyes_manager_config)
  Applitools::EyesLogger.logger.debug "EyesManager config: #{eyes_manager_config}"
  eyes_manager = core_make_manager(eyes_manager_config)
  Applitools::EyesLogger.logger.debug "EyesManager applitools-ref-id: #{eyes_manager[:"applitools-ref-id"]}"
  Applitools::UniversalEyesManager.new(eyes_manager, self)
end

#map_open_eyes_config_to_commands_config(config) ⇒ Object



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
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/applitools/universal_sdk/universal_client.rb', line 114

def map_open_eyes_config_to_commands_config(config)
  [
    :eyesServerUrl, :apiKey, :proxy, :connectionTimeout, :removeSession, :agentId, :appName, :testName, :displayName,
    :userTestId, :sessionType, :properties, :batch, :keepBatchOpen, :environmentName, :environment, :branchName,
    :parentBranchName, :baselineEnvName, :baselineBranchName, :compareWithParentBranch, :ignoreBaseline,
    :ignoreGitBranching, :saveDiffs, :abortIdleTestTimeout
  ].each do |k|
    config_mapping(config, commands_config[:open], k)
  end

  commands_config[:open][:keepBatchOpen] = config.delete(:dontCloseBatches) unless config[:dontCloseBatches].nil?

  [:hideCaret, :hideScrollbars, :disableBrowserFetching, :sendDom, :stitchMode,
   :layoutBreakpoints, :waitBeforeCapture].each do |k|
    config_mapping(config, commands_config[:check], k)
  end

  commands_config[:check][:environments] = config.delete(:browsersInfo) unless config[:browsersInfo].nil?

  unless config[:defaultMatchSettings].nil?
    if config[:defaultMatchSettings][:accessibilitySettings]
      commands_config[:check][:accessibilitySettings] = {}
      commands_config[:check][:accessibilitySettings][:level] = config[:defaultMatchSettings][:accessibilitySettings].delete(:level) unless config[:defaultMatchSettings][:accessibilitySettings][:level].nil?
      commands_config[:check][:accessibilitySettings][:version] = config[:defaultMatchSettings][:accessibilitySettings].delete(:guidelinesVersion) unless config[:defaultMatchSettings][:accessibilitySettings][:guidelinesVersion].nil?
      config[:defaultMatchSettings].delete(:accessibilitySettings) if config[:defaultMatchSettings][:accessibilitySettings].empty?
    end
    commands_config[:check][:ignoreCaret] = config[:defaultMatchSettings].delete(:ignoreCaret) unless config[:defaultMatchSettings][:ignoreCaret].nil?
    commands_config[:check][:matchLevel] = config[:defaultMatchSettings].delete(:matchLevel) unless config[:defaultMatchSettings][:matchLevel].nil?
    config.delete(:defaultMatchSettings) if config[:defaultMatchSettings].empty?
  end

  if commands_config[:check][:fully].nil?
    commands_config[:check][:fully] = config.delete(:forceFullPageScreenshot) unless config[:forceFullPageScreenshot].nil?
  end

  commands_config[:close][:updateBaselineIfNew] = config.delete(:saveNewTests) unless config[:saveNewTests].nil?
  commands_config[:close][:updateBaselineIfDifferent] = config.delete(:saveFailedTests) unless config[:saveFailedTests].nil?

  commands_config[:screenshot] = commands_config[:check]
end