Class: PageMagic::Session

Inherits:
Object show all
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

Instance Method Summary collapse

Constructor Details

#initialize(capybara_session, base_url = nil) ⇒ Session

Create a new session instance

Parameters:

  • capybara_session (Object)

    an instance of a capybara session

  • base_url (String) (defaults to: nil)

    url to start the session at.



23
24
25
26
27
# File 'lib/page_magic/session.rb', line 23

def initialize(capybara_session, base_url = nil)
  @raw_session = capybara_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

Returns:

  • (Object)

    returned object from the page object method call



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_urlObject (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_sessionObject (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

#transitionsObject (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_pageObject

is found then nil returned

Returns:

  • (Object)

    returns page object representing the currently loaded page on the browser. If no mapping



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.

Examples:

self.define_page_mappings '/' => HomePage, %r{/messages/d+}

Parameters:

  • transitions (Hash)
    • paths mapped to page classes

Options Hash (transitions):

  • path (String)

    as literal

  • path (Regexp)

    as a regexp for dynamic matching.



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

Returns:

  • (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

Returns:

  • (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.

Parameters:

  • args

    see Object#respond_to?

Returns:

  • (Boolean)

    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

Overloads:

  • #visit(page: page_object) ⇒ PageMagic::Session

    Parameters:

    • page (Object) (defaults to: page_object)

      page class. The required url will be generated using the session's base url and the mapped path

  • #visit(url: url) ⇒ PageMagic::Session

    Parameters:

    • url (String) (defaults to: url)

      url to be visited.

  • #visit(page: page_class, url: url) ⇒ PageMagic::Session

    Parameters:

    • url (String) (defaults to: url)

      url to be visited.

    • page (Object) (defaults to: page_class)

      the supplied page class will be instantiated to be used against the given url.

Returns:

Raises:



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