module SitePrism::DSL
SitePrism::DSL
-
This is the core Module Namespace for all of the public-facing
DSL
methodssuch 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
# File lib/site_prism/dsl.rb, line 12 def self.included(klass) klass.extend ClassMethods end
Private Instance Methods
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
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
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
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
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
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
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 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