Class: Applitools::EyesBase

Inherits:
Object
  • Object
show all
Extended by:
Helpers, Forwardable
Defined in:
lib/applitools/core/eyes_base.rb

Direct Known Subclasses

Images::Eyes

Defined Under Namespace

Classes: UserInputArray

Constant Summary collapse

DEFAULT_MATCH_TIMEOUT =

seconds

2
USE_DEFAULT_TIMEOUT =
-1
SCREENSHOT_AS_IS =
Applitools::EyesScreenshot::COORDINATE_TYPES[:screenshot_as_is].freeze
CONTEXT_RELATIVE =
Applitools::EyesScreenshot::COORDINATE_TYPES[:context_relative].freeze
MATCH_LEVEL =
{
  none: 'None',
  layout: 'Layout',
  layout2: 'Layout2',
  content: 'Content',
  strict: 'Strict',
  exact: 'Exact'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

abstract_attr_accessor, abstract_method

Constructor Details

#initialize(server_url = nil) ⇒ EyesBase

Returns a new instance of EyesBase.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/applitools/core/eyes_base.rb', line 43

def initialize(server_url = nil)
  Applitools::Connectivity::ServerConnector.server_url = server_url
  self.disabled = false
  @viewport_size = nil
  self.match_timeout = DEFAULT_MATCH_TIMEOUT
  self.running_session = nil
  self.save_new_tests = true
  self.save_failed_tests = false
  self.agent_id = nil
  self.last_screenshot = nil
  @user_inputs = UserInputArray.new
  self.app_output_provider = Object.new
  self.verbose_results = false

  get_app_output_method = ->(r, s) { get_app_output_with_screenshot r, s }

  app_output_provider.instance_eval do
    define_singleton_method :app_output do |r, s|
      get_app_output_method.call(r, s)
    end
  end

  self.default_match_settings = MATCH_LEVEL[:exact]
end

Instance Attribute Details

#agent_idObject

Returns the value of attribute agent_id.



33
34
35
# File 'lib/applitools/core/eyes_base.rb', line 33

def agent_id
  @agent_id
end

#app_nameObject

Returns the value of attribute app_name.



33
34
35
# File 'lib/applitools/core/eyes_base.rb', line 33

def app_name
  @app_name
end

#base_line_nameObject

Returns the value of attribute base_line_name.



33
34
35
# File 'lib/applitools/core/eyes_base.rb', line 33

def base_line_name
  @base_line_name
end

#baseline_nameObject

Returns the value of attribute baseline_name.



33
34
35
# File 'lib/applitools/core/eyes_base.rb', line 33

def baseline_name
  @baseline_name
end

#batchObject

Returns the value of attribute batch.



33
34
35
# File 'lib/applitools/core/eyes_base.rb', line 33

def batch
  @batch
end

#branch_nameObject

Returns the value of attribute branch_name.



33
34
35
# File 'lib/applitools/core/eyes_base.rb', line 33

def branch_name
  @branch_name
end

#cut_providerObject

Returns the value of attribute cut_provider.



33
34
35
# File 'lib/applitools/core/eyes_base.rb', line 33

def cut_provider
  @cut_provider
end

#failure_reportsObject

Returns the value of attribute failure_reports.



33
34
35
# File 'lib/applitools/core/eyes_base.rb', line 33

def failure_reports
  @failure_reports
end

#host_appObject

Returns the value of attribute host_app.



33
34
35
# File 'lib/applitools/core/eyes_base.rb', line 33

def host_app
  @host_app
end

#host_osObject

Returns the value of attribute host_os.



33
34
35
# File 'lib/applitools/core/eyes_base.rb', line 33

def host_os
  @host_os
end

#match_timeoutObject

Returns the value of attribute match_timeout.



33
34
35
# File 'lib/applitools/core/eyes_base.rb', line 33

def match_timeout
  @match_timeout
end

#parent_branch_nameObject

Returns the value of attribute parent_branch_name.



33
34
35
# File 'lib/applitools/core/eyes_base.rb', line 33

def parent_branch_name
  @parent_branch_name
end

#position_providerObject

Returns the value of attribute position_provider.



33
34
35
# File 'lib/applitools/core/eyes_base.rb', line 33

def position_provider
  @position_provider
end

#save_failed_testsObject

Returns the value of attribute save_failed_tests.



33
34
35
# File 'lib/applitools/core/eyes_base.rb', line 33

def save_failed_tests
  @save_failed_tests
end

#save_new_testsObject

Returns the value of attribute save_new_tests.



33
34
35
# File 'lib/applitools/core/eyes_base.rb', line 33

def save_new_tests
  @save_new_tests
end

#scale_ratioObject

Returns the value of attribute scale_ratio.



33
34
35
# File 'lib/applitools/core/eyes_base.rb', line 33

def scale_ratio
  @scale_ratio
end

#verbose_resultsboolean

If set to true it will display test results in verbose format, including all fields returned by the server Default value is false.

Returns:

  • (boolean)

    verbose_results flag



33
34
35
# File 'lib/applitools/core/eyes_base.rb', line 33

attr_accessor :app_name, :baseline_name, :branch_name, :parent_branch_name, :batch, :agent_id, :full_agent_id,
:match_timeout, :save_new_tests, :save_failed_tests, :failure_reports, :default_match_settings, :cut_provider,
:scale_ratio, :host_os, :host_app, :base_line_name, :position_provider, :viewport_size, :verbose_results

#viewport_sizeObject

Returns the value of attribute viewport_size.



33
34
35
# File 'lib/applitools/core/eyes_base.rb', line 33

def viewport_size
  @viewport_size
end

Instance Method Details

#abort_if_not_closedObject



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
# File 'lib/applitools/core/eyes_base.rb', line 92

def abort_if_not_closed
  if disabled?
    logger.info "#{__method__} Ignored"
    return
  end

  self.open = false
  self.last_screenshot = nil
  clear_user_inputs

  if running_session.nil?
    logger.info 'Closed'
    return
  end

  logger.info 'Aborting server session...'
  Applitools::Connectivity::ServerConnector.stop_session(running_session, true, false)
  logger.info '---Test aborted'

rescue Applitools::EyesError => e
  logger.error e.messages

ensure
  self.running_session = nil
end

#check_window_base(region_provider, tag, ignore_mismatch, retry_timeout) ⇒ 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
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
# File 'lib/applitools/core/eyes_base.rb', line 157

def check_window_base(region_provider, tag, ignore_mismatch, retry_timeout)
  if disabled?
    logger.info "#{__method__} Ignored"
    result = Applitools::MatchResults.new
    result.as_expected = true
    return result
  end

  raise Applitools::EyesError.new 'Eyes not open' unless open?
  Applitools::ArgumentGuard.not_nil region_provider, 'region_provider'

  logger.info "check_window_base(#{region_provider}, #{tag}, #{ignore_mismatch}, #{retry_timeout})"

  tag = '' if tag.nil?

  if running_session.nil?
    logger.info 'No running session, calling start session..'
    start_session
    logger.info 'Done!'
    @match_window_task = Applitools::MatchWindowTask.new(
      logger,
      running_session,
      match_timeout,
      app_output_provider
    )
  end

  logger.info 'Calling match_window...'
  result = @match_window_task.match_window(
    user_inputs: user_inputs,
    last_screenshot: last_screenshot,
    region_provider: region_provider,
    tag: tag,
    should_match_window_run_once_on_timeout: should_match_window_run_once_on_timeout,
    ignore_mismatch: ignore_mismatch,
    retry_timeout: retry_timeout
  )
  logger.info 'match_window done!'

  if result.as_expected?
    clear_user_inputs
    self.last_screenshot = result.screenshot
  else
    unless ignore_mismatch
      clear_user_inputs
      self.last_screenshot = result.screenshot
    end

    self.should_match_window_run_once_on_timeout = true

    logger.info "Mistmatch! #{tag}" unless running_session.new_session?

    if failure_reports == :immediate
      raise Applitools::TestFailedException.new "Mistmatch found in #{session_start_info.scenario_id_or_name}" \
          " of #{session_start_info.app_id_or_name}"
    end
  end

  logger.info 'Done!'
  result
end

#close(throw_exception = true) ⇒ Object

Closes eyes otherwise the test will pass. Default is true

Parameters:

  • throw_exception (Boolean) (defaults to: true)

    If set to true eyes will trow [Applitools::TestFailedError] exception,



223
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
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
278
279
280
281
# File 'lib/applitools/core/eyes_base.rb', line 223

def close(throw_exception = true)
  if disabled?
    logger.info "#{__method__} Ignored"
    return
  end

  logger.info "close(#{throw_exception})"
  raise Applitools::EyesError.new 'Eyes not open' unless open?

  self.open = false
  self.last_screenshot = nil

  clear_user_inputs

  unless running_session
    logger.info 'Server session was not started'
    logger.info '--- Empty test ended'
    return Applitools::TestResults.new
  end

  is_new_session = running_session.new_session?
  session_results_url = running_session.url

  logger.info 'Ending server session...'

  save = is_new_session && save_new_tests || !is_new_session && save_failed_tests

  logger.info "Automatically save test? #{save}"

  results = Applitools::Connectivity::ServerConnector.stop_session running_session, false, save

  results.is_new = is_new_session
  results.url = session_results_url

  logger.info results.to_s(verbose_results)

  if results.failed?
    logger.error "--- Failed test ended. see details at #{session_results_url}"
    error_message = "#{session_start_info.scenario_id_or_name} of #{session_start_info.app_id_or_name}. " \
        "See details at #{session_results_url}."
    raise Applitools::TestFailedError.new error_message, results if throw_exception
    return results
  end

  if results.new?
    instructions = "Please approve the new baseline at #{session_results_url}"
    logger.info "--- New test ended. #{instructions}"
    error_message = "#{session_start_info.scenario_id_or_name} of #{session_start_info.app_id_or_name}. " \
        "#{instructions}"
    raise Applitools::TestFailedError.new error_message, results if throw_exception
    return results
  end

  logger.info '--- Test passed'
  return results
ensure
  self.running_session = nil
  self.current_app_name = nil
end

#disabled=(value) ⇒ Object



76
77
78
# File 'lib/applitools/core/eyes_base.rb', line 76

def disabled=(value)
  @disabled = Applitools::Utils.boolean_value value
end

#disabled?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/applitools/core/eyes_base.rb', line 80

def disabled?
  @disabled
end

#open?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/applitools/core/eyes_base.rb', line 84

def open?
  @open
end

#open_base(options) ⇒ Object



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
154
155
# File 'lib/applitools/core/eyes_base.rb', line 118

def open_base(options)
  if disabled?
    logger.info "#{__method__} Ignored"
    return
  end

  Applitools::ArgumentGuard.hash options, 'open_base parameter', [:test_name]
  default_options = { session_type: 'SEQUENTAL' }
  options = default_options.merge options

  if app_name.nil?
    Applitools::ArgumentGuard.not_nil options[:app_name], 'options[:app_name]'
    self.current_app_name = options[:app_name]
  else
    self.current_app_name = app_name
  end

  Applitools::ArgumentGuard.not_nil options[:test_name], 'options[:test_name]'
  self.test_name = options[:test_name]
  logger.info "Agent = #{full_agent_id}"
  logger.info "openBase(app_name: #{options[:app_name]}, test_name: #{options[:test_name]}," \
      " viewport_size: #{options[:viewport_size]})"

  raise Applitools::EyesError.new 'API key is missing! Please set it using api_key=' if api_key.nil?

  if open?
    abort_if_not_closed
    raise Applitools::EyesError.new 'A test is already running'
  end

  self.viewport_size = options[:viewport_size]
  self.session_type = options[:session_type]

  self.open = true
rescue Applitools::EyesError => e
  logger.error e.message
  raise e
end