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'.freeze
- REGEXP_MAPPING_MSG =
'URL could not be derived because mapping contains Regexps'.freeze
- INVALID_MAPPING_MSG =
'mapping must be a string or regexp'.freeze
- UNSUPPORTED_OPERATION_MSG =
'execute_script not supported by driver'.freeze
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.
-
#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(script) ⇒ Object
execute javascript on the browser @param [String] script the script to be executed @return [Object] object returned by the capybara execute_script method.
-
#initialize(capybara_session, base_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, base_url = nil) ⇒ Session
Create a new session instance
18 19 20 21 22 23 |
# File 'lib/page_magic/session.rb', line 18 def initialize(, base_url = nil) @raw_session = @base_url = base_url visit(url: base_url) if base_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
70 71 72 |
# File 'lib/page_magic/session.rb', line 70 def method_missing(name, *args, &block) current_page.send(name, *args, &block) end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
13 14 15 |
# File 'lib/page_magic/session.rb', line 13 def base_url @base_url end |
#raw_session ⇒ Object (readonly)
Returns the value of attribute raw_session.
13 14 15 |
# File 'lib/page_magic/session.rb', line 13 def raw_session @raw_session end |
#transitions ⇒ Object (readonly)
Returns the value of attribute transitions.
13 14 15 |
# File 'lib/page_magic/session.rb', line 13 def transitions @transitions end |
Instance Method Details
#current_page ⇒ Object
is found then nil returned
27 28 29 30 31 |
# File 'lib/page_magic/session.rb', line 27 def current_page mapping = find_mapped_page(current_url) @current_page = initialize_page(mapping) if mapping @current_page end |
#current_path ⇒ String
Returns path in the browser.
34 35 36 |
# File 'lib/page_magic/session.rb', line 34 def current_path raw_session.current_path end |
#current_url ⇒ String
Returns full url in the browser.
39 40 41 |
# File 'lib/page_magic/session.rb', line 39 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.
51 52 53 54 55 56 |
# File 'lib/page_magic/session.rb', line 51 def define_page_mappings(transitions) @transitions = transitions.collect do |key, value| key = key.is_a?(Matcher) ? key : Matcher.new(key) [key, value] end.to_h end |
#execute_script(script) ⇒ Object
execute javascript on the browser @param [String] script the script to be executed @return [Object] object returned by the capybara execute_script method
62 63 64 65 66 |
# File 'lib/page_magic/session.rb', line 62 def execute_script(script) raw_session.execute_script(script) rescue Capybara::NotSupportedByDriverError raise NotSupportedException, UNSUPPORTED_OPERATION_MSG end |
#respond_to?(*args) ⇒ Boolean
Returns true if self or the current page object responds to the give method name.
76 77 78 |
# File 'lib/page_magic/session.rb', line 76 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
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/page_magic/session.rb', line 93 def visit(page = nil, url: nil) target_url = url || begin if (mapping = transitions.key(page)) raise InvalidURLException, REGEXP_MAPPING_MSG unless mapping.can_compute_uri? url(base_url, mapping.compute_uri) end end raise InvalidURLException, URL_MISSING_MSG unless target_url raw_session.visit(target_url) @current_page = initialize_page(page) if page self end |