Class: Applitools::Calabash::Eyes

Inherits:
EyesBase
  • Object
show all
Defined in:
lib/applitools/calabash/eyes.rb

Constant Summary

Constants inherited from EyesBase

EyesBase::CONTEXT_RELATIVE, EyesBase::DEFAULT_MATCH_TIMEOUT, EyesBase::SCREENSHOT_AS_IS, EyesBase::USE_DEFAULT_TIMEOUT

Instance Attribute Summary collapse

Attributes inherited from EyesBase

#agent_id, #app_name, #base_line_name, #baseline_name, #batch, #branch_name, #cut_provider, #default_match_settings, #exact, #failure_reports, #host_app, #host_os, #match_level, #match_timeout, #parent_branch_name, #position_provider, #remove_session_if_matching, #save_failed_tests, #save_new_tests, #scale_ratio, #server_remainder, #server_scale, #verbose_results, #viewport_size

Instance Method Summary collapse

Methods inherited from EyesBase

#abort_if_not_closed, #add_property, #check_single_base, #check_window_base, #close, #disabled=, #disabled?, #ensure_running_session, #open?, #open_base, #set_default_match_settings

Methods included from Helpers

#abstract_attr_accessor, #abstract_method

Methods included from MatchLevelSetter

#match_level_with_exact

Constructor Details

#initialize(server_url = Applitools::Connectivity::ServerConnector::DEFAULT_SERVER_URL) ⇒ Eyes

Returns a new instance of Eyes.



8
9
10
11
12
13
14
15
# File 'lib/applitools/calabash/eyes.rb', line 8

def initialize(server_url = Applitools::Connectivity::ServerConnector::DEFAULT_SERVER_URL)
  super
  self.base_agent_id = "eyes.calabash.ruby/#{Applitools::VERSION}".freeze
  self.debug_screenshot = true
  self.debug_screenshot_provider = Applitools::DebugScreenshotProvider.new
                                                                      .tag_access { tag_for_debug }
                                                                      .debug_flag_access { debug_screenshot }
end

Instance Attribute Details

#base_agent_idObject

Returns the value of attribute base_agent_id.



4
5
6
# File 'lib/applitools/calabash/eyes.rb', line 4

def base_agent_id
  @base_agent_id
end

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/applitools/calabash/eyes.rb', line 6

def context
  @context
end

#debug_screenshotObject

Returns the value of attribute debug_screenshot.



4
5
6
# File 'lib/applitools/calabash/eyes.rb', line 4

def debug_screenshot
  @debug_screenshot
end

#debug_screenshot_providerObject

Returns the value of attribute debug_screenshot_provider.



4
5
6
# File 'lib/applitools/calabash/eyes.rb', line 4

def debug_screenshot_provider
  @debug_screenshot_provider
end

#device_pixel_ratioObject

Returns the value of attribute device_pixel_ratio.



4
5
6
# File 'lib/applitools/calabash/eyes.rb', line 4

def device_pixel_ratio
  @device_pixel_ratio
end

#full_page_capture_algorithmObject

Returns the value of attribute full_page_capture_algorithm.



4
5
6
# File 'lib/applitools/calabash/eyes.rb', line 4

def full_page_capture_algorithm
  @full_page_capture_algorithm
end

#tag_for_debugObject

Returns the value of attribute tag_for_debug.



4
5
6
# File 'lib/applitools/calabash/eyes.rb', line 4

def tag_for_debug
  @tag_for_debug
end

#titleObject

Returns the value of attribute title.



4
5
6
# File 'lib/applitools/calabash/eyes.rb', line 4

def title
  @title
end

Instance Method Details

#add_context(value) ⇒ Object



34
35
36
# File 'lib/applitools/calabash/eyes.rb', line 34

def add_context(value)
  @context = value
end

#capture_screenshotObject



42
43
44
45
# File 'lib/applitools/calabash/eyes.rb', line 42

def capture_screenshot
  return screenshot_provider.capture_screenshot(debug_suffix: tag_for_debug) unless full_page_capture_algorithm
  full_page_capture_algorithm.get_stitched_region
end

#check(name, target) ⇒ Object



23
24
25
26
# File 'lib/applitools/calabash/eyes.rb', line 23

def check(name, target)
  self.tag_for_debug = get_tag_for_debug(name)
  check_it(name, target, Applitools::MatchWindowData.new)
end

#check_it(name, target, match_window_data) ⇒ Object



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
# File 'lib/applitools/calabash/eyes.rb', line 61

def check_it(name, target, match_window_data)
  Applitools::ArgumentGuard.not_nil(name, 'name')

  logger.info 'Full element requested' if target.options[:stitch_content]

  self.full_page_capture_algorithm = target.options[:stitch_content] &&
    get_full_page_capture_algorithm(target.region_to_check)

  region_provider = if full_page_capture_algorithm
                      entire_screenshot_region
                    else
                      get_region_provider(target)
                    end

  match_window_data.tag = name
  update_default_settings(match_window_data)
  match_window_data.read_target(target, nil)

  self.viewport_size = Applitools::Calabash::EyesSettings.instance.viewport_size if viewport_size.nil?

  if match_window_data.is_a? Applitools::MatchSingleCheckData
    return check_single_base(
      region_provider,
      target.options[:timeout] || Applitools::EyesBase::USE_DEFAULT_TIMEOUT,
      match_window_data
    )
  end

  check_window_base(
    region_provider,
    target.options[:timeout] || Applitools::EyesBase::USE_DEFAULT_TIMEOUT,
    match_window_data
  )
end

#entire_screenshot_regionObject



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/applitools/calabash/eyes.rb', line 111

def entire_screenshot_region
  Object.new.tap do |prov|
    prov.instance_eval do
      define_singleton_method :region do
        Applitools::Region::EMPTY
      end

      define_singleton_method :coordinate_type do
        nil
      end
    end
  end
end

#get_app_output_with_screenshot(*args) ⇒ Object



104
105
106
107
108
109
# File 'lib/applitools/calabash/eyes.rb', line 104

def get_app_output_with_screenshot(*args)
  # super do |screenshot|
  #   screenshot.scale_it!
  # end
  super(*args, &:scale_it!)
end

#get_full_page_capture_algorithm(element) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/applitools/calabash/eyes.rb', line 161

def get_full_page_capture_algorithm(element)
  logger.info "Trying to get full page capture algorithm for element #{element}..."
  environment = Applitools::Calabash::EnvironmentDetector.current_environment
  element_class = Applitools::Calabash::Utils.send("grub_#{environment}_class_name", context, element).first
  logger.info "Trying to get FullPageCaptureAlgorithm for #{element_class}..."
  algo = Applitools::Calabash::FullPageCaptureAlgorithm.get_algorithm_class(environment, element_class)
  if algo
    logger.info "Using #{algo}"
    algo = algo.new(screenshot_provider, element, debug_screenshot_provider: debug_screenshot_provider)
  else
    logger.info "FullPageCaptureAlgorithm for #{element_class} not found. Continue with :check_region instead"
  end
  algo
end

#get_region_provider(target) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/applitools/calabash/eyes.rb', line 96

def get_region_provider(target)
  if (region_to_check = target.region_to_check).nil?
    entire_screenshot_region
  else
    region_for_element(region_to_check)
  end
end

#get_tag_for_debug(name) ⇒ Object



176
177
178
179
# File 'lib/applitools/calabash/eyes.rb', line 176

def get_tag_for_debug(name)
  return "#{current_app_name} #{test_name}" if name.empty?
  "#{current_app_name} #{test_name} - #{name}"
end

#inferred_environmentObject



28
29
30
31
32
# File 'lib/applitools/calabash/eyes.rb', line 28

def inferred_environment
  return @inferred_environment unless @inferred_environment.nil?
  return unless device_pixel_ratio
  "device pixel ratio: #{device_pixel_ratio}"
end

#open(options = {}) ⇒ Object



17
18
19
20
21
# File 'lib/applitools/calabash/eyes.rb', line 17

def open(options = {})
  Applitools::ArgumentGuard.hash options, 'open(options)', [:app_name, :test_name]
  # options[:viewport_size] = Applitools::RectangleSize.from_any_argument options[:viewport_size]
  open_base options
end

#region_for_element(region_to_check) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/applitools/calabash/eyes.rb', line 125

def region_for_element(region_to_check)
  Object.new.tap do |prov|
    prov.instance_eval do
      define_singleton_method :region do
        case region_to_check
        when Applitools::Calabash::CalabashElement
          region_to_check.region
        when Applitools::Region
          region_to_check
        else
          raise Applitools::EyesError, "Incompatible region type: #{region_to_check.class}"
        end
        # region_to_check.respond_to?(:region) ? region_to_check.region : region_to_check
      end
      define_singleton_method :coordinate_type do
        Applitools::Calabash::EyesCalabashScreenshot::DRIVER
      end
    end
  end
end

#remove_contextObject



38
39
40
# File 'lib/applitools/calabash/eyes.rb', line 38

def remove_context
  @context = nil
end

#screenshot_providerObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/applitools/calabash/eyes.rb', line 47

def screenshot_provider
  env = Applitools::Calabash::EnvironmentDetector.current_environment
  case env
  when :android
    Applitools::Calabash::AndroidScreenshotProvider.instance.with_density(device_pixel_ratio)
                                                   .using_context(context)
                                                   .with_debug_screenshot_provider(debug_screenshot_provider)
  when :ios
    Applitools::Calabash::IosScreenshotProvider.instance.with_density(device_pixel_ratio)
                                               .using_context(context)
                                               .with_debug_screenshot_provider(debug_screenshot_provider)
  end
end

#vp_sizeObject Also known as: get_viewport_size



146
147
148
# File 'lib/applitools/calabash/eyes.rb', line 146

def vp_size
  viewport_size
end

#vp_size=(value, skip_check_if_open = false) ⇒ Object Also known as: set_viewport_size



150
151
152
153
154
155
156
# File 'lib/applitools/calabash/eyes.rb', line 150

def vp_size=(value, skip_check_if_open = false)
  unless skip_check_if_open || open?
    raise Applitools::EyesNotOpenException.new 'set_viewport_size: Eyes not open!'
  end
  Applitools::ArgumentGuard.not_nil 'value', value
  @viewport_size = Applitools::RectangleSize.for value
end