Class: Ferris::Core

Inherits:
Object
  • Object
show all
Extended by:
Region
Includes:
Watir::Waitable
Defined in:
lib/ferris/core.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Region

region, region_list

Constructor Details

#initialize(browser = Browser.browser) ⇒ Core

Returns a new instance of Core.



50
51
52
53
# File 'lib/ferris/core.rb', line 50

def initialize(browser = Browser.browser)
  @browser = browser
  @base_url = self.class.base_url || ''
end

Class Attribute Details

.base_urlObject

Returns the value of attribute base_url.



99
100
101
# File 'lib/ferris/core.rb', line 99

def base_url
  @base_url
end

.require_urlObject (readonly)

Returns the value of attribute require_url.



98
99
100
# File 'lib/ferris/core.rb', line 98

def require_url
  @require_url
end

.required_element_listObject



111
112
113
# File 'lib/ferris/core.rb', line 111

def required_element_list
  @required_element_list ||= []
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



47
48
49
# File 'lib/ferris/core.rb', line 47

def base_url
  @base_url
end

#browserObject (readonly)

Returns the value of attribute browser.



47
48
49
# File 'lib/ferris/core.rb', line 47

def browser
  @browser
end

#rootObject

Returns the value of attribute root.



48
49
50
# File 'lib/ferris/core.rb', line 48

def root
  @root
end

Class Method Details

.element(name, required: false, &block) ⇒ Object



119
120
121
122
# File 'lib/ferris/core.rb', line 119

def element(name, required: false, &block)
  define_method(name) {|*args| self.instance_exec(*args, &block) }
  required_element_list << name.to_sym if required
end

.inherited(subclass) ⇒ Object



115
116
117
# File 'lib/ferris/core.rb', line 115

def inherited(subclass)
  subclass.required_element_list = required_element_list.dup
end

.page_titleObject



107
108
109
# File 'lib/ferris/core.rb', line 107

def page_title
  define_method('page_title') {|*args| yield(*args) }
end

.page_url(required: false) ⇒ Object Also known as: partial_url



101
102
103
104
# File 'lib/ferris/core.rb', line 101

def page_url(required: false)
  @require_url = required
  define_method('page_url') {|*args| base_url + yield(*args) }
end

Instance Method Details

#check_required_elementsObject



90
91
92
93
94
# File 'lib/ferris/core.rb', line 90

def check_required_elements
  browser.wait_until { |_b| self.class.required_element_list.all? { |e| send(e).present? } }
rescue Watir::Wait::TimeoutError
  false
end

#check_titleObject



86
87
88
# File 'lib/ferris/core.rb', line 86

def check_title
  browser.title == page_title
end

#check_urlObject



82
83
84
# File 'lib/ferris/core.rb', line 82

def check_url
  page_url[%r{^#{URI.parse(page_url).scheme}://(.*?)/?$}, 1] == browser.url[%r{^#{URI.parse(browser.url).scheme}://(.*?)/?$}, 1]
end

#inspectObject Also known as: selector_string



55
56
57
# File 'lib/ferris/core.rb', line 55

def inspect
  '#<%s url=%s title=%s>' % [self.class, url.inspect, title.inspect]
end

#loaded?Boolean

Returns:

  • (Boolean)

Raises:

  • (Selenium::WebDriver::Error::WebDriverError)


77
78
79
80
# File 'lib/ferris/core.rb', line 77

def loaded?
  raise Selenium::WebDriver::Error::WebDriverError, 'Cannot verify a page/region without requirements' unless page_verifiable?
  [!self.class.require_url || check_url, !respond_to?(:page_title) || check_title, !self.class.required_element_list.any? || check_required_elements].all?
end

#present?Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/ferris/core.rb', line 60

def present?
  raise 'No root element set.' unless root
  root.present?
end

#verifiable?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/ferris/core.rb', line 73

def verifiable?
  self.class.require_url || self.respond_to?(:page_title) || self.class.required_element_list.any?
end

#visit(*args) ⇒ Object

Raises:

  • (exception)


65
66
67
68
69
70
71
# File 'lib/ferris/core.rb', line 65

def visit(*args)
  browser.goto page_url(*args)
  exception = Selenium::WebDriver::Error::WebDriverError
  message = "Expected to be on #{self.class}, but conditions not met"
  raise exception, message if verifiable? && !loaded?
  self
end