lib/page_magic/session.rb in page_magic-1.0.0.alpha9 vs lib/page_magic/session.rb in page_magic-1.0.0.alpha10

- old
+ new

@@ -1,18 +1,25 @@ require 'wait' module PageMagic + class InvalidURLException < Exception;end + class Session + URL_MISSING_MSG = 'a url must be specified as either a parameter or on the page class' + REGEXP_MAPPING_MSG = 'URL could not be derived because mapping is a Regexp' + attr_accessor :current_page, :raw_session, :transitions def initialize(browser) @raw_session = browser + @transitions = {} end def define_page_mappings(transitions) @transitions = transitions end + def current_page if transitions mapping = find_mapped_page(current_path) @current_page = mapping.new(self) if mapping end @@ -24,14 +31,31 @@ string_matches?(path, key) end transitions[mapping] end - def visit(page, url: nil) - raw_session.visit url || page.url - @current_page = page.new self + + + def visit(page_or_url, url:nil) + if page_or_url.is_a?(String) + raw_session.visit page_or_url + elsif page_or_url.ancestors.include?(PageMagic) + page = page_or_url + if url + raw_session.visit(url) + elsif page.url + raw_session.visit(page.url) + elsif path = transitions.key(page) + raise InvalidURLException, REGEXP_MAPPING_MSG if path.is_a?(Regexp) + raw_session.visit("#{current_url}#{path}") + else + raise InvalidURLException, URL_MISSING_MSG + end + @current_page = page.new self + end self end + def current_path raw_session.current_path end