Class: PageMagic::Session
- Extended by:
- Forwardable
- Defined in:
- lib/page_magic/session.rb
Overview
class Session - coordinates access to the browser though page objects.
Constant Summary collapse
- URL_MISSING_MSG =
'a path must be mapped or a url supplied'
- INVALID_MAPPING_MSG =
'mapping must be a string or regexp'
- UNSUPPORTED_OPERATION_MSG =
'execute_script not supported by driver'
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#raw_session ⇒ Object
readonly
Returns the value of attribute raw_session.
-
#transitions ⇒ Object
readonly
Returns the value of attribute transitions.
Instance Method Summary collapse
-
#current_page ⇒ Object
is found then nil returned.
-
#define_page_mappings(transitions) ⇒ Object
Map paths to Page classes.
-
#initialize(capybara_session, base_url = nil) ⇒ Session
constructor
Create a new session instance.
- #is_a?(klass) ⇒ Boolean
-
#method_missing(name, *args, &block) ⇒ Object
proxies unknown method calls to the currently loaded page object.
- #on?(page_class) ⇒ Boolean
-
#respond_to_missing?(*args) ⇒ Boolean
True if self or the current page object responds to the give method name.
-
#visit(page_or_url = nil, url: nil) ⇒ PageMagic::Session
Direct the browser to the given page or url.
Constructor Details
#initialize(capybara_session, base_url = nil) ⇒ Session
Create a new session instance
23 24 25 26 27 |
# File 'lib/page_magic/session.rb', line 23 def initialize(, base_url = nil) @raw_session = @base_url = base_url define_page_mappings({}) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
proxies unknown method calls to the currently loaded page object
57 58 59 60 61 |
# File 'lib/page_magic/session.rb', line 57 def method_missing(name, *args, &block) return raw_session.send(name, *args, &block) if raw_session.respond_to?(name) current_page.send(name, *args, &block) end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
16 17 18 |
# File 'lib/page_magic/session.rb', line 16 def base_url @base_url end |
#raw_session ⇒ Object (readonly)
Returns the value of attribute raw_session.
16 17 18 |
# File 'lib/page_magic/session.rb', line 16 def raw_session @raw_session end |
#transitions ⇒ Object (readonly)
Returns the value of attribute transitions.
16 17 18 |
# File 'lib/page_magic/session.rb', line 16 def transitions @transitions end |
Instance Method Details
#current_page ⇒ Object
is found then nil returned
31 32 33 34 35 |
# File 'lib/page_magic/session.rb', line 31 def current_page mapping = transitions.mapped_page(current_url) @current_page = initialize_page(mapping) if mapping @current_page end |
#define_page_mappings(transitions) ⇒ Object
Map paths to Page classes. The session will auto load page objects from these mapping when the Session#current_path is matched.
45 46 47 |
# File 'lib/page_magic/session.rb', line 45 def define_page_mappings(transitions) @transitions = Transitions.new(transitions) end |
#is_a?(klass) ⇒ Boolean
49 50 51 52 53 |
# File 'lib/page_magic/session.rb', line 49 def is_a?(klass) return true if klass == Capybara::Session super end |
#on?(page_class) ⇒ Boolean
63 64 65 |
# File 'lib/page_magic/session.rb', line 63 def on?(page_class) current_page.is_a?(page_class) end |
#respond_to_missing?(*args) ⇒ Boolean
Returns true if self or the current page object responds to the give method name.
69 70 71 |
# File 'lib/page_magic/session.rb', line 69 def respond_to_missing?(*args) super || current_page.respond_to?(*args) || raw_session.respond_to?(*args) end |
#visit(page: page_object) ⇒ PageMagic::Session #visit(url: url) ⇒ PageMagic::Session #visit(page: page_class, url: url) ⇒ PageMagic::Session
Direct the browser to the given page or url. #current_page will be set be an instance of the given/mapped page class
87 88 89 90 91 92 93 94 95 |
# File 'lib/page_magic/session.rb', line 87 def visit(page_or_url = nil, url: nil) url ||= page_or_url.is_a?(String) ? page_or_url : transitions.url_for(page_or_url, base_url: base_url) raise InvalidURLException, URL_MISSING_MSG unless url raw_session.visit(url) @current_page = initialize_page(page_or_url) unless page_or_url.is_a?(String) self end |