Class: PageMagic::Session

Inherits:
Object
  • 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'
REGEXP_MAPPING_MSG =
'URL could not be derived because mapping contains Regexps'
INVALID_MAPPING_MSG =
'mapping must be a string or regexp'

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.



17
18
19
20
21
22
# File 'lib/page_magic/session.rb', line 17

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

Returns:

  • (Object)

    returned object from the page object method call



65
66
67
# File 'lib/page_magic/session.rb', line 65

def method_missing(name, *args, &block)
  current_page.send(name, *args, &block)
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



12
13
14
# File 'lib/page_magic/session.rb', line 12

def base_url
  @base_url
end

#raw_sessionObject (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

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

is found then nil returned

Returns:

  • (Object)

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



26
27
28
29
30
# File 'lib/page_magic/session.rb', line 26

def current_page
  mapping = find_mapped_page(current_url)
  @current_page = initialize_page(mapping) if mapping
  @current_page
end

#current_pathString

Returns path in the browser.

Returns:

  • (String)

    path in the browser



33
34
35
# File 'lib/page_magic/session.rb', line 33

def current_path
  raw_session.current_path
end

#current_urlString

Returns full url in the browser.

Returns:

  • (String)

    full url in the browser



38
39
40
# File 'lib/page_magic/session.rb', line 38

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.

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.



50
51
52
53
54
55
# File 'lib/page_magic/session.rb', line 50

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_scriptObject

execute javascript on the browser

Parameters:

  • script (String)

    the script to be executed

Returns:

  • (Object)

    object returned by the capybara execute_script method



61
# File 'lib/page_magic/session.rb', line 61

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.

Parameters:

  • args

    see Object#respond_to?

Returns:

  • (Boolean)

    true if self or the current page object responds to the give method name



71
72
73
# File 'lib/page_magic/session.rb', line 71

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

Overloads:

  • #visit(page: page_object) ⇒ Object

    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) ⇒ Object

    Parameters:

    • url (String) (defaults to: url)

      url to be visited.

  • #visit(page: page_class, url: url) ⇒ Object

    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.

Raises:



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/page_magic/session.rb', line 88

def visit(page = nil, url: nil)
  if url
    raw_session.visit(url)
  elsif (mapping = transitions.key(page))
    fail InvalidURLException, REGEXP_MAPPING_MSG unless mapping.can_compute_uri?
    raw_session.visit(url(base_url, mapping.compute_uri))
  else
    fail InvalidURLException, URL_MISSING_MSG
  end
  @current_page = initialize_page(page) if page
  self
end