Sha256: fbf3b0072cd19bcd24a91abd4288a1a7c5c652ca92801452a9da609228bee208

Contents?: true

Size: 1.31 KB

Versions: 5

Compression:

Stored size: 1.31 KB

Contents

require 'wait'
module PageMagic
  class Session
    attr_accessor :current_page, :raw_session, :transitions

    def initialize(browser)
      @raw_session = browser
    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
      @current_page
    end

    def find_mapped_page(path)
      mapping = transitions.keys.find do |key|
        string_matches?(path, key)
      end
      transitions[mapping]
    end

    def visit(page, url: nil)
      raw_session.visit url || page.url
      @current_page = page.new self
      self
    end

    def current_path
      raw_session.current_path
    end

    def current_url
      raw_session.current_url
    end

    def wait_until(&block)
      @wait ||= Wait.new
      @wait.until &block
    end

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

    def respond_to?(*args)
      super || current_page.respond_to?(*args)
    end

    private

    def string_matches?(string, matcher)
      if matcher.is_a?(Regexp)
        string =~ matcher
      elsif matcher.is_a?(String)
        string == matcher
      else
        false
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
page_magic-1.0.0.alpha9 lib/page_magic/session.rb
page_magic-1.0.0.alpha8 lib/page_magic/session.rb
page_magic-1.0.0.alpha7 lib/page_magic/session.rb
page_magic-1.0.0.alpha6 lib/page_magic/session.rb
page_magic-1.0.0.alpha5 lib/page_magic/session.rb