Module: Howitzer::Web::PageValidator::ClassMethods

Defined in:
lib/howitzer/web/page_validator.rb

Overview

This module holds page validation class methods

Instance Method Summary collapse

Instance Method Details

#matched_pagesArray

Finds all matched pages which satisfy of defined validations on current page

Returns:

  • (Array)

    page name list



83
84
85
# File 'lib/howitzer/web/page_validator.rb', line 83

def matched_pages
  PageValidator.validations.keys.select { |klass| klass.opened?(sync: false) }
end

#opened?(sync: true) ⇒ Boolean

Check whether current page is opened or no

Parameters:

  • sync (Boolean) (defaults to: true)

    if true then waits until validation true during Howitzer.capybara_wait_time or returns false. If false, returns result immediately

Returns:

  • (Boolean)

Raises:



74
75
76
77
78
# File 'lib/howitzer/web/page_validator.rb', line 74

def opened?(sync: true)
  return validations.all? { |(_, validation)| validation.call(self, sync) } if validations.present?

  raise Howitzer::NoValidationError, "No any page validation was found for '#{name}' page"
end

#validate(type, pattern_or_element_name, *args, **options) ⇒ Object

Adds validation to validation list for current page

Examples:

class ArticleListPage < Howitzer::Web::Page
  validate :title, /\ADemo web application - Listing Articles\z/
end
class ArticlePage < Howitzer::Web::Page
  validate :url, %r{\/articles\/\d+\/?\z}
end
class HomePage < Howitzer::Web::Page
  validate :element_presence, :menu_item, lambda_args(text: 'Logout')
  element :menu_item, :xpath, ->(text:) { ".//a[.='#{text}']" }
end

Parameters:

  • type (Symbol, String)

    a validation type. Possible values [:url, :element_presence, :title]

  • pattern_or_element_name (Symbol, String, Regexp)

    For :url and :title validation types must be Regexp For :element_presence must be one of element names described for the page

  • args (Array)

    any arguments required to pass for a lambda selector (:element_presence type only)

  • options (Hash)

    keyword arguments required to pass for a lambda selector (:element_presence type only)

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/howitzer/web/page_validator.rb', line 53

def validate(type, pattern_or_element_name, *args, **options)
  case type.to_s.to_sym
  when :url, :title
    if args.present? || options.present?
      raise ArgumentError, "Additional arguments and options are not supported by '#{type}' the validator"
    end

    send("validate_by_#{type}", pattern_or_element_name)
  when :element_presence
    validate_by_element_presence(pattern_or_element_name, *args, **options)
  else
    raise Howitzer::UnknownValidationError, "unknown '#{type}' validation type"
  end
end

#validationsHash

Returns defined validations for current page class.

Returns:

  • (Hash)

    defined validations for current page class



89
90
91
# File 'lib/howitzer/web/page_validator.rb', line 89

def validations
  PageValidator.validations[self] ||= {}
end