module Howitzer::Web::SectionDsl::ClassMethods

This module holds section dsl class methods

Protected Instance Methods

section(name, *args, **options, &block) click to toggle source

DSL method which defines named or anonymous section within a page or a section @note This method generates following dynamic methods:

<b><em>section_name</em>_section</b> - equals capybara #find(...) method

<b><em>section_name</em>_sections</b> - equals capybara #all(...) method

<b><em>section_name</em>_sections.first</b> - equals capybara #first(...) method

<b>has_<em>section_name</em>_section?</b> - equals capybara #has_selector(...) method

<b>has_no_<em>section_name</em>_section?</b> - equals capybara #has_no_selector(...) method

@note It is possible to use nested anonymous sections @param name [Symbol, String] an unique section name @param args [Array] original Capybara arguments. For details, see ‘Capybara::Node::Finders#all.

(In most cases should be ommited for named sections because the section selector is specified
with `#me` method. But must be specified for anonymous sections)

@param options [Hash] original Capybara options. For details, see ‘Capybara::Node::Finders#all.

(In most cases should be ommited for named sections because the section selector is specified
with `#me` method. But may be specified for anonymous sections)

@param block [Proc] this block can contain nested sections and elements @example Named section

class MenuSection < Howitzer::Web::Section
  me :xpath, ".//*[@id='panel']"
end
class HomePage < Howitzer::Web::Page
  section :menu
end
HomePage.on { is_expected.to have_menu_section }

@example Anonymous section

class HomePage < Howitzer::Web::Page
  section :info_panel, '#panel' do
    element :edit_button, '.edit'
    element :title_field, '.title'

    def edit_info(title: nil)
      edit_button_element.click
      title_field_element.set(title)
    end
  end
end
HomePage.on { info_panel.edit_info(title: 'Test Title') }

@example Anonymous nested section

class HomePage < Howitzer::Web::Page
  section :info_panel, '#panel' do
    element :edit_button, '.edit'

    section :form, '.form' do
      element :title_field, '.title'
    end
  end
end
HomePage.on { info_panel_section.edit_info(title: 'Test Title') }

@!visibility public

# File lib/howitzer/web/section_dsl.rb, line 127
def section(name, *args, **options, &block)
  scope = SectionScope.new(name, *args, **options, &block)
  define_section_method(scope.section_class, name, *scope.finder_args, **scope.finder_options)
  define_sections_method(scope.section_class, name, *scope.finder_args, **scope.finder_options)
  define_has_section_method(name, *scope.finder_args, **scope.finder_options)
  define_has_no_section_method(name, *scope.finder_args, **scope.finder_options)
end

Private Instance Methods

define_has_no_section_method(name, *args, **options) click to toggle source
# File lib/howitzer/web/section_dsl.rb, line 170
def define_has_no_section_method(name, *args, **options)
  define_method("has_no_#{name}_section?") do |*block_args, **block_options|
    conv_args, conv_options = convert_arguments(args, options, block_args, block_options)
    if conv_options.present?
      capybara_context.has_no_selector?(*conv_args, **conv_options)
    else
      capybara_context.has_no_selector?(*conv_args)
    end
  end
end
define_has_section_method(name, *args, **options) click to toggle source
# File lib/howitzer/web/section_dsl.rb, line 159
def define_has_section_method(name, *args, **options)
  define_method("has_#{name}_section?") do |*block_args, **block_options|
    conv_args, conv_options = convert_arguments(args, options, block_args, block_options)
    if conv_options.present?
      capybara_context.has_selector?(*conv_args, **conv_options)
    else
      capybara_context.has_selector?(*conv_args)
    end
  end
end
define_section_method(klass, name, *args, **options) click to toggle source
# File lib/howitzer/web/section_dsl.rb, line 137
def define_section_method(klass, name, *args, **options)
  define_method("#{name}_section") do |*block_args, **block_options|
    conv_args, conv_options = convert_arguments(args, options, block_args, block_options)
    if conv_options.present?
      klass.new(self, capybara_context.find(*conv_args, **conv_options))
    else
      klass.new(self, capybara_context.find(*conv_args))
    end
  end
end
define_sections_method(klass, name, *args, **options) click to toggle source
# File lib/howitzer/web/section_dsl.rb, line 148
def define_sections_method(klass, name, *args, **options)
  define_method("#{name}_sections") do |*block_args, **block_options|
    conv_args, conv_options = convert_arguments(args, options, block_args, block_options)
    if conv_options.present?
      capybara_context.all(*conv_args, **conv_options).map { |el| klass.new(self, el) }
    else
      capybara_context.all(*conv_args).map { |el| klass.new(self, el) }
    end
  end
end