module SitePrism::DSL

SitePrism::DSL

This is the core Module Namespace for all of the public-facing DSL methods

such as `element`. The code here is designed to be used through the defining
of said items, and not to be instantiated directly.

The whole package here can be thought of as [@api private]

Public Class Methods

included(klass) click to toggle source
# File lib/site_prism/dsl.rb, line 12
def self.included(klass)
  klass.extend ClassMethods
end

Private Instance Methods

_all(*find_args) click to toggle source

Call `all` inside context set on page/section

# File lib/site_prism/dsl.rb, line 25
def _all(*find_args)
  kwargs = find_args.pop
  page.all(*find_args, **kwargs)
end
_find(*find_args) click to toggle source

Call `find` inside context set on page/section

# File lib/site_prism/dsl.rb, line 19
def _find(*find_args)
  kwargs = find_args.pop
  page.find(*find_args, **kwargs)
end
element_does_not_exist?(*find_args) click to toggle source

Call `has_no_selector?` inside context set on page/section

# File lib/site_prism/dsl.rb, line 37
def element_does_not_exist?(*find_args)
  kwargs = find_args.pop
  page.has_no_selector?(*find_args, **kwargs)
end
element_exists?(*find_args) click to toggle source

Call `has_selector?` inside context set on page/section

# File lib/site_prism/dsl.rb, line 31
def element_exists?(*find_args)
  kwargs = find_args.pop
  page.has_selector?(*find_args, **kwargs)
end
merge_args(find_args, runtime_args, visibility_args = {}) click to toggle source

Sanitize method called before calling any SitePrism DSL method or meta-programmed method. This ensures that the Capybara query is correct.

Accepts any combination of arguments sent at DSL definition or runtime and combines them in such a way that Capybara can operate with them.

# File lib/site_prism/dsl.rb, line 78
def merge_args(find_args, runtime_args, visibility_args = {})
  find_args = find_args.dup
  runtime_args = runtime_args.dup
  options = visibility_args.dup
  SitePrism.logger.debug("Initial args: #{find_args}, #{runtime_args}.")

  recombine_args(find_args, runtime_args, options)

  return [*find_args, *runtime_args, {}] if options.empty?

  [*find_args, *runtime_args, options]
end
raise_if_block(obj, name, has_block, type) click to toggle source

Prevent users from calling methods with blocks when they shouldn't be.

Example (Triggering error):

class MyPage
  element :sample, '.css-locator' do
    puts "This won't be output"
  end
end

At runtime this will generate a `SitePrism::UnsupportedBlockError`

The only DSL keywords that can use blocks are :section and :iframe

# File lib/site_prism/dsl.rb, line 55
def raise_if_block(obj, name, has_block, type)
  return unless has_block

  SitePrism.logger.debug("Type passed in: #{type}")
  SitePrism.logger.warn('section / iFrame can only accept blocks.')
  SitePrism.logger.error("#{obj.class}##{name} does not accept blocks")

  raise SitePrism::UnsupportedBlockError
end
recombine_args(find_args, runtime_args, options) click to toggle source

Options re-combiner. This takes the original inputs and combines them such that there is only one hash passed as a final argument to Capybara.

If the hash is empty, then the hash is omitted from the payload sent to Capybara, and the find / runtime arguments are sent alone.

NB: If the wait key is present in the options hash, even as false or 0, It will be set as the user-supplied value (So user error can be the cause for issues).

# File lib/site_prism/dsl.rb, line 100
def recombine_args(find_args, runtime_args, options)
  options.merge!(find_args.pop) if find_args.last.is_a? Hash
  options.merge!(runtime_args.pop) if runtime_args.last.is_a? Hash
  options[:wait] = Capybara.default_max_wait_time unless options.key?(:wait)
end
warn_if_dsl_collision(obj, name) click to toggle source

Warn users from naming the elements starting with no_

# File lib/site_prism/dsl.rb, line 66
def warn_if_dsl_collision(obj, name)
  return unless name.to_s.start_with?('no_')

  SitePrism.logger.warn("#{obj.class}##{name} should not start with no_")
  SitePrism::Deprecator.deprecate('Using no_ prefix in DSL definition')
end