Sha256: b0fd763b630ac893315898d965b26a1679e28d8dd08142ed2b92a14949b30236

Contents?: true

Size: 1.82 KB

Versions: 3

Compression:

Stored size: 1.82 KB

Contents

require 'rutl/utilities'
require 'rutl/base_page'

#
# Currently called Browser, this top-level class controls a browser and
# a fake browser. It will soon call into apps, at which point I need to
# rethink this naming convention.
#
class Browser
  include Utilities

  def initialize(interface_type: :none, page_object_dir: 'spec/pages')
    @pages = load_pages(page_object_dir: page_object_dir)
    load_interface(type: interface_type)
  end

  def load_interface(type: :none)
    pages = @pages
    require "rutl/interface/#{type}_interface"
    klass = "#{type.to_s.capitalize}Interface"
    @interface = Object.const_get(klass).new(pages: pages)
    @interface.interface = @interface
  end

  # Ugly. Requires files for page objects. Returns array of class names to load.
  def require_pages(page_object_dir: 'spec/pages')
    names = []
    Dir["#{page_object_dir}/*"].each do |file|
      require "rutl/../../#{file}"
      File.open(file).each do |line|
        names << $1 if line =~ /class (.*) < BasePage/
      end
    end
    names
  end

  def load_pages(*)
    pages = []
    names = require_pages
    names.each do |klass|
      # Don't have @interface set yet.
      # That would have been the param to new, :interface.
      pages << Object.const_get(klass).new
    end
    pages
  end

  def method_missing(method, *args, &block)
    result = if args.empty?
               @interface.send(method)
             else
               @interface.send(method, *args, &block)
             end
    if result.class == Array && (result[0].class.ancestors.include?(BasePage) ||
                                 result[0].class == Exception)
      wait_for_transition(result)
    else
      result
    end
  end

  def respond_to_missing?(*args)
    @interface.respond_to?(*args)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rutl-0.1.3 lib/rutl/browser.rb
rutl-0.1.2 lib/rutl/browser.rb
rutl-0.1.1 lib/rutl/browser.rb