Class: PageMagic::Session
- Inherits:
-
Object
- Object
- 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'- REGEXP_MAPPING_MSG =
'URL could not be derived because mapping is a Regexp'- INVALID_MAPPING_MSG =
'mapping must be a string or regexp'
Instance Attribute Summary collapse
-
#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.
-
#current_path ⇒ String
Path in the browser.
-
#current_url ⇒ String
Full url in the browser.
-
#define_page_mappings(transitions) ⇒ Object
Map paths to Page classes.
-
#execute_script ⇒ Object
execute javascript on the browser.
-
#initialize(capybara_session, url = nil) ⇒ Session
constructor
Create a new session instance.
-
#method_missing(name, *args, &block) ⇒ Object
proxies unknown method calls to the currently loaded page object.
-
#respond_to?(*args) ⇒ Boolean
True if self or the current page object responds to the give method name.
-
#visit(page = nil, url: nil) ⇒ Object
Direct the browser to the given page or url.
Constructor Details
#initialize(capybara_session, url = nil) ⇒ Session
Create a new session instance
17 18 19 20 21 |
# File 'lib/page_magic/session.rb', line 17 def initialize(, url = nil) @raw_session = visit(url: url) if url @transitions = {} 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
61 62 63 |
# File 'lib/page_magic/session.rb', line 61 def method_missing(name, *args, &block) current_page.send(name, *args, &block) end |
Instance Attribute Details
#raw_session ⇒ Object (readonly)
Returns the value of attribute raw_session.
12 13 14 |
# File 'lib/page_magic/session.rb', line 12 def raw_session @raw_session end |
#transitions ⇒ Object (readonly)
Returns the value of attribute transitions.
12 13 14 |
# File 'lib/page_magic/session.rb', line 12 def transitions @transitions end |
Instance Method Details
#current_page ⇒ Object
is found then nil returned
25 26 27 28 29 |
# File 'lib/page_magic/session.rb', line 25 def current_page mapping = find_mapped_page(current_path) @current_page = initialize_page(mapping) if mapping @current_page end |
#current_path ⇒ String
Returns path in the browser.
32 33 34 |
# File 'lib/page_magic/session.rb', line 32 def current_path raw_session.current_path end |
#current_url ⇒ String
Returns full url in the browser.
37 38 39 |
# File 'lib/page_magic/session.rb', line 37 def current_url raw_session.current_url end |
#define_page_mappings(transitions) ⇒ Object
Map paths to Page classes. The session will auto load page objects from these mapping when the #current_path is matched.
49 50 51 |
# File 'lib/page_magic/session.rb', line 49 def define_page_mappings(transitions) @transitions = transitions end |
#execute_script ⇒ Object
execute javascript on the browser
57 |
# File 'lib/page_magic/session.rb', line 57 def_delegator :raw_session, :execute_script |
#respond_to?(*args) ⇒ Boolean
Returns true if self or the current page object responds to the give method name.
67 68 69 |
# File 'lib/page_magic/session.rb', line 67 def respond_to?(*args) super || current_page.respond_to?(*args) end |
#visit(page: page_object) ⇒ Object #visit(url: url) ⇒ Object #visit(page: page_class, url: url) ⇒ Object
Direct the browser to the given page or url. #current_page will be set be an instance of the given/mapped page class
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/page_magic/session.rb', line 84 def visit(page = nil, url: nil) if url raw_session.visit(url) elsif (path = transitions.key(page)) fail InvalidURLException, REGEXP_MAPPING_MSG if path.is_a?(Regexp) raw_session.visit(url(current_url, path)) else fail InvalidURLException, URL_MISSING_MSG end @current_page = initialize_page(page) if page self end |