Module: Appom::ElementCache::CacheAwareFinder

Defined in:
lib/appom/element_cache.rb

Overview

Cache-aware element finder mixin

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



233
234
235
236
237
238
239
# File 'lib/appom/element_cache.rb', line 233

def self.included(klass)
  # Don't alias if methods don't exist yet
  klass.send(:alias_method, :original_find_element, :find_element) if klass.method_defined?(:find_element)
  return unless klass.method_defined?(:find_elements)

  klass.send(:alias_method, :original_find_elements, :find_elements)
end

Instance Method Details

#cache_statsObject

Get cache statistics



309
310
311
# File 'lib/appom/element_cache.rb', line 309

def cache_stats
  element_cache.statistics
end

#clear_element_cacheObject

Clear cache for this finder



304
305
306
# File 'lib/appom/element_cache.rb', line 304

def clear_element_cache
  element_cache.clear
end

#element_cacheObject



296
297
298
299
300
301
# File 'lib/appom/element_cache.rb', line 296

def element_cache
  @element_cache ||= Cache.new(
    max_size: Appom.cache_config[:max_size],
    ttl: Appom.cache_config[:ttl],
  )
end

#find_element(strategy, locator, use_cache: true) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/appom/element_cache.rb', line 241

def find_element(strategy, locator, use_cache: true)
  # If not using cache or caching is disabled, use standard find method
  return _find_without_cache(strategy, locator) unless use_cache && begin
    Appom.cache_config[:enabled]
  rescue StandardError
    true
  end

  # Use global cache
  cache_key = Appom::ElementCache.cache.generate_key(strategy, locator)
  cached = Appom::ElementCache.cache.get(cache_key)

  return cached if cached

  element = _find_without_cache(strategy, locator)
  Appom::ElementCache.cache.store(strategy, locator, element) if element
  element
end

#find_elements(strategy, locator, use_cache: true) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/appom/element_cache.rb', line 275

def find_elements(strategy, locator, use_cache: true)
  # If original method doesn't exist, delegate to the page/driver
  return page.find_elements(strategy, locator) unless respond_to?(:original_find_elements)

  return original_find_elements(strategy, locator) unless use_cache && begin
    Appom.cache_config[:enabled]
  rescue StandardError
    true
  end

  # Use global cache
  cache_key = Appom::ElementCache.cache.generate_key(strategy, locator)
  cached = Appom::ElementCache.cache.get(cache_key)

  return cached if cached

  elements = original_find_elements(strategy, locator)
  Appom::ElementCache.cache.store(strategy, locator, elements) if elements
  elements
end